Git Foundations / Undo Changes Safely

Move a branch with reset

Lesson 13 of 33. Open to read. No account needed.

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

Commits A, B and C, with main moving from C back to B after git reset HEAD~1. A table compares the three modes. --soft moves the branch and keeps the staging area and working tree. --mixed, the default, moves the branch and resets the staging area to match B, keeping the working tree. --hard moves the branch and resets both the staging area and the working tree to match B, discarding uncommitted edits.

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:

  • --soft touches 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.
  • --hard makes 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

SituationSafest commandChanges commits?
Uncommitted edit you do not wantgit restore <file>No
Staged a file by mistakegit restore --staged <file>No
Last commit, not shared, needs a fixgit commit --amendReplaces the last one
Recent commits, not shared, should be redonegit reset --soft or git resetRemoves them from the branch
Recent commits, not shared, are unwanted entirelygit reset --hardRemoves them; files match too
A commit others already havegit revertAdds 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.

  1. 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 --oneline
    

    The log shows four commits.

  2. --soft: combine the last two commits into one. Predict, then run:

    git reset --soft HEAD~2
    git log --oneline
    git status
    

    The newest commit is now "Add step one". git status shows checklist.md under "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 --oneline
    

    Three commits now, and the newest says 1 file changed, 2 insertions(+).

  3. --mixed: undo a commit but keep its edits, unstaged. Predict, then run:

    git reset HEAD~1
    git log --oneline
    git status
    git diff
    

    Git prints Unstaged changes after reset: and M checklist.md. The newest commit is "Add step one" again. git status lists the file under "Changes not staged for commit", and git diff shows the step two and step three lines as added.

  4. --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.md
    

    Git prints HEAD is now at with the first commit's ID and "Add checklist". Only that commit is left, git status reports 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 seeLikely causeNext step
unknown revision or path not in the working treeFewer commits than HEAD~n needsgit log --oneline; count the commits
Changes are unstaged after a --soft resetThe mode was left out, so --mixed rangit add the file, then commit
The reset went too far backWrong number in HEAD~nRepeat the reset with the same mode and ORIG_HEAD, before running another reset
An edit you needed is gone after --hardIt was never committedCheck your editor's undo history; next time run git status first
  1. 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.

  1. 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"
    
  2. 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"
    
  3. The last commit, not shared, has a typo in its message:

    cat >> checklist.md <<'EOF'
    - Step six
    EOF
    git commit -am "Add stpe six"
    
  4. 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.

  1. You staged debug.log by mistake. Nothing is committed yet.
  2. Your last three commits are experiments only you have, and you want them gone. git status is clean.
  3. A commit from last week, which the whole team has, introduced a bug.
  4. Your last commit, not yet shared, forgot to include a file.

Answer and Reasoning

Independent task

  1. git revert --no-edit HEAD. The commit is shared, so it must stay in the history; the revert removes notes.md in a new commit.

  2. Combine them with a soft reset, then commit once:

    git reset --soft HEAD~2
    git commit -m "Add steps four and five"
    
  3. git commit --amend -m "Add step six", since it is the newest commit and not shared.

  4. git diff to confirm the damage, then git 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

  1. git restore --staged debug.log. It only unstages; no commit changes and nothing is lost.
  2. 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~3 would keep the edits as unstaged changes instead.
  3. git revert <commit>. The commit is shared, so the fix must be a new commit rather than a rewrite.
  4. Stage the file, then git commit --amend --no-edit. It is the newest commit and nobody else has it.

References

Study this course with an account

Course access adds revision notes, the Quick Reference, saved progress, bookmarks and your own notes.

INR 99 plus 18% GST. 90 days of online access. No auto-renewal.

See course access

This website is operated by VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED, operating as Vectametric.

Corporate Identity Number (CIN): U85499KA2026OPC229822. GSTIN: 29AAMCV8281D1ZS. Registered office: RS NO/38/2A/1 BASTAND, ROAD MAHANTESH NAGAR, Ramdurg, Ramdurg, Belgaum- 591123, Karnataka.

Queries and grievances: Prajval Honnungar, Grievance Officer and Data Protection Contact.

Telephone: +91 94800 09310. Support: support@vectametric.com. Grievances: support@vectametric.com.

Copyright 2026 VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED. All rights reserved.

Vectametric home · Roadmaps · Vecta IT Exams · Vecta Government Exams · Vecta Banking & Finance Exams · Vecta Entrance Exams · About us · Contact · Privacy Policy · Terms and Conditions · No-Refund Policy