Page 2 of 2
reset and revert both undo work, but they work completely differently, and picking wrong matters.
git reset --soft HEAD~1 # undo the last commit, keep changes staged
git reset --mixed HEAD~1 # undo the last commit, keep changes unstaged (default mode)
git reset --hard HEAD~1 # undo the last commit, DISCARD the changes entirely
reset moves your branch pointer backward and rewrites history - fine for commits you haven't pushed. --hard is the one to be careful with: it throws away work with no confirmation.
git revert a3f9c2d # create a NEW commit that undoes commit a3f9c2d
revert doesn't rewrite history at all - it adds a new commit that cancels out an old one. That makes it the correct choice for undoing something that's already been pushed and shared, since it doesn't disturb commits other people may have already pulled.
As a decision:
Two tools for figuring out why code looks the way it does.
git blame file.py # shows which commit last touched each line, and who
blame is often the fastest way to find who to ask about a confusing piece of code, or which commit introduced it.
git bisect start
git bisect bad # current commit has the bug
git bisect good v1.2.0 # this earlier tag was fine
# Git checks out a commit halfway between - you test it and report:
git bisect good # or
git bisect bad
# repeat - Git binary-searches down to the exact commit that introduced the bug
bisect turns "somewhere in these 400 commits a bug appeared" into a handful of test-and-answer steps, which is dramatically faster than checking commits one at a time.
Local branches can be linked to a remote branch so push/pull know where to go without you specifying it every time.
git push -u origin feature-x # -u sets up tracking, future push/pull need no args
git branch -vv # shows each local branch and what it tracks
git fetch downloads remote changes without merging them into your working branch - it's the safe, look-before-you-leap counterpart to git pull, which is really just fetch followed immediately by merge.
git fetch origin
git log main..origin/main # see exactly what's new on the remote before merging it
A tag is a permanent, named pointer to a specific commit, typically used to mark releases (v1.0.0).
git tag v1.0.0 # lightweight tag on the current commit
git tag -a v1.0.0 -m "First release" # annotated tag, stores message/author/date
git push origin v1.0.0 # tags aren't pushed automatically - explicit step
Unlike branches, tags aren't meant to move - once v1.0.0 points at a commit, it should stay there permanently, so anyone can check out exactly what was released.
Real scenario-based DevOps questions, hands-on practice, and clear explanations for every answer.