Sub topic 5
--soft: move the branch only
This chapter assumes you can run git init, git add, git commit, git status, and git log. It also assumes you know that a branch is a movable pointer to a commit, and that you know the three areas Git tracks: the HEAD commit, the index (staging area), and the working directory.
What --soft does
git reset --soft <commit> does one thing. It moves the current branch, and HEAD with it, to the commit you name. It leaves the index and the working directory alone.
The commits you move past are not deleted right away. They are just no longer reachable from the branch. Because the index and working files stay as they were, the changes from those commits now look like staged changes waiting to be committed.
A worked example
Start with a small repository that 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 --onelineThe log shows three commits, with "third" at the top. Now move the branch back one commit.
git reset --soft HEAD~1
git statusHEAD~1 means "the commit one step before HEAD". After the reset, git log --oneline shows only "first" and "second". The git status output lists file.txt under "Changes to be committed". The line three is still in the file, and it is already staged.
This happens because the index still matches the old "third" commit. Git compares the index with the new HEAD ("second") and reports the difference as a staged change.
Reading the result
| Area | Before reset | After `--soft HEAD~1` |
|---|---|---|
| Branch and HEAD | at "third" | at "second" |
| Index | matches "third" | unchanged, matches "third" |
| Working directory | matches "third" | unchanged |
| `git status` | clean | change staged |
Only the first row changes. The other two rows are the point of --soft: nothing you have edited or staged is touched, so the command cannot lose file contents.
When to use it
--soft fits when the commit contents are right but the commit itself is wrong. Typical cases:
- The commit message is poor and you want to write it again.
- You committed too early and want to add more changes before committing.
- You want to combine several local commits into one.
For the last case, move back three commits and commit once.
git reset --soft HEAD~3
git commit -m "Combine the last three commits"The soft reset keeps the combined changes of all three commits staged, so a single new commit captures them.
Common mistakes
- Expecting
--softto unstage files. It does the opposite. It leaves everything staged, so use the default--mixedmode to unstage. - Using a count that is too large.
HEAD~3moves back three commits, not to the third commit in the log. - Running it with a detached HEAD. The reset then moves HEAD alone, because no branch is checked out.
Exercise
A branch has commits A, B, C, and D, with D at the tip. You run git reset --soft HEAD~2 and then git status. Which commit does the branch point at, and where do the changes from C and D appear?
Show answer
The branch points at B. The changes from C and D are in the index, so git status lists them under "Changes to be committed". The working directory is unchanged.
Exercise
You made three small commits on a local branch and want one commit instead. Which two commands do that, and why does the first one keep your work?
Show answer
git reset --soft HEAD~3 followed by git commit -m "message". The soft reset moves only the branch, so the index still holds the combined changes of all three commits, and the new commit records them together.