Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close
    Cheat sheets: all cheat sheets

    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 HEAD names 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 --soft keeps the undone changes staged, --mixed (the default) keeps them unstaged, --hard discards them.
    • git reflog lists where HEAD pointed recently (git reflog show <branch> for one branch), so a commit "lost" to reset --hard can usually be found again.

    Everyday commands

    TaskCommand
    Start a branch from the current commitgit switch -c feature/x
    See unstaged / staged changesgit diff / git diff --staged
    Stage part of a filegit add -p
    Fix the last commit before pushing itgit commit --amend
    Download remote changes without touching your branchgit fetch
    Fetch and integrategit pull --rebase or git pull --no-rebase (merge)
    Put work in progress asidegit stash push -m "wip", later git stash pop (-u adds untracked files)
    History as a graphgit log --oneline --graph --decorate --all
    Who changed these linesgit blame -L 10,20 path
    Find the commit that broke somethinggit bisect start, git bisect bad, git bisect good <sha>
    Copy one commit onto the current branchgit cherry-pick <sha>
    Tag a releasegit 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 featuregit rebase main (on the feature branch)
    Historykept as it happened, plus a merge commit (unless fast-forward)linear: your commits replayed on top of main
    Commit hashesunchangednew for every replayed commit
    Conflictsresolved once, in the merge commitpossibly once per replayed commit
    On a shared branchsafeunsafe: others' work is based on the old commits
    Typical useintegrating into main, often through a pull requesttidying 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-ff forces a merge commit.
    • A squash merge puts one new commit on main with no link to the feature branch's commits. git merge --squash stages that change; you still run git commit.

    Undoing things

    SituationCommandRewrites history?
    Discard unstaged edits to a filegit restore pathno
    Unstage a file, keep the editsgit restore --staged pathno
    Undo the last commit, keep its changes stagedgit reset --soft HEAD~1yes, locally
    Undo the last commit, keep its changes unstagedgit reset HEAD~1 (--mixed)yes, locally
    Undo the last commit and discard its changesgit reset --hard HEAD~1yes, and it destroys uncommitted work
    Undo a commit that is already pushed or sharedgit revert <sha> (a merge commit needs -m <parent>)no: it adds an inverse commit
    Get a "lost" commit backfind it in git reflog, then git switch -c rescue <sha>no
    Stop a merge or rebase in progressgit merge --abort / git rebase --abortno
    Push after rewriting your own branchgit push --force-with-leaseyes, on the remote
    • --force-with-lease refuses to overwrite the remote branch if it no longer matches your remote-tracking branch. A background git fetch can 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 --force has 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.

    Shell
    #!/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)"
    Output
    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 where HEAD was before the last move, here the commit that reset --hard dropped.
    • revert kept c3 in the history and added a commit that undoes it.
    • The feature branch started at c2; after the rebase, feat is a new commit on top of main.

    Workflows interviewers ask about

    WorkflowShapeFits
    Trunk-based developmentshort-lived branches merged to main often; feature flags hide unfinished workcontinuous delivery, frequent deploys
    GitHub flowbranch, pull request, review and CI, merge to main, deploymost web teams
    Git flowdevelop, release/*, hotfix/* and mainversioned 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 ignores bin/, obj/, .vs/ and user files.
    • Keep secrets out of appsettings.json and appsettings.*.json: use dotnet user-secrets in 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 --hard with 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 revert it 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 --force after 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?

    A git reset --hard HEAD~1, then force-push
    B git revert <sha> and push the new commit
    C git rebase -i to drop it, then force-push
    D Delete main and push it again from a clean copy
    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?

    A Gone, until you search for them in the reflog
    B Staged, ready to commit again as they were
    C In the working tree, but not staged
    D In a new stash entry made by the reset
    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?

    A That every commit on the branch is signed
    B That CI has passed on the commits being pushed
    C That no one else has the branch checked out
    D That the remote branch still matches what you last fetched
    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.

    Sources

    TechInterviewNotes Editorial Team · Updated 28 Nov 2026 ·