Lesson 6 of 8 in Git and collaboration
Undoing mistakes safely
Undoing in git means one of three things: discarding edits that are not committed, moving a branch back to an earlier commit, or adding a new commit that cancels an old one. Which one is safe depends on whether the work was committed and whether anyone else has it.
Draft. This lesson is written and waiting for a person to run every command and review it. It is safe to learn from, but check the official documentation for anything you depend on.
Read these first: Remotes, fetch, push and pull requests
The problem this solves
Everyone commits to the wrong branch, stages a password, or deletes a file they wanted. Git offers many ways to undo, and tutorials list them as a flat set of commands. People then run a destructive one at the wrong moment.
Two questions sort the options. First, was the work ever committed? Uncommitted edits that you discard are gone for good. Committed work is very hard to lose. Second, has anyone else received the commits? Rewriting history that others already have causes trouble for the whole team.
This is also where git's safety net is. Because commits are never edited, a commit you moved away from usually still exists and can be found again for a while.
How it works
- Control
- Runs code
- Data
Restore discards uncommitted edits, reset moves a local branch back, and revert adds a new commit that cancels a shared one.
- git restore to Uncommitted edits: overwrites files
- git reset to Local commits: moves the branch
- git revert to Pushed commits: adds an undo commit
- Data
- Control
- Other
After a hard reset moves main back to commit A, commits B and C still exist and the reflog still points at C, so a second reset can bring main back.
- Commit B to Commit A: parent
- Commit C to Commit B: parent
- main to Commit A: 1 now points here
- reflog to Commit C: 2 still remembers
git restore <file> overwrites a file in your working tree with the staged or committed version. Anything you had not committed or staged is gone, and git cannot bring it back. git restore --staged <file> is gentler: it only unstages the file and leaves your edits alone.
git reset moves the current branch label to another commit. With --hard it also makes the files match, which discards uncommitted edits. Use it on commits you have not pushed. git revert <commit> leaves history alone and adds a new commit with the opposite changes. Because nothing is rewritten, it is the right undo for commits that others already have.
The reflog is a private journal of where HEAD and your branches pointed, kept in your local repository. Entries expire after a while: by default 90 days for commits still reachable and 30 days for unreachable ones. In the second diagram, main moved back to A, yet C still exists and the reflog names it, so you can move main back.
Try it yourself
Practice in a throwaway repository with three commits. Never run git reset --hard in a repository holding work you care about unless it is committed.
- 1Set up three commits
mkdir git-lab-5 && cd git-lab-5 git init -b main git config user.name "Your Name" git config user.email "you@example.com" echo "one" > a.txt && git add a.txt && git commit -m "One" echo "two" >> a.txt && git commit -am "Two" echo "three" >> a.txt && git commit -am "Three" git log --oneline
- 2Discard an uncommitted edit
echo "oops" >> a.txt cat a.txt git restore a.txt cat a.txt
The second
catno longer shows the oops line. This is the destructive undo: nothing remembers that line. - 3Unstage without losing the edit
echo "keep me" >> a.txt git add a.txt git restore --staged a.txt git status -s
You should see
M a.txt
The edit is back to unstaged and still in the file. Finish by running
git restore a.txtto drop it. - 4Move the branch back, then recover it
git reset --hard HEAD~1 git log --oneline git reflog git reset --hard 'HEAD@{1}' git log --onelineThe first reset drops commit Three from the branch.
git refloglists the move, andHEAD@{1}means where HEAD was one move ago. The second reset puts the branch back. The quotes stop some shells from reading the braces. - 5Undo a commit without rewriting history
git revert HEAD --no-edit git log --oneline cat a.txt
A new commit appears on top. The old one is still in the history, but its change is cancelled. This is what you do for commits that are already pushed.
What breaks, and where to look
After
git reset --hardorgit restore ., your uncommitted edits are gone andgit reflogdoes not show them.- WHY IT HAPPENS
- The reflog records where branches and HEAD pointed. It does not record edits that were never committed.
- CHECK FIRST
- Run
git refloganyway to see whether the edits were ever committed. If you had rungit addon them,git fsck --lost-foundmay find the staged content as dangling objects. Otherwise check your editor's local history.
! [rejected] main -> main (non-fast-forward) after you reset or amended a commit that was already pushed.
- WHY IT HAPPENS
- You rewrote history that exists on the remote. Your branch no longer contains the commits the remote has, so the push looks like it would erase them.
- CHECK FIRST
- Run
git log origin/main..mainandgit log main..origin/main. If teammates may have the old commits, undo withgit revertinstead of forcing. If forcing is truly needed on your own branch, usegit push --force-with-lease, which stops if someone else pushed in the meantime.
error: could not revert <short-id>... <commit message>
- WHY IT HAPPENS
- Later commits changed the same lines, so reversing the old commit conflicts with current content.
- CHECK FIRST
- Run
git status, resolve the conflict as in the previous concept,git addthe file, then rungit revert --continue. To cancel, rungit revert --abort.
When not to use this
- Do not rewrite commits that others have pulled. Prefer
git reverton shared branches such as main. - A reflog entry is local and eventually expires. Treat it as a short-term rescue, not as a backup. Push work you care about to a remote.
Check yourself
Which undo cannot be recovered if you change your mind?
Discarding uncommitted edits, for example with git restore <file> or git reset --hard. Git never saved those edits.
Why use revert instead of reset on a pushed commit?
Revert adds a new commit and rewrites nothing, so teammates who already have the old commit stay in sync.
How do you find a commit after resetting past it?
Run git reflog, find the entry where the branch pointed at it, and reset or switch back to that entry such as HEAD@{1}.
Keep going
Lesson 36 of 92 in the DevOps path