15 Git Scenario Interview Questions
A driving test doesn't only ask what a stop sign means. At some point the examiner says "the road ahead is closed; what do you do?" Git interviews work the same way: alongside "what is rebase?" you will hear "you committed to main by mistake; fix it". These are scenario questions, and they test whether you can apply what you know calmly, without losing anyone's work. Every command on this page was run for real, and its output is shown as Git printed it. Each scenario starts in its own folder, so you can try them one at a time.
How to Answer Any Scenario
A strong answer follows the same five steps, and every model answer here uses them:
- Restate the problem in one sentence.
- Name the cause.
- Give the fix, with the commands.
- Say how you would prevent it.
- Mention the trade-offs, such as "this rewrites history, so only if nobody pulled yet".
Easy Scenarios (S1 to S5)
S1: I Committed to main Instead of My Feature Branch
Restate: two commits that belong on a feature branch are on main, and they aren't pushed yet.
Cause: forgot to create or switch to the branch before committing.
Fix: create the branch where main is now, so it keeps the commits, then move main back:
mkdir s1 && cd s1 && git init --quiet
echo 'Term fee: $12,000' > fees.txt && git add fees.txt && git commit --quiet -m "Add the term fee"
echo 'Late fee: $250' >> fees.txt && git commit --quiet -am "Add the late fee"
echo "Late fee applies after day 10" >> fees.txt && git commit --quiet -am "Explain the late fee"
git branch feature/late-fee
git reset --quiet --hard HEAD~2
git log --oneline --all --graph* a02b2b1 (feature/late-fee) Explain the late fee
* 9566698 Add the late fee
* 5c7eaa5 (HEAD -> main) Add the term feePrevent: check the branch in your prompt or with git status before committing; protect main on the server so a push would be refused anyway.
Trade-offs: reset --hard is safe here because the commits are kept on the new branch and nothing was pushed. If main was already pushed, don't rewrite it: git revert the commits on main instead. Revise: Fixing Commits in the Wrong Place.
S2: My Push Was Rejected: Non-Fast-Forward
Restate: git push fails because the remote branch has commits I don't have.
Cause: a teammate pushed first.
Fix: fetch their work, put mine on top, then push. Never --force a shared branch:
cd .. && mkdir s2 && cd s2
git init --quiet --bare server.git
git clone --quiet server.git navika 2>/dev/null
cd navika && echo 'Term fee: $12,000' > fees.txt && git add . && git commit --quiet -m "Add the term fee" && git push --quiet -u origin main
cd .. && git clone --quiet server.git advitha
cd advitha && echo 'Exam fee: $800' > exam.txt && git add . && git commit --quiet -m "Add the exam fee" --author="Advitha Anamaneni <advitha@example.com>" && git push --quiet
cd ../navika && echo 'Late fee: $250' >> fees.txt && git commit --quiet -am "Add the late fee"
git push --quiet 2>/dev/null || echo "rejected"
git pull --quiet --rebase
git push --quiet
git log --onelinerejected
6dcd316 (HEAD -> main, origin/main, origin/HEAD) Add the late fee
810295b Add the exam fee
88fbdea Add the term feePrevent: pull before starting and before pushing; set pull.rebase true if the team prefers a straight history.
Trade-offs: --rebase rewrites only my unpushed commits, so it is safe. A merge is equally correct and keeps the exact history; it adds a merge commit. Revise: Common Git Errors: Push and Pull.
S3: I Ran git reset --hard and Lost a Day of Work
Restate: commits disappeared from my branch after a hard reset.
Cause: reset --hard moved the branch back. Committed work isn't deleted, only unreferenced.
Fix: find the old position in the reflog and move back to it:
cd ../.. && mkdir s3 && cd s3 && git init --quiet
echo 'Term fee: $12,000' > fees.txt && git add . && git commit --quiet -m "Add the term fee"
echo 'Late fee: $250' >> fees.txt && git commit --quiet -am "A day of work"
git reset --quiet --hard HEAD~1
git reflog -3
git reset --quiet --hard "HEAD@{1}"
git log --onelinedde9e25 (HEAD -> main) HEAD@{0}: reset: moving to HEAD~1
5a2fbdb HEAD@{1}: commit: A day of work
dde9e25 (HEAD -> main) HEAD@{2}: commit (initial): Add the term fee
5a2fbdb (HEAD -> main) A day of work
dde9e25 Add the term feePrevent: commit often (even work in progress), push branches to the server, and prefer git reset --keep or git restore over --hard.
Trade-offs: this rescues committed work. Uncommitted changes wiped by --hard are usually gone; git fsck --lost-found can only find what was at least staged. Revise: Git Reflog.
S4: I Need to Review a Pull Request Without Losing My Current Work
Restate: I have half-finished changes and need to run a colleague's branch.
Cause: one working folder can have only one branch checked out.
Fix: open the branch in a second worktree:
cd .. && mkdir s4 && cd s4 && git init --quiet
echo 'Term fee: $12,000' > fees.txt && git add . && git commit --quiet -m "Add the term fee"
git branch fix/late-fee
echo "Unfinished idea" >> fees.txt
git worktree add --quiet ../s4-review fix/late-fee
git worktree list
git status --short
git worktree remove ../s4-review/home/navika/projects/s4 2f9aa5a [main]
/home/navika/projects/s4-review 2f9aa5a [fix/late-fee]
M fees.txtPrevent: nothing to prevent; this is the normal way. git stash also works for a quick switch.
Trade-offs: a worktree needs disk space for one more checkout but no second clone, and keeps my changes exactly as they are. Revise: Git Worktree.
S5: Every Line Shows as Changed After a Colleague Edited on Windows
Restate: a one-line edit shows the whole file as changed.
Cause: the file's line endings changed from LF to CRLF.
Fix: make the rules explicit in .gitattributes, then renormalize:
cd .. && mkdir s5 && cd s5 && git init --quiet
printf 'Term fee: $12,000\nLate fee: $250\n' > fees.txt && git add . && git commit --quiet -m "Add the fees"
printf 'Term fee: $12,000\r\nLate fee: $300\r\n' > fees.txt
git diff --stat
echo "* text=auto" > .gitattributes
git add --renormalize .
git diff --cached --stat fees.txt | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
fees.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)Explanation: before the fix, both lines counted as changed. With * text=auto, Git stores LF in the repository, and only the real change, the late fee, remains.
Prevent: commit a .gitattributes in every project, and let Windows users keep core.autocrlf true.
Trade-offs: --renormalize on an existing project can create one large commit that touches many files; make it a separate commit and add it to .git-blame-ignore-revs. Revise: Git .gitattributes and Line Endings.
Medium Scenarios (S6 to S10)
S6: A Teammate Force-Pushed and My Local Branch Is a Mess
Restate: after git pull, my branch has duplicate commits or conflicts, because the remote branch was rewritten.
Cause: the teammate rebased and force-pushed a shared branch; my copy still has the old commits.
Fix: if I have no local work on it, match the remote. If I do, replay only my commits onto the new remote branch:
cd .. && mkdir s6 && cd s6
git init --quiet --bare server.git
git clone --quiet server.git advitha 2>/dev/null
cd advitha && echo 'Term fee: $12,000' > fees.txt && git add . && git commit --quiet -m "Add the term fee" && echo "Draft" > notes.txt && git add . && git commit --quiet -m "Draft notes" && git push --quiet -u origin main
cd .. && git clone --quiet server.git navika
cd navika && echo 'Late fee: $250' > late.txt && git add . && git commit --quiet -m "Add the late fee (Navika)"
cd ../advitha && git commit --quiet --amend -m "Add notes" && git push --quiet --force-with-lease
cd ../navika && git fetch --quiet
git rebase --quiet --onto origin/main "origin/main@{1}" main
git log --oneline2682f54 (HEAD -> main) Add the late fee (Navika)
86ca2e5 (origin/main, origin/HEAD) Add notes
402b4dd Add the term feeExplanation: origin/main@{1} is where origin/main was before the fetch, the old base of my work. --onto origin/main replays only the commits after it, my one commit, onto the rewritten branch, leaving the old "Draft notes" commit behind. (git pull --rebase usually works this out by itself, using the fork point.)
Prevent: agree that shared branches are never rewritten; protect them on the server; use --force-with-lease only on your own branches.
Trade-offs: git reset --hard origin/main is simpler but throws away my local commits. Revise: Rebase Onto and Stacked Branches.
S7: I Pushed a File with an API Key
Restate: a secret is in a pushed commit.
Cause: it was committed before being ignored or moved to the environment.
Fix, in order: 1) revoke or rotate the key immediately at its provider, 2) remove it from the code and read it from the environment, 3) rewrite history with git filter-repo --replace-text or --path ... --invert-paths on a fresh clone, 4) force-push and have everyone re-clone, 5) ask the platform to clear cached views. Finding every commit that contained it:
cd ../.. && mkdir s7 && cd s7 && git init --quiet
echo 'Term fee: $12,000' > fees.txt && git add . && git commit --quiet -m "Add the term fee"
echo "PAYMENT_KEY=pk_live_51Navika" > settings.env && git add . && git commit --quiet -m "Add payment settings"
git rm --quiet settings.env && git commit --quiet -m "Remove settings"
git log --all --oneline -S "pk_live"b3590dc (HEAD -> main) Remove settings
4b84c06 Add payment settingsExplanation: deleting the file in a new commit didn't help: two commits still contain the key (the one that added it and the one that removed it, in its diff). git filter-repo isn't part of Git, so it isn't run here.
Prevent: .gitignore for settings files, environment variables, a secret scanner in a pre-commit hook and CI, and the platform's push protection.
Trade-offs: rewriting history disrupts everyone and doesn't make the key safe again. Rotation is the real fix. Revise: Keeping Secrets Out of Git.
S8: The Build Broke Sometime in the Last 200 Commits
Restate: a test fails now and passed at a known older commit.
Cause: unknown, which is exactly what git bisect finds.
Fix: let git bisect run test commits automatically by binary search:
cd .. && mkdir s8 && cd s8 && git init --quiet
for n in $(seq 1 20); do
if [ "$n" -lt 14 ]; then fee=250; else fee=2,500; fi
printf 'Late fee: $%s (change %s)\n' "$fee" "$n" > fees.txt
git add fees.txt && git commit --quiet -m "Change $n"
done
git bisect start HEAD "HEAD~19" > /dev/null
git bisect run grep -qF '$250 ' fees.txt | grep "is the first bad commit"
git bisect log | grep -c "^git bisect \(good\|bad\)"
git bisect log | tail -1
git bisect reset1df27adfb2dad33e5d100d87e784dba2d1f59cd2 is the first bad commit
5
# first bad commit: [1df27adfb2dad33e5d100d87e784dba2d1f59cd2] Change 14
Previous HEAD position was 034091b Change 13
Switched to branch 'main'Explanation: the test command (grep -qF, which looks for the exact text $250 ) exits with 0 for a good commit and 1 for a bad one. git bisect log holds 5 good or bad marks, including the two given to git bisect start, so Git ran the test on just 3 of the 20 commits and named "Change 14", where the fee became $2,500. For 200 commits, it needs about 8 tests.
Prevent: CI on every pull request, so a breaking change is caught before merging.
Trade-offs: bisect needs a reliable, scriptable test; commits that don't build can be skipped with git bisect skip (exit code 125 in a script). Revise: Git Bisect.
S9: We Need a Hotfix for Version 1.2 While main Has Unreleased Features
Restate: production runs v1.2.0, main has features that aren't ready, and a bug must be fixed now.
Cause: none; this is why release tags exist.
Fix: branch from the tag, fix, tag the patch release, and bring the fix back to main:
cd .. && mkdir s9 && cd s9 && git init --quiet
echo 'Late fee: $25O' > fees.txt && git add . && git commit --quiet -m "Add the late fee" && git tag -a v1.2.0 -m "Release 1.2.0"
echo "Unfinished feature" > beta.txt && git add . && git commit --quiet -m "Start the beta feature"
git switch --quiet -c hotfix/1.2.1 v1.2.0
echo 'Late fee: $250' > fees.txt && git commit --quiet -am "Fix the late fee typo"
git tag -a v1.2.1 -m "Release 1.2.1"
git switch --quiet main
git cherry-pick -x hotfix/1.2.1 > /dev/null
git log --oneline --graph --all* 8f6e52c (HEAD -> main) Fix the late fee typo
* d9bcb5c Start the beta feature
| * 22be837 (tag: v1.2.1, hotfix/1.2.1) Fix the late fee typo
|/
* 90e1cac (tag: v1.2.0) Add the late feeExplanation: v1.2.1 contains only the fix, not the beta feature. cherry-pick -x copied the fix to main, and its message records where it came from. Merging the hotfix branch into main is the other common choice, as in Gitflow.
Prevent: tag every release, so you can always branch from exactly what is in production.
Trade-offs: cherry-picking duplicates the commit (two IDs); merging keeps one commit but brings everything on the hotfix branch. Revise: Git Cherry-Pick.
S10: My Feature Branch Is 60 Commits Behind main and Full of Merge Commits
Restate: my branch has many "Merge branch 'main'" commits and is hard to review.
Cause: main was merged into it repeatedly.
Fix: if the branch is only mine, rebuild it as a clean series on top of the current main. Rebase drops the merge commits and replays only my own changes:
cd .. && mkdir s10 && cd s10 && git init --quiet
echo 'Term fee: $12,000' > fees.txt && git add . && git commit --quiet -m "Add the term fee"
git switch --quiet -c feature/books
echo 'Books fee: $600' > books.txt && git add . && git commit --quiet -m "Add the books fee"
for n in 1 2; do
git switch --quiet main && echo "Change $n" >> fees.txt && git commit --quiet -am "Main change $n"
git switch --quiet feature/books && git merge --quiet --no-edit main
done
git log --oneline feature/books | wc -l | tr -d " "
git rebase --quiet main
git log --oneline --graph6
* b024812 (HEAD -> feature/books) Add the books fee
* 5032a6e (main) Main change 2
* 47e58cb Main change 1
* 73df34a Add the term feeExplanation: the branch had 6 commits, including two merge commits. After git rebase main, it is one commit on top of main, easy to review.
Prevent: update feature branches with git pull --rebase or git rebase main, and keep branches short.
Trade-offs: rebasing rewrites the branch: force-push with --force-with-lease, and only if nobody else works on it. Squash-merging the pull request is an alternative that doesn't touch the branch. Revise: Merge vs Rebase.
Hard Scenarios (S11 to S15)
S11: The Repository Is 4 GB and Cloning Takes 20 Minutes
Restate: clones and fetches are slow because the repository is huge.
Cause: usually large binary files in history, or a monorepo with years of history and many files.
Fix: first measure what is big, then choose:
cd .. && mkdir s11 && cd s11 && git init --quiet
head -c 400000 /dev/zero | tr "\0" "x" > video.bin && echo 'Term fee: $12,000' > fees.txt && git add . && git commit --quiet -m "Add files"
git rev-list --objects --all | git cat-file --batch-check="%(objecttype) %(objectsize) %(rest)" | grep "^blob" | sort -k2 -n -rblob 400000 video.bin
blob 18 fees.txtThen: for large binary files, move them to Git LFS (git lfs migrate import); for developers, use git clone --filter=blob:none and sparse checkout; for CI, --depth 1; and turn on git maintenance and scalar for local speed.
Prevent: block large files with a pre-receive hook or platform limits, use LFS from the start, and keep build output out of Git.
Trade-offs: migrating to LFS rewrites history; partial clones need the server for old content. Revise: Shallow and Partial Clones.
S12: Two Teams Keep Resolving the Same Conflict in a Generated File
Restate: a generated file, such as a lock file or a version list, conflicts in every merge.
Cause: both branches regenerate it, and it shouldn't be merged line by line at all.
Fix: regenerate it instead of merging, or tell Git how to merge it. For a file that should always keep our version on merge:
cd .. && mkdir s12 && cd s12 && git init --quiet
git config merge.ours.driver true
echo "build.properties merge=ours" > .gitattributes
echo "build=1" > build.properties && git add . && git commit --quiet -m "Start"
git switch --quiet -c team-b && echo "build=2" > build.properties && git commit --quiet -am "Build 2"
git switch --quiet main && echo "build=3" > build.properties && git commit --quiet -am "Build 3"
git merge --no-edit team-b
cat build.propertiesAuto-merging build.properties
Merge made by the 'ort' strategy.
build=3Explanation: the custom merge driver ours (the command true, which leaves our version) stopped the conflict. For conflicts that recur in normal code, rerere.enabled true lets Git reuse the recorded resolution.
Prevent: don't commit generated files when the build can create them; otherwise regenerate after merging.
Trade-offs: merge=ours silently ignores the other side's version, so regenerate the file afterwards or make sure it truly doesn't matter. The driver setting lives in each person's config, not in the repository. Revise: Git Rerere.
S13: A Submodule Shows Changes Nobody Made
Restate: git status says modified: lib (new commits) after a pull, but I changed nothing.
Cause: the superproject's pinned commit changed, and my submodule folder is still on the old one.
Fix: update the submodule to the pinned commit:
cd .. && mkdir s13 && cd s13
git config --global protocol.file.allow always
git init --quiet lib && (cd lib && echo "v1" > rules.txt && git add . && git commit --quiet -m "Rules v1")
git init --quiet app && cd app && git commit --quiet --allow-empty -m "Start"
git submodule --quiet add ../lib lib && git commit --quiet -m "Add lib"
cd .. && git clone --quiet --recurse-submodules app copy 2>/dev/null
cd lib && echo "v2" > rules.txt && git commit --quiet -am "Rules v2"
cd ../app && git -C lib pull --quiet && git commit --quiet -am "Update lib"
cd ../copy && git pull --quiet && git status --short
git submodule --quiet update && git status --short && cat lib/rules.txt M lib
v2Explanation: after the pull, lib showed as modified ( M) because it was still on "Rules v1". git submodule update checked out the pinned "Rules v2", and the status became clean. (protocol.file.allow is needed only because this example uses local folders as servers.)
Prevent: git config --global submodule.recurse true, so pulls update submodules.
Trade-offs: committing in that state with -a would pin the old version again, undoing the teammate's update. Revise: Git Submodules.
S14: A Release Tag Points at the Wrong Commit, and It Is Already Pushed
Restate: v2.0.0 was created on the wrong commit and pushed.
Cause: tagged before the last commit, or on the wrong branch.
Fix: don't move a published tag: people and build systems may already have the old one, and Git doesn't update tags on fetch. Release the correct commit under a new version:
cd ../.. && mkdir s14 && cd s14 && git init --quiet
echo 'Term fee: $12,000' > fees.txt && git add . && git commit --quiet -m "Prepare 2.0"
git tag -a v2.0.0 -m "Release 2.0.0"
echo 'Late fee: $250' >> fees.txt && git commit --quiet -am "Last fix for 2.0"
git tag -a v2.0.1 -m "Release 2.0.1: includes the missing fix"
git log --oneline --decorate7305b8d (HEAD -> main, tag: v2.0.1) Last fix for 2.0
108b707 (tag: v2.0.0) Prepare 2.0Explanation: v2.0.1 marks the right commit, and its message explains why. If the tag was pushed minutes ago and nobody can have used it, the team may agree to delete and recreate it (git push origin :refs/tags/v2.0.0, then retag and push), and must tell everyone to run git fetch --tags --force.
Prevent: tag from CI on the merged commit, or check git log -1 before tagging; sign release tags.
Trade-offs: a new version number is always safe; moving a tag is only acceptable before anyone used it. Revise: Git Tag.
S15: We Are Moving a Project from SVN to Git
Restate: a project's history is in Subversion, and the team wants to move to Git without losing it.
Cause: none; it is a migration.
Fix: this needs an SVN server, so it is shown without output:
git svn clone https://svn.example.com/fee-tracker --stdlayout --authors-file=authors.txt fee-tracker
cd fee-tracker
git remote add origin git@github.com:school/fee-tracker.git
git push -u origin --all
git push origin --tagsExplanation: git svn clone (a separate package on some systems) reads every SVN revision and turns it into a Git commit; --stdlayout maps SVN's trunk, branches and tags folders to Git branches and tags, and authors.txt maps SVN user names to Name <email>. SVN tags arrive as branches and should be converted to real Git tags. For very large histories, tools such as svn2git or the hosting platforms' importers are faster.
Prevent: not applicable; plan a freeze day, migrate, check the history, and make the SVN repository read-only.
Trade-offs: large binaries and empty folders (which Git doesn't track) need special care; teach the team the new workflow before the switch, not after. Revise: Git Best Practices.
Before the Interview
- Try each scenario in a practice folder, without looking at the answer.
- Say the five steps out loud for two scenarios a day.
- Know your safety nets:
git reflog,git branch backup,--force-with-leaseandgit merge --abort. - Revise the theory with 50 Git Interview Questions and Answers.
Was this tutorial helpful?