Reset A Branch
Part of the Version Control section of Coddy's Terminal journey — lesson 44 of 58.
git reset moves the current branch pointer to a different commit. The most common use is to undo recent commits you have not shared yet.
git reset --hard HEAD~1This moves main back one commit and overwrites the working directory to match. The commit you skipped is no longer reachable from your branch (Git holds onto it for a while in case you change your mind, but expect it to disappear).
Three flavors of reset exist:
--soft: move the branch pointer; keep staged changes--mixed(default): move the pointer; unstage but keep working files--hard: move the pointer; throw away working file changes too
--hard is the most destructive form. It is great for cleaning up local mistakes, but never use it on commits other people have already pulled.
Challenge
MediumThe folder contains doc.md. Initialize the repo and create three commits in order:
- Add
doc.mdwith messageAdd doc. - Append
secondtodoc.md, commit with messageSecond draft. - Append
thirdtodoc.md, commit with messageThird draft.
Hard-reset the branch back to the very first commit using HEAD~2. Print the contents of doc.md.
Cheat sheet
git reset moves the current branch pointer to a different commit. Three flavors:
--soft: move pointer; keep staged changes--mixed(default): move pointer; unstage but keep working files--hard: move pointer; discard working file changes
git reset --hard HEAD~1 # undo last commit, discard changes
git reset --hard HEAD~2 # undo last 2 commitsWarning: Never use --hard on commits others have already pulled.
Try it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Version Control
2Getting Started
Initialize A RepositoryThe .git FolderConfigure Your IdentityGit StatusRecap - First Repo8Merging
What Is A MergeFast-Forward MergeThree-Way MergeMerge ConflictsResolve A ConflictRecap - Merge Master11Feature Branch Project
Project OverviewInitialize Main3Tracking Changes
The Staging AreaGit AddGit CommitModifying A Tracked FileGit LogRecap - First Commits6Recipe Site Project
Project OverviewInitialize And Ignore9Undoing Changes
Discard Unstaged ChangesUnstage A FileAmend The Last CommitRevert A CommitReset A BranchRecap - Time Machine