Git & Terminal Cheatsheet

Undoing Changes

Use this Git & Terminal reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

Before You Commit

git restore file.js             # discard unstaged edits to a file
git restore .                   # discard ALL unstaged edits (careful)
git restore --staged file.js    # unstage, keep the edits
git restore --source HEAD~2 file.js   # bring back an old version of a file
git clean -nd                   # preview untracked files/dirs to delete
git clean -fd                   # actually delete them

git restore (Git 2.23+) replaced the confusing git checkout -- file form for this job.

Fixing the Last Commit

git commit --amend -m "Better message"   # reword the latest commit
git add forgotten.js
git commit --amend --no-edit             # add a file to it, keep the message
git commit --amend --author="Ada <ada@example.com>"   # fix the author

Amending REPLACES the commit (new hash). If the old one was already pushed, the next push needs --force-with-lease.

reset vs revert

git resetgit revert
What it doesmoves the branch pointer back, erasing commits from the branchadds a NEW commit that undoes an old one
Historyrewrittenpreserved
Safe on pushed/shared branchesno (forces a force-push, breaks teammates' pulls)yes, this is the tool for shared history
git reset --soft HEAD~1     # undo last commit, keep changes STAGED
git reset HEAD~1            # (mixed) undo last commit, keep changes unstaged
git reset --hard HEAD~1     # undo last commit AND delete the changes
git reset --hard origin/main    # make local exactly match the remote

git revert abc1234          # undo one pushed commit safely
git revert HEAD~3..HEAD     # revert the last 3 commits
git revert -n abc1234       # stage the undo without committing yet
git revert -m 1 abc1234     # revert a MERGE commit (keep parent #1's side)

Rule of thumb: reset for commits only you have, revert for anything already on the remote.

Lost a Commit? reflog

git reflog records every position HEAD has had, even across resets, rebases, and deleted branches. It is the canonical "I lost my work" recovery:

git reflog                      # HEAD@{0} is now, HEAD@{1} one move ago...
git reflog show feature         # movements of one branch
git reset --hard HEAD@{1}       # jump the branch back one move
git branch rescue abc1234       # or pin a found commit to a new branch
git switch rescue

Typical rescue: you ran git reset --hard on the wrong thing. git reflog, find the entry before the reset, git reset --hard HEAD@{1}. Reflog entries expire (default 90 days, 30 for unreachable commits), so they are a safety net, not a backup.

Undoing a Merge, Rebase, or Pull

git merge --abort           # mid-conflict: cancel the merge
git rebase --abort          # mid-conflict: cancel the rebase
git reset --hard ORIG_HEAD  # just finished a merge/rebase/pull? go back to before it
git revert -m 1 <merge-hash>    # undo an already-PUSHED merge commit

ORIG_HEAD is set automatically before dangerous moves (merge, rebase, reset).

Quick Reference

GoalCommand
Discard file editsgit restore <file>
Unstagegit restore --staged <file>
Delete untracked filesgit clean -fd (preview with -nd)
Reword last commitgit commit --amend
Undo local commit, keep workgit reset --soft HEAD~1
Undo local commit, drop workgit reset --hard HEAD~1
Undo a PUSHED commitgit revert <hash>
Recover after bad resetgit reflog then git reset --hard HEAD@{1}
Undo a just-finished mergegit reset --hard ORIG_HEAD