Everyday
| Command | What it does |
|---|
git status / git diff / git diff --staged | what changed / unstaged / staged |
git add -p | stage part of a file |
git commit -m "msg" | commit what is staged |
git log --oneline --graph --all | the history as a graph |
git show <commit> | one commit in full |
git blame file | who last changed each line |
Branches
| Command | What it does |
|---|
git switch -c feature | create and move to a branch |
git switch main | move back |
git branch -d feature | delete a merged branch |
git merge feature | merge it into the current branch |
git rebase main | replay my commits on top of main |
git rebase -i main | squash, reword or drop my commits |
Conflicts
Edit the files between <<<<<<< and >>>>>>>, then git add file and git commit (merge) or git rebase --continue (rebase). Give up: git merge --abort / git rebase --abort.
Remotes
| Command | What it does |
|---|
git fetch / git pull --rebase | download / download and rebase |
git push -u origin feature | first push of a branch |
git push --force-with-lease | push a rewritten branch, safely |
Undo
| Situation | Command |
|---|
| discard changes to a file | git restore file |
| unstage a file | git restore --staged file |
| fix the last commit | git commit --amend |
| undo last commit, keep changes | git reset --soft HEAD~1 |
| throw away last commit and changes | git reset --hard HEAD~1 |
| undo a pushed commit | git revert <commit> |
| copy one commit here | git cherry-pick <commit> |
| put work aside | git stash / git stash pop |
| find a lost commit | git reflog |
Find the commit that broke it
git bisect start
git bisect bad # this commit is broken
git bisect good v1.2 # this one worked
# test, then: git bisect good | git bisect bad … until Git names the commit
git bisect run ./test.sh # or let a script decide
git bisect reset
Find any command in the Git command finder.