Git for Interviews Cheat Sheet: Merge, Rebase, Reset, Revert and Reflog
Understand commits, branches, staging and working-tree changes.
Git for interviews: what commits and branches are, merge against rebase, and which command undoes which mistake (an uncommitted edit, a local commit, a commit already pushed).
At a glance
- A commit is a snapshot of the project plus its parent commit(s), named by its hash. A branch is a movable pointer to a commit, and
HEADnames the branch you are on (or a commit: detached HEAD). - Changes live in three places: the working tree, the index (staging area) and the commits.
- Merge keeps the history and adds a merge commit (unless it can fast-forward). Rebase replays your commits on a new base, and each replayed commit gets a new hash.
- Never rebase or force-push commits other people have built on. Undo a shared commit with
git revert, which adds an inverse commit. reset --softkeeps the undone changes staged,--mixed(the default) keeps them unstaged,--harddiscards them.git refloglists whereHEADpointed recently (git reflog show <branch>for one branch), so a commit "lost" toreset --hardcan usually be found again.
Everyday commands
| Task | Command |
|---|---|
| Start a branch from the current commit | git switch -c feature/x |
| See unstaged / staged changes | git diff / git diff --staged |
| Stage part of a file | git add -p |
| Fix the last commit before pushing it | git commit --amend |
| Download remote changes without touching your branch | git fetch |
| Fetch and integrate | git pull --rebase or git pull --no-rebase (merge) |
| Put work in progress aside | git stash push -m "wip", later git stash pop (-u adds untracked files) |
| History as a graph | git log --oneline --graph --decorate --all |
| Who changed these lines | git blame -L 10,20 path |
| Find the commit that broke something | git bisect start, git bisect bad, git bisect good <sha> |
| Copy one commit onto the current branch | git cherry-pick <sha> |
| Tag a release | git tag -a v1.2.0 -m "v1.2.0", then git push origin v1.2.0 |
In current Git, a plain git pull with no pull.rebase or pull.ff setting only fast-forwards: if your branch has diverged, it stops and asks you to choose.
Merge or rebase?
git merge feature | git rebase main (on the feature branch) | |
|---|---|---|
| History | kept as it happened, plus a merge commit (unless fast-forward) | linear: your commits replayed on top of main |
| Commit hashes | unchanged | new for every replayed commit |
| Conflicts | resolved once, in the merge commit | possibly once per replayed commit |
| On a shared branch | safe | unsafe: others' work is based on the old commits |
| Typical use | integrating into main, often through a pull request | tidying a private branch; git rebase -i to squash or reword |
- Fast-forward: when your branch tip is an ancestor of the other tip, merge just moves the pointer.
--no-ffforces a merge commit. - A squash merge puts one new commit on
mainwith no link to the feature branch's commits.git merge --squashstages that change; you still rungit commit.
Undoing things
| Situation | Command | Rewrites history? |
|---|---|---|
| Discard unstaged edits to a file | git restore path | no |
| Unstage a file, keep the edits | git restore --staged path | no |
| Undo the last commit, keep its changes staged | git reset --soft HEAD~1 | yes, locally |
| Undo the last commit, keep its changes unstaged | git reset HEAD~1 (--mixed) | yes, locally |
| Undo the last commit and discard its changes | git reset --hard HEAD~1 | yes, and it destroys uncommitted work |
| Undo a commit that is already pushed or shared | git revert <sha> (a merge commit needs -m <parent>) | no: it adds an inverse commit |
| Get a "lost" commit back | find it in git reflog, then git switch -c rescue <sha> | no |
| Stop a merge or rebase in progress | git merge --abort / git rebase --abort | no |
| Push after rewriting your own branch | git push --force-with-lease | yes, on the remote |
--force-with-leaserefuses to overwrite the remote branch if it no longer matches your remote-tracking branch. A backgroundgit fetchcan update that expectation, so naming the branch you rewrite and its expected tip, for example--force-with-lease=feature/x:<expected-sha>, is the strict form. Plain--forcehas no such check.- The reflog is local and its entries expire (by default after 90 days, or 30 for commits no longer reachable from the branch). It records where refs pointed, not edits you never committed.
The undo commands, run
The script builds a throwaway repository and shows each command's effect. Run it with Git 2.28 or later and Bash; this is the real output of a run with Git 2.45.
#!/usr/bin/env bash
set -euo pipefail
cd "$(mktemp -d)" # a throwaway repository
git init -q -b main
git config user.email demo@example.com; git config user.name demo
log() { git log --format=%s | paste -sd ' ' -; }
for n in 1 2 3; do echo "$n" >> f.txt; git add f.txt; git commit -qm "c$n"; done
git reset -q --soft HEAD~1
echo "soft: staged=[$(git diff --cached --name-only)] log=$(log)"
git commit -qm c3
git reset -q --mixed HEAD~1
echo "mixed: staged=[$(git diff --cached --name-only)] unstaged=[$(git diff --name-only)] log=$(log)"
git commit -qam c3
git reset -q --hard HEAD~1
echo "hard: file=$(paste -sd ' ' f.txt) log=$(log)"
git reset -q --hard 'HEAD@{1}'
echo "reflog: log=$(log)"
git revert --no-edit HEAD > /dev/null
echo "revert: log=$(log)"
git switch -q -c feature HEAD~2
echo x > g.txt; git add g.txt; git commit -qm feat
git rebase -q main
echo "rebase: log=$(log)"soft: staged=[f.txt] log=c2 c1 mixed: staged=[] unstaged=[f.txt] log=c2 c1 hard: file=1 2 log=c2 c1 reflog: log=c3 c2 c1 revert: log=Revert "c3" c3 c2 c1 rebase: log=feat Revert "c3" c3 c2 c1
HEAD@{1}is whereHEADwas before the last move, here the commit thatreset --harddropped.revertkeptc3in the history and added a commit that undoes it.- The feature branch started at
c2; after the rebase,featis a new commit on top ofmain.
Workflows interviewers ask about
| Workflow | Shape | Fits |
|---|---|---|
| Trunk-based development | short-lived branches merged to main often; feature flags hide unfinished work | continuous delivery, frequent deploys |
| GitHub flow | branch, pull request, review and CI, merge to main, deploy | most web teams |
| Git flow | develop, release/*, hotfix/* and main | versioned products with scheduled releases; heavier |
A good pull request is small, has a commit message that says why, passes CI, and goes into a protected main with required reviews and checks.
In a .NET repository
- Start with
dotnet new gitignore: it ignoresbin/,obj/,.vs/and user files. - Keep secrets out of
appsettings.jsonandappsettings.*.json: usedotnet user-secretsin development and a secret store in production. If a secret was pushed, rotate it; rewriting history does not take back what was already copied. - Add a
.gitattributes(for example* text=auto) so Windows and Linux machines do not produce whole-file line-ending diffs.
Common mistakes
git reset --hardwith uncommitted work: staged and unstaged changes to tracked files are gone, and the reflog cannot bring back edits that were never committed. Commit or stash first.- Rebasing or force-pushing a shared branch: teammates' history diverges and work can be lost. Rebase only your own branches.
- Resetting a commit that is already on the remote
main:git revertit instead. - Long-lived feature branches: big, risky merges. Integrate small and often.
- Committing
bin/,obj/or secrets: ignore build output, and rotate any secret that was pushed. - Plain
--forceafter a rebase: use--force-with-lease, ideally with the expected tip, so a push made since your last fetch is not overwritten.
In an interview
What is a branch, physically? A named pointer to a commit. Committing on the branch moves the pointer to the new commit; nothing is copied.
Merge or rebase? Merge to integrate shared work: it keeps history and changes no hashes. Rebase to tidy your own branch before review; never rebase commits other people have built on.
You pushed a bad commit to main. What do you do? git revert it: that adds a new commit that undoes it, so nobody's history is rewritten. Resetting and force-pushing main breaks everyone who already pulled.
reset --soft, --mixed and --hard? All three move HEAD, and the current branch with it when one is checked out, to the given commit. Soft keeps the changes staged, mixed (the default) keeps them in the working tree but unstaged, hard discards them.
You ran reset --hard by mistake. How do you get the commits back? Find the old tip in git reflog, check it, and create a branch at it (git switch -c rescue <sha>) or reset back to it.
fetch or pull? fetch downloads commits and updates remote-tracking branches such as origin/main; your branch does not move. pull is a fetch followed by a merge or a rebase into your branch.
How do you find which of 200 commits introduced a bug? git bisect: mark one good and one bad commit and test the middle each time. Each test halves the range, so about 8 tests cover 200 commits.
Check yourself
A bad commit is already on the shared main branch. What is the safe way to undo it?
Show answer
Correct answer: B
revert adds a new commit that undoes the bad one, so nobody's history is rewritten.
Option A: Resetting and force-pushing rewrites history that others have already pulled.
Option C: Dropping a commit with an interactive rebase also rewrites shared history.
Option D: Recreating the branch rewrites history too, and loses everyone's newer commits.
After git reset --mixed HEAD~1, where are the last commit's changes?
Show answer
Correct answer: C
--mixed moves the branch and resets the index; the working tree keeps the changes.
Option A: That is --hard, which discards the changes.
Option B: That is --soft, which leaves the index as it was.
Option D: reset never creates a stash entry.
What does git push --force-with-lease check that --force does not?
Show answer
Correct answer: D
The push is refused if the remote branch no longer matches your remote-tracking branch. A background fetch can move that expectation, so the strict form also names the expected tip.
Option A: Signatures are a separate feature; the lease only compares branch positions.
Option B: Git push knows nothing about CI results.
Option C: Git cannot see other people's checkouts.
Related on TechInterviewNotes
- Production-Grade .NET Backend Interview Playbook
- Security, Configuration, Health Checks, and Production Delivery (production delivery)
- Reviewed Migrations and Expand/Contract Schema Evolution (safe delivery alongside your Git workflow)
Sources
- git-reset: the soft, mixed and hard modes.
- git-revert: inverse commits, and
-mfor merge commits. - git-rebase: replaying commits onto a new base.
- git-reflog and git-gc: the reflog and when its entries expire.
- git-push:
--force-with-leaseand its expected values. - Safe storage of app secrets in development in ASP.NET Core:
dotnet user-secrets.
TechInterviewNotes Editorial Team · Updated 28 Nov 2026 ·