Moving a Branch Back
A branch name such as main is a label on one commit, the newest one on that
branch. git reset <commit> moves the label of the branch you are on to a
different commit, usually an earlier one. The commits after it disappear from
git log, and their changes go wherever the reset mode says.
Reset rewrites your branch's history, so the same rule as amend applies: use it only on commits you have not shared. For shared commits, use revert.
The Three Modes

In the diagram, git reset <mode> HEAD~1 moves main from C back to B. Every
mode moves the branch. They differ in what happens to C's changes:
--softtouches nothing else. C's changes are still in your files and still staged, ready to commit again. Use it to recommit work differently, for example to combine several commits into one.--mixed, the default when you give no mode, also makes the staging area match B. C's changes are still in your files, but unstaged. Use it to undo commits while keeping the edits, so you can stage them again in a different way.--hardmakes the staging area and your files match B. C's changes are gone from your folder, along with any uncommitted edits to tracked files. Untracked files are normally left alone.
git reset --soft HEAD~1 # move the branch; changes stay staged
git reset HEAD~1 # same as --mixed; changes stay, unstaged
git reset --hard HEAD~1 # move the branch; files match the commit
Why --hard Needs Care
Commits you reset away are not destroyed at once. Git keeps them for a while,
and the recovery module shows how to find them. Uncommitted edits are
different: --hard overwrites them, and because Git never stored them, nothing
brings them back. Before any --hard reset, run git status and make sure
nothing you want is uncommitted.
If you reset by mistake, Git has saved the previous tip of the branch under
the name ORIG_HEAD. Repeat the reset with the same mode and ORIG_HEAD in
place of the commit, for example git reset --soft ORIG_HEAD after a soft
reset, and the branch is back where it was. This works only until another
command replaces ORIG_HEAD, such as a second reset.
Reset With a File Name
git reset <file>, with a file name and no commit, does something smaller:
it unstages that file, exactly like git restore --staged <file>. You may see
it in older guides. It never moves the branch.
Choosing an Undo Command
| Situation | Safest command | Changes commits? |
|---|---|---|
| Uncommitted edit you do not want | git restore <file> | No |
| Staged a file by mistake | git restore --staged <file> | No |
| Last commit, not shared, needs a fix | git commit --amend | Replaces the last one |
| Recent commits, not shared, should be redone | git reset --soft or git reset | Removes them from the branch |
| Recent commits, not shared, are unwanted entirely | git reset --hard | Removes them; files match too |
| A commit others already have | git revert | Adds one |
Start from the top: the earlier rows touch less, and the first two never change
a commit. Only reach for --hard when you are sure nothing uncommitted
matters.
Exercise: Apply Each Reset Mode
You will make three commits, then use each mode in turn. Before each reset,
predict what git log, git status and the file will show.
Starting state: Git configured as in the configuration lesson.
-
Run the setup. It makes a first commit and three more, one per step:
mkdir -p ~/git-practice/reset-modes cd ~/git-practice/reset-modes git init -b main cat > checklist.md <<'EOF' # Launch checklist EOF git add checklist.md git commit -m "Add checklist" cat >> checklist.md <<'EOF' - Step one EOF git commit -am "Add step one" cat >> checklist.md <<'EOF' - Step two EOF git commit -am "Add step two" cat >> checklist.md <<'EOF' - Step three EOF git commit -am "Add step three" git log --onelineThe log shows four commits.
-
--soft: combine the last two commits into one. Predict, then run:git reset --soft HEAD~2 git log --oneline git statusThe newest commit is now "Add step one".
git statusshowschecklist.mdunder "Changes to be committed": the two steps are still in the file and still staged. Commit them as one:git commit -m "Add steps two and three" git log --onelineThree commits now, and the newest says
1 file changed, 2 insertions(+). -
--mixed: undo a commit but keep its edits, unstaged. Predict, then run:git reset HEAD~1 git log --oneline git status git diffGit prints
Unstaged changes after reset:andM checklist.md. The newest commit is "Add step one" again.git statuslists the file under "Changes not staged for commit", andgit diffshows the step two and step three lines as added. -
--hard: throw everything away. First add a note you never commit, then predict and run:cat >> checklist.md <<'EOF' - Remember to buy batteries EOF git status git reset --hard HEAD~1 git log --oneline git status cat checklist.mdGit prints
HEAD is now atwith the first commit's ID and "Add checklist". Only that commit is left,git statusreports a clean working tree, and the file holds just the heading. The step lines came from commits Git still keeps for a while; the batteries note was never committed, so it is gone for good.
Success: each prediction matched, or you can say why not: --soft kept the
changes staged, --mixed kept them unstaged, and --hard removed them and
the uncommitted note.
If something differs:
| What you see | Likely cause | Next step |
|---|---|---|
unknown revision or path not in the working tree | Fewer commits than HEAD~n needs | git log --oneline; count the commits |
Changes are unstaged after a --soft reset | The mode was left out, so --mixed ran | git add the file, then commit |
| The reset went too far back | Wrong number in HEAD~n | Repeat the reset with the same mode and ORIG_HEAD, before running another reset |
An edit you needed is gone after --hard | It was never committed | Check your editor's undo history; next time run git status first |
-
Clean up:
cd ~/git-practice ls rm -rf reset-modes ls
Independent task: before cleaning up, handle four mistakes. For each one, run the setup lines, then choose and apply the safest command from the table above.
-
A commit that others already have added a file by mistake. Undo it:
cat > notes.md <<'EOF' Bring a torch. EOF git add notes.md git commit -m "Add notes" -
Two commits nobody else has should have been one:
cat >> checklist.md <<'EOF' - Step four EOF git commit -am "Add step four" cat >> checklist.md <<'EOF' - Step five EOF git commit -am "Add step five" -
The last commit, not shared, has a typo in its message:
cat >> checklist.md <<'EOF' - Step six EOF git commit -am "Add stpe six" -
You overwrote the file by accident and want it back as committed:
cat > checklist.md <<'EOF' oops EOF
Check Your Understanding
Pick the safest command for each situation, and say why.
- You staged
debug.logby mistake. Nothing is committed yet. - Your last three commits are experiments only you have, and you want them
gone.
git statusis clean. - A commit from last week, which the whole team has, introduced a bug.
- Your last commit, not yet shared, forgot to include a file.
Answer and Reasoning
Independent task
-
git revert --no-edit HEAD. The commit is shared, so it must stay in the history; the revert removesnotes.mdin a new commit. -
Combine them with a soft reset, then commit once:
git reset --soft HEAD~2 git commit -m "Add steps four and five" -
git commit --amend -m "Add step six", since it is the newest commit and not shared. -
git diffto confirm the damage, thengit restore checklist.md. No commit is involved, so a restore is enough.
Afterwards, git log --oneline lists, newest first: "Add step six", "Add steps
four and five", Revert "Add notes", "Add notes" and "Add checklist".
Understanding check
git restore --staged debug.log. It only unstages; no commit changes and nothing is lost.git reset --hard HEAD~3. The commits are unshared and nothing uncommitted is at risk, so moving the branch back and matching the files is safe. If you were unsure,git reset HEAD~3would keep the edits as unstaged changes instead.git revert <commit>. The commit is shared, so the fix must be a new commit rather than a rewrite.- Stage the file, then
git commit --amend --no-edit. It is the newest commit and nobody else has it.