Sub topic 5
--soft: move the branch only
This chapter assumes you can use git init, add, commit, status, and log, and that you know what a branch is. It also assumes you know the three areas Git tracks: the commit that HEAD points to, the index (staging area), and the working directory. git reset --soft is the mildest of the three reset modes. It changes only the first of those areas.
What --soft does
git reset --soft <commit> does one thing: it moves the current branch so that it points at <commit>. It does not touch the index. It does not touch the working directory.
Because only the branch pointer moves, the commits you "removed" are not destroyed. Their changes are still in the index, so Git shows them as staged and ready to commit again.
| Area | Changed by `--soft`? |
|---|---|
| Branch pointer (and so `HEAD`'s commit) | Yes, moved to the target |
| Index (staging area) | No |
| Working directory | No |
Reset is described elsewhere as "move the branch, then optionally update the index and working directory". --soft stops after the first step.
Set up a practice repository
Create a repository with three commits. If Git complains about your identity, set user.name and user.email first.
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 lists three commits, with "third" on top. file.txt contains the lines one, two, and three.
Run a soft reset
Move the branch back one commit:
git reset --soft HEAD~1
git statusThe status output looks like this:
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: file.txtCheck each area to see why:
- Branch:
git log --onelinenow shows only "first" and "second". The "third" commit is gone from the branch history. - Working directory:
cat file.txtstill printsone,two,three. Nothing was rewritten on disk. - Index:
git diff --stagedshows+three. The index still holds the file as it was in the removed commit. Compared with the newHEAD, that is a staged change.
The staged change is the same change the "third" commit contained. Running git commit -m "third again" creates a new commit with the same file contents as the old one. It has a different hash, because its timestamp and possibly its message differ.
HEAD versus the branch
HEAD normally holds the name of a branch, not a commit hash. When you run git reset --soft HEAD~1 on main, Git does not change what HEAD says. HEAD still says "the branch main". Git moves main itself to the older commit.
You can check this:
cat .git/HEAD
git rev-parse main
git rev-parse HEAD.git/HEAD contains ref: refs/heads/main. The two rev-parse commands print the same hash before and after a reset, and that hash changes when you reset.
If you are in detached HEAD state, there is no branch to move. In that case reset moves HEAD directly. Commits left behind are not on any branch, so be careful to note their hashes first, or rely on the reflog (see the recovery chapter).
The target can be any commit
--soft accepts the same targets as other reset modes: HEAD~n, a hash, a branch name, or a tag. The index is never recalculated, so the staged diff is always the difference between the new HEAD and the old index.
git reset --soft HEAD~3moves back three commits. Everything those commits added is now staged together.git reset --soft a1b2c3dmoves to that commit, if it exists in your repository.git reset --soft HEADmoves to where you already are. Nothing happens, because the branch already points there.
The target does not have to be an ancestor of the current commit. If you run git reset --soft other-branch, the current branch jumps to the tip of other-branch. The index still holds your old state. The staged diff then shows how to turn other-branch back into what you had, which is rarely what you want. Use it deliberately.
Staged and unstaged changes are not separated
A soft reset leaves the index alone, and that includes anything you had already staged. Suppose you have staged one extra edit before running the reset. After git reset --soft HEAD~1, the staged changes from the removed commit and your extra staged edit are mixed in the index. Git does not record which part came from where.
Unstaged edits stay unstaged. The reset does not touch them.
echo four >> file.txt
git reset --soft HEAD~1
git statusHere four is an uncommitted edit in the working directory. After the reset, git status reports file.txt under both "Changes to be committed" (the removed commit's change) and "Changes not staged for commit" (the four line). If you want the removed commit's changes staged and nothing else, make sure your working directory and index are clean before you reset.
A typical use: redo a commit
One common reason to use --soft is to redo the last commit with a different set of changes or a different message.
git reset --soft HEAD~1
# adjust the index if needed, for example:
git restore --staged unwanted.txt
git commit -m "A better message"This is close to git commit --amend. Amend replaces the last commit with a new one, and a soft reset followed by a commit does the same thing in two steps. The two-step form works for more than one commit, and it lets you inspect the staged result before you commit.
To keep the old message as a starting point, Git saves the previous HEAD in ORIG_HEAD, so you can reuse it:
git reset --soft HEAD~1
git commit -c ORIG_HEAD-c ORIG_HEAD opens your editor with the old commit's message and authorship information. (-C reuses it without editing.)
Combining several commits into one uses the same idea with a larger step. Reset back by the number of commits to combine, then commit once:
git reset --soft HEAD~3
git commit -m "Combine the last three commits"The reset keeps the combined changes staged, so one commit captures all of them. Only do this for commits you have not shared. The safety chapter explains why.
What happens to the removed commit
The old commit is not deleted when the branch moves. Git keeps it in its object database until garbage collection removes unreferenced objects, which by default happens only after the reflog entries expire (90 days for reachable entries and 30 days for unreachable ones, by default). You can reach it two ways:
ORIG_HEADholds theHEADposition from before the last reset.git log ORIG_HEADshows the removed commits.git refloglists recent positions ofHEAD, including the reset itself, which appears as an entry likereset: moving to HEAD~1.
To undo the soft reset, move the branch back:
git reset --soft ORIG_HEAD
git statusBecause a soft reset left the index matching the old tip, moving back means index and HEAD match again and git status reports a clean state. In this specific case --soft is the safer way to move back than --hard, because it cannot discard anything.
ORIG_HEAD is overwritten by the next operation that sets it, such as another reset, a merge, or a rebase. If you do several things, use the reflog instead.
Edge cases and errors
- Paths are not allowed.
git reset --soft -- file.txtfails withfatal: Cannot do soft reset with paths.A soft reset moves a branch, and a path reset only edits the index. For paths, use plaingit reset -- <path>. - Merges in progress. During an unfinished merge,
git reset --softfails withfatal: Cannot do a soft reset in the middle of a merge.Finish the merge withgit commit, or cancel it withgit merge --abort, then reset. - Unborn branch. In a repository with no commits, there is nothing to move back to.
git reset --soft HEAD~1fails becauseHEAD~1does not exist. - First commit.
HEAD~1does not exist when you are on the root commit, so you cannot soft-reset past it this way. - Untracked files are never affected. Reset only deals with tracked content in the index.
Summary of the model
Use this checklist to predict a --soft result before you run it:
- The branch moves to the target commit.
- The index is unchanged, so it still describes the old tip plus anything you staged.
- The working directory is unchanged.
git statusshows the difference between the newHEADand the index as staged changes.- The old tip is saved in
ORIG_HEADand in the reflog.
The next chapter covers --mixed, which does everything --soft does and then also resets the index to match the new HEAD.
Exercise
You have three local commits on main: A, B, and C (C is the tip). You run git reset --soft A. Where does main point afterward, what does git status show for the changes made in B and C, and what happens to the files in your working directory?
Show answer
main points to A. The changes from B and C appear as staged changes ("Changes to be committed"), because the index still matches C and now differs from HEAD. The working directory files do not change. You could commit them as one new commit, or run git reset --soft ORIG_HEAD to move main back to C.
Exercise
After a soft reset you notice git commit would also include a file you staged by accident before the reset. Without losing the other staged changes, how do you remove just that file from the next commit, and why can git reset --soft not do it for you?
Show answer
Run git restore --staged <file> (or git reset -- <file>) to unstage only that file. git reset --soft cannot take a path (it fails with "Cannot do soft reset with paths"), because it only moves the branch and never edits the index.