Git Survival Kit
Eight commands that get you out of the usual Git trouble. Each one was run in a scratch repository and the real output is shown under a Test line. Make a copy of your repository before trying any of them.
1. Undo the last commit but keep your work
Undo the last commit but keep your work?
Moves the branch back one commit and leaves the changes staged.
Command: git reset --soft HEAD~1
f89429f one
A two.txtTest: PASS (git 2.34.1, scratch repo, exit 0) Watch out: --soft keeps changes staged; --hard would delete them. Check git status after. Source: https://git-scm.com/docs/git-reset
2. Fix the last commit message
Fix the last commit message?
Rewrites only the newest commit. Do not amend a commit you already pushed.
Command: git commit --amend -m "better message"
96ac970 better messageTest: PASS (git 2.34.1, scratch repo, exit 0) Watch out: If the commit is already pushed, make a new commit instead of amending. Source: https://git-scm.com/docs/git-commit
3. Park unfinished work with stash
Park unfinished work with stash?
Stash saves tracked changes and gives you a clean tree. Pop brings them back.
Command: git stash; git stash pop
clean:
restored:
M one.txtTest: PASS (git 2.34.1, scratch repo, exit 0) Watch out: Untracked files are not stashed unless you add -u. Run git stash list to see what is parked. Source: https://git-scm.com/docs/git-stash
4. Throw away changes in one file
Throw away changes in one file?
Restores the file from the last commit. The discarded edits cannot be recovered.
Command: git restore one.txt
(empty = clean)Test: PASS (git 2.34.1, scratch repo, exit 0) Watch out: There is no undo for this. If unsure, stash the file instead. Source: https://git-scm.com/docs/git-restore
5. Recover a commit after reset --hard
Recover a commit after reset --hard?
The reflog records where HEAD was. Reset back to the entry from before the mistake.
Command: git reflog; git reset --hard HEAD@{1}
after reset:
f89429f one
recovered:
90d3353 two
f89429f oneTest: PASS (git 2.34.1, scratch repo, exit 0) Watch out: Reflog entries expire after a while and live only on your machine. Act soon. Source: https://git-scm.com/docs/git-reflog
6. Copy one commit to another branch
Copy one commit to another branch?
Applies one commit as a new commit on the current branch.
Command: git cherry-pick <hash>
828ab7d fix
0c9ea98 baseTest: PASS (git 2.34.1, scratch repo, exit 0) Watch out: If the commit touches code that differs between branches you may get a conflict. Resolve it, then git cherry-pick --continue. Source: https://git-scm.com/docs/git-cherry-pick
7. Back out of a merge conflict
Back out of a merge conflict?
Abort returns the branch to the state before the merge started.
Command: git merge --abort
conflict
UU f
aborted:
(empty = clean)Test: PASS (git 2.34.1, scratch repo, exit 0) Watch out: Use abort only before you commit the merge. After that, use git reset or git revert. Source: https://git-scm.com/docs/git-merge
8. Find the commit that broke something
Find the commit that broke something?
Bisect binary-searches history with a test script and names the first bad commit.
Command: git bisect start; git bisect bad; git bisect good <hash>; git bisect run ./test.sh
first bad commit: c4Test: PASS (git 2.34.1, scratch repo, exit 0) Watch out: The test script must exit 0 for good and non-zero for bad. Run git bisect reset when done. Source: https://git-scm.com/docs/git-bisect
Worked example: a bad afternoon
You stash half-finished work, ship a hotfix, bring the work back and commit it. Then you run reset --hard by mistake and lose two commits. The reflog gets them back. Read the log before and after to confirm.
after mistake:
4a6e5f5 app
after recovery:
1c8fec8 feature
48d1690 hotfix
4a6e5f5 appTest: PASS (git 2.34.1, scratch repo, exit 0) Source: https://git-scm.com/docs/git-reset
Quick reference
git reset --soft HEAD~1: Undo the last commit but keep your workgit commit --amend -m "better message": Fix the last commit messagegit stash: Park unfinished work with stashgit restore one.txt: Throw away changes in one filegit reflog: Recover a commit after reset --hardgit cherry-pick <hash>: Copy one commit to another branchgit merge --abort: Back out of a merge conflictgit bisect start: Find the commit that broke something
Words used in this kit
- HEAD: the commit you are on now
- staged: changes marked for the next commit with git add
- working tree: the files on disk you are editing
- reflog: a local log of where HEAD has pointed
- branch: a movable name for a line of commits
- conflict: two changes to the same lines that Git cannot merge alone
Checklist
- Before any history-changing command: copy the repository, run git status, read the output, then act.
- Never force-push a shared branch without telling the people who use it.
Note: all outputs are from a scratch repository. Keys shown are fake. Commit hashes differ on your machine.
All full guides: /guide