To understand git
use the terminal
Once you understand git
, use your IDE functionality
git config --local
git config --global
git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"
git config --global --list
This is the bare minimum config to be able to commit
man git config
You might get a warning about branch naming.
git config --global init.defaultBranch main
Everything in git is tracked using files in
.git
As long as you dont remove or modify files manually inside
.git
it is generally difficult permanently to lose
work (in tracked files)
Files get added to the staging area
A commit integrates changes in the staging area into the git history
add
the files prior to making the
commit
Branch pointers get updated as we generate commits
Branches are just updatable pointers
where's my_new_branch
and its last commit?!
git log --graph --all
main
and my_new_branch
have
diverged
merge-commits have two parents
What will happen here when we
git merge main
?
Note how HEAD
is being updated on each
switch
But, can we inspect those changes prior to merging them?
git pull
is equivalent to fetching and
merging
You can use git merge --abort
to start over
git status
to track to what branches you are
commiting
git reset [commit]
You can use HEAD~N to refer to the Nth commit
After the reset commit 0bad
is dangling.
No branch points to it and no one has it as its parent. Did we lose our work?
git reflog
shows the history of the
HEAD
and can help us reach dangling commits
git reset
if you did it backwards ;)
revert-commits apply the inverse change of the commit they
revert
git checkout [commit|branch]
|
Move to a [commit] or [branch] |
git reset [commit]
|
Reset branch to [commit] |
git reflog
|
History of HEAD |
git log --oneline --graph --all
|
Represent repo in graph form |
git checkout [file]
|
Remove changes with respect to HEAD in [file] |
git reset --hard
|
Clear all changes in tracked files |
git diff -w
|
Show difference ignoring white-space |
checkout and reset --hard can result in loss of work if the changes removed have not been commited anywhere
git rebase |
rebase can prevent merges by replaying changes, making histories more linear |
git cherrypick |
with cherrypick you can apply changes from specific commits instead of whole branches |
git worktree |
with worktrees you can have several branches/commits checked out |