Sub topic 9
Everyday recipes
This chapter collects the reset commands you will use most often. It assumes you know git init, add, commit, status, and log, that you are comfortable with branches, and that you know what --soft, --mixed, and --hard change in HEAD, the index, and the working directory. Each recipe names the situation, shows the commands, and states what to check afterward.
Set up a practice repository
Run the recipes in a throwaway repository so you can see each result. This one has three commits.
git init demo && cd demo
echo one > file.txt
git add file.txt
git commit -m "first"
echo two >> file.txt
git commit -am "second"
echo three >> file.txt
git commit -am "third"
git log --onelineEach recipe below starts from this state. Re-create the repository, or use the recovery recipe at the end, to get back to it.
Recipes at a glance
| Goal | Command | Result |
|---|---|---|
| Undo a commit, keep changes staged | `git reset --soft HEAD~1` | Changes stay in the index |
| Undo a commit, keep changes unstaged | `git reset HEAD~1` | Changes stay in the working directory |
| Undo a commit, discard changes | `git reset --hard HEAD~1` | Changes are removed from tracked files |
| Unstage a file | `git reset -- file.txt` | File content is untouched |
| Combine local commits | `git reset --soft HEAD~3` then commit | One commit holds all changes |
Undo a commit and keep the changes staged
Use this when the commit message was wrong, or you want to add one more file before committing again.
git reset --soft HEAD~1
git statusThe branch moves back one commit. The index and working directory stay as they were, so the changes from the removed commit appear as staged. You can now commit again with a better message.
Undo a commit and keep the changes unstaged
Use this when you want to re-split the work into several smaller commits.
git reset HEAD~1
git statusThis is the default --mixed mode. The branch moves and the index is reset, so the changes appear as unstaged modifications. Stage them in pieces with git add or git add -p.
Undo a commit and throw the work away
Use this only when you are sure you do not want the changes.
git reset --hard HEAD~1
git statusThe branch, the index, and the tracked files all return to the older commit. Uncommitted changes to tracked files are lost too.
Unstage a file
You ran git add on a file you did not mean to include in the next commit.
git add file.txt
git reset -- file.txt
git statusResetting a path copies that file's entry from HEAD into the index. The file is unstaged, and its contents in the working directory do not change. The -- separates the path from any revision name, which avoids confusion when a file and a branch share a name.
Combine the last three commits into one
Use this to tidy local work before you share it.
git reset --soft HEAD~3
git commit -m "Combine the last three commits"The soft reset moves the branch back three commits and leaves their combined changes staged. A single new commit then captures all of them. Only do this for commits you have not pushed. Rewriting pushed commits forces teammates to repair their copies.
Go back after a mistake
If a reset removed commits you wanted, the reflog lists where HEAD has recently pointed.
git reflog
git reset --hard HEAD@{1}HEAD@{1} is the position HEAD held before its latest move. Check the reflog output before running the second command, because the entry you want may be further back.
Right after a reset, there is a shorter route. Git saves the previous HEAD in ORIG_HEAD.
git reset --hard ORIG_HEADOther commands, such as merge and rebase, also set ORIG_HEAD. Use it immediately after the reset you want to undo.
Choosing a recipe
Common errors
- Running
git reset --hardwith uncommitted work you wanted. Commit or stash first. - Squashing commits that are already pushed. This rewrites shared history.
- Counting wrongly with
HEAD~n. Rungit log --onelinefirst and confirm which commit becomes the new tip. - Expecting a reset to remove untracked files. Reset does not touch them.
Exercise
You made three local commits and have not pushed them. You want one commit with the same final content. Which commands do you run, and why is a soft reset the right mode?
Show answer
Run git reset --soft HEAD~3, then git commit -m "your message". The soft reset moves the branch back three commits but leaves the index and working directory unchanged, so all three commits' changes stay staged and one new commit captures them.
Exercise
You staged notes.txt by mistake and want to keep its edits but leave it out of the next commit. Which command do you use, and what happens to the file's contents?
Show answer
Run git reset -- notes.txt. It copies the file's entry from HEAD into the index, which unstages it. The contents in the working directory are unchanged.