Git interview questions: commands with real output
The version control questions developers, DevOps engineers and data professionals get, each answered in plain words and run on a sample repository.
Git interviews test whether you understand what each command does to your branch, the staging area and your files: fetch versus pull, merge versus rebase, fast-forward merges, undoing work with reset, revert and restore, stashing, resolving conflicts, cherry-picking, tags, and recovering or tracking down commits with the reflog and bisect. Below are 15 questions from basic to advanced; every command runs on a small sample repository and the output shown is real.
The repository every example starts from
Each example starts in a fresh, empty directory, runs this setup script, then runs its own commands in the same shell. The script fixes the author, the dates and the Git settings, so the commit IDs and output are the same on every run.
export GIT_CONFIG_NOSYSTEM=1 GIT_AUTHOR_NAME="Ava Lee" GIT_AUTHOR_EMAIL="ava@example.com"
export GIT_COMMITTER_NAME="Ava Lee" GIT_COMMITTER_EMAIL="ava@example.com"
export GIT_AUTHOR_DATE="2026-01-05T09:00:00Z" GIT_COMMITTER_DATE="2026-01-05T09:00:00Z"
git config --global init.defaultBranch main
git config --global core.autocrlf false
git config --global advice.detachedHead false
git init -q shop && cd shop
printf 'def total(items):\n return sum(items)\n' > cart.py
git add cart.py && git commit -q -m "Add cart total"
printf '# Shop\n' > README.md
git add README.md && git commit -q -m "Add README"Basic Git interview questions
Basic1. What is the difference between git fetch and git pull?
git fetch downloads new commits from the remote and updates your remote-tracking branches, such as origin/main, but leaves your own branch where it was. git pull runs a fetch and then integrates the result into your current branch. With no configuration, Git only fast-forwards: if your branch has no commits of its own, it moves forward to match the remote. If both sides have new commits (the histories have diverged), Git stops and asks you to choose a merge or a rebase, with git pull --no-rebase or --rebase each time, or once with the pull.rebase setting. The example shows all three cases: fetch, a fast-forward pull, and a divergent pull that stops until --rebase is given.
Bashgit clone -q --bare . ../origin.git && git remote add origin ../origin.git
git fetch -q origin && git branch -q -u origin/main
git clone -q ../origin.git ../teammate
(cd ../teammate && echo 'TAX = 0.2' >> cart.py && git commit -q -am "Add tax rate" && git push -q)
git fetch -q
echo "after fetch: main = $(git log -1 --format=%s main) | origin/main = $(git log -1 --format=%s origin/main)"
git pull -q
echo "after pull (fast-forward): main = $(git log -1 --format=%s main)"
(cd ../teammate && echo 'DISCOUNT = 0.1' >> cart.py && git commit -q -am "Add discount" && git push -q)
echo '# Notes' >> README.md && git commit -q -am "Local notes"
git pull > pull.log 2>&1 || echo "diverged, git pull stopped (exit $?): $(grep fatal pull.log)"
git pull -q --rebase
git log --format=%sOutput (Git 2.54.0 in GNU bash 5.3.9)after fetch: main = Add README | origin/main = Add tax rate after pull (fast-forward): main = Add tax rate diverged, git pull stopped (exit 128): fatal: Need to specify how to reconcile divergent branches. Local notes Add discount Add tax rate Add README Add cart total
Checked against: Git reference manual: git-fetch, Git reference manual: git-pull
Basic2. What is the staging area (the index)?
The index sits between your working files and the next commit: git add copies a file's current content into it, and git commit records what the index holds. That is why one file can be staged and another only modified. In git status --short, the left column is the index and the right column is the working tree.
Bashecho '# Shop app' > README.md
git add README.md
echo 'print(total([1, 2]))' >> cart.py
git status --shortOutput (Git 2.54.0 in GNU bash 5.3.9)M README.md M cart.py
Checked against: Git reference manual: glossary
Basic3. How do you undo the last commit, and what do --soft, --mixed and --hard change?
git reset moves the branch back to an earlier commit; the mode decides what happens to your files. --soft keeps the changes staged, --mixed (the default) keeps them in the working tree but unstaged, and --hard discards them from both. The example makes one commit and undoes it three ways; after each it restores the commit with ORIG_HEAD so the next mode starts from the same place.
Bashecho 'TAX = 0.2' >> cart.py && git commit -q -am "Add tax"
for mode in soft mixed hard; do
git reset -q --$mode HEAD~1
echo "--$mode: HEAD = '$(git log -1 --format=%s)', status = [$(git status --short)]"
git reset -q --hard ORIG_HEAD
doneOutput (Git 2.54.0 in GNU bash 5.3.9)--soft: HEAD = 'Add README', status = [M cart.py] --mixed: HEAD = 'Add README', status = [ M cart.py] --hard: HEAD = 'Add README', status = []
Checked against: Git reference manual: git-reset
Basic4. What is the difference between git revert and git reset?
git revert adds a new commit that undoes an earlier one, so history only moves forward; that makes it the safe choice on a branch other people already have. git reset moves the branch pointer back, which rewrites history and should stay on commits nobody else has pulled.
Bashecho 'TAX = 0.2' >> cart.py && git commit -q -am "Add tax"
git revert --no-edit HEAD > /dev/null
git log --format=%s
echo "last line of cart.py: $(tail -1 cart.py)"Output (Git 2.54.0 in GNU bash 5.3.9)Revert "Add tax" Add tax Add README Add cart total last line of cart.py: return sum(items)
Checked against: Git reference manual: git-revert
Basic5. When do you use git stash?
When you have uncommitted work but need a clean working tree, for example to switch branches for an urgent fix. git stash saves the changes and resets the files to the last commit; git stash pop brings them back. Give stashes a message so the list stays readable.
Bashecho 'DISCOUNT = 0.1' >> cart.py
git stash push -q -m "discount idea"
git stash list
echo "status after stash: [$(git status --short)]"
git stash pop -q
echo "status after pop: [$(git status --short)]"Output (Git 2.54.0 in GNU bash 5.3.9)stash@{0}: On main: discount idea
status after stash: []
status after pop: [ M cart.py]Checked against: Git reference manual: git-stash
Intermediate Git interview questions
Intermediate6. What is the difference between git merge and git rebase?
Both bring the work of one branch into another. Merge keeps both lines of history and joins them with a merge commit. Rebase replays your commits on top of the other branch, giving a straight line but creating new commits with new IDs. The Git manual warns against rebasing a branch that others have based work on, because they would have to repair their own history.
Bashgit switch -q -c feature && echo 'def count(items): return len(items)' >> cart.py && git commit -q -am "Add count"
git switch -q main && echo 'MIT' > LICENSE && git add LICENSE && git commit -q -m "Add license"
git branch feature-copy feature
git merge -q --no-edit feature && echo "merge:" && git log --graph --format=%s
git reset -q --hard HEAD~1
git switch -q feature-copy && git rebase -q main && echo "rebase:" && git log --graph --format=%sOutput (Git 2.54.0 in GNU bash 5.3.9)merge: * Merge branch 'feature' |\ | * Add count * | Add license |/ * Add README * Add cart total rebase: * Add count * Add license * Add README * Add cart total
Checked against: Git reference manual: git-merge, Git reference manual: git-rebase
Intermediate7. What is a fast-forward merge, and when would you use --no-ff?
If your branch has no commits of its own since the other branch split off, Git can merge by simply moving your branch pointer forward: no merge commit is made. --no-ff forces a merge commit anyway, which some teams use so each feature shows up as one merge in the history.
Bashgit switch -q -c hotfix && echo '# rounding fix' >> cart.py && git commit -q -am "Fix rounding"
git switch -q main && git merge -q --ff-only hotfix
echo "fast-forward:"; git log --graph --format=%s
git switch -q -c docs && echo 'Usage notes' >> README.md && git commit -q -am "Update docs"
git switch -q main && git merge -q --no-ff -m "Merge docs" docs
echo "no-ff:"; git log --graph --format=%sOutput (Git 2.54.0 in GNU bash 5.3.9)fast-forward: * Fix rounding * Add README * Add cart total no-ff: * Merge docs |\ | * Update docs |/ * Fix rounding * Add README * Add cart total
Checked against: Git reference manual: git-merge
Intermediate8. How do you resolve a merge conflict?
A conflict happens when both branches changed the same lines. Git stops, marks the file with conflict markers (your side between <<<<<<< and =======, theirs between ======= and >>>>>>>), and waits. You edit the file to the version you want, git add it, and commit to finish the merge. Read both sides before deciding: often the right result combines them.
Bashgit switch -q -c price && sed -i 's/sum(items)/round(sum(items), 2)/' cart.py && git commit -q -am "Round total"
git switch -q main && sed -i 's/sum(items)/sum(items) * 1.2/' cart.py && git commit -q -am "Add tax to total"
git merge price || true
cat cart.py
printf 'def total(items):\n return round(sum(items) * 1.2, 2)\n' > cart.py
git add cart.py && git commit -q --no-edit
git log --graph --format=%sOutput (Git 2.54.0 in GNU bash 5.3.9)Auto-merging cart.py
CONFLICT (content): Merge conflict in cart.py
Automatic merge failed; fix conflicts and then commit the result.
def total(items):
<<<<<<< HEAD
return sum(items) * 1.2
=======
return round(sum(items), 2)
>>>>>>> price
* Merge branch 'price'
|\
| * Round total
* | Add tax to total
|/
* Add README
* Add cart totalChecked against: Git reference manual: git-merge
Intermediate9. What does git cherry-pick do?
It copies the change made by one commit onto your current branch as a new commit. Use it to bring a single fix across, for example to a release branch, without merging everything else on the source branch.
Bashgit switch -q -c experiments
echo '# idea 1' >> README.md && git commit -q -am "Try idea 1"
echo 'def safe_total(items): return sum(items or [])' >> cart.py && git commit -q -am "Handle empty cart"
git switch -q main
git cherry-pick experiments > /dev/null
git log --format=%s
echo "idea 1 on main: $(grep -c 'idea 1' README.md)"Output (Git 2.54.0 in GNU bash 5.3.9)Handle empty cart Add README Add cart total idea 1 on main: 0
Checked against: Git reference manual: git-cherry-pick
Intermediate10. How do you get back a commit after git reset --hard?
Commits are not deleted right away: the reflog records every position HEAD has had in your local repository, so you can find the lost commit and reset back to it. The reflog is local only and old entries expire, so recover soon.
Bashecho 'TAX = 0.2' >> cart.py && git commit -q -am "Add tax"
git reset -q --hard HEAD~1
echo "after reset: $(git log -1 --format=%s)"
git reflog --format='%gd %gs' | head -3
git reset -q --hard 'HEAD@{1}'
echo "recovered: $(git log -1 --format=%s)"Output (Git 2.54.0 in GNU bash 5.3.9)after reset: Add README
HEAD@{0} reset: moving to HEAD~1
HEAD@{1} commit: Add tax
HEAD@{2} commit: Add README
recovered: Add taxChecked against: Git reference manual: git-reflog
Intermediate11. How does .gitignore work, and why is a file still tracked after you add it there?
.gitignore lists patterns for untracked files Git should not offer to add; a later pattern starting with ! re-includes a path. It does not affect files that are already tracked: to stop tracking one, remove it from the index with git rm --cached: the file stays on disk, status shows the removal as staged (D), and after the next commit Git ignores it. git check-ignore -v shows which rule matched.
Bashprintf '*.log\nbuild/\n!keep.log\n' > .gitignore
mkdir build && touch debug.log keep.log build/app.bin
git status --short --untracked-files=all
git check-ignore -v debug.log build/app.bin
git add -f debug.log && git commit -q -m "Track a log by mistake"
echo 'more' >> debug.log && git status --short debug.log
git rm -q --cached debug.log && git status --short debug.logOutput (Git 2.54.0 in GNU bash 5.3.9)?? .gitignore ?? keep.log .gitignore:1:*.log debug.log .gitignore:2:build/ build/app.bin M debug.log D debug.log
Checked against: Git reference manual: gitignore
Advanced Git interview questions
Advanced12. What is the difference between a lightweight and an annotated tag?
A lightweight tag is just a name pointing at a commit. An annotated tag is its own object with the tagger, a date and a message, and it can be signed. The Git manual recommends annotated tags for releases and lightweight tags for private or temporary labels.
Bashgit tag v1.0-light
git tag -a v1.0 -m "First release"
echo "v1.0-light is a $(git cat-file -t v1.0-light), v1.0 is a $(git cat-file -t v1.0)"
git tag -nOutput (Git 2.54.0 in GNU bash 5.3.9)v1.0-light is a commit, v1.0 is a tag v1.0 First release v1.0-light Add README
Checked against: Git reference manual: git-tag
Advanced13. How do you find the commit that introduced a bug?
Use git bisect: mark a bad commit and a good one, and Git checks out commits in between using binary search, so even a long history takes few steps. With git bisect run, a script decides for each commit: exit code 0 means good, 1 to 127 (except 125) means bad, 125 means the commit cannot be tested and is skipped, and any other code aborts the bisect. Here the bug is the + 1 added in the fourth of six new commits.
Bashfor i in 1 2 3 4 5 6; do
if [ $i -eq 4 ]; then sed -i 's/sum(items)/sum(items) + 1/' cart.py; fi
echo "change $i" >> CHANGES && git add -A && git commit -q -m "Change $i"
done
git bisect start HEAD HEAD~6 > /dev/null
git bisect run sh -c '! grep -q "sum(items) + 1" cart.py' > bisect.log
echo "first bad commit: $(git show -s --format=%s refs/bisect/bad)"
git bisect reset > /dev/null 2>&1Output (Git 2.54.0 in GNU bash 5.3.9)first bad commit: Change 4
Checked against: Git reference manual: git-bisect
Advanced14. What is a detached HEAD, and how do you keep work made in that state?
Normally HEAD points to a branch, and new commits move that branch forward. If you check out a commit directly, HEAD points at the commit itself: you can look around and even commit, but no branch remembers those commits. To keep them, create a branch at that point before switching away.
Bashgit switch -q --detach HEAD~1
git status | head -1
echo "current branch: '$(git branch --show-current)'"
echo '# experiment' >> cart.py && git commit -q -am "Experiment"
git switch -q -c experiment
echo "current branch: '$(git branch --show-current)'"; git log -1 --format=%sOutput (Git 2.54.0 in GNU bash 5.3.9)HEAD detached at aa8a6ec current branch: '' current branch: 'experiment' Experiment
Checked against: Git reference manual: glossary, Git reference manual: git-switch
Advanced15. What do git restore and git restore --staged do?
git restore discards changes to a file in the working tree, replacing it with the version from the index (or another source with --source). git restore --staged unstages a file: it copies the committed version into the index and leaves your working copy alone. Both replace jobs that git checkout and git reset used to do for files.
Bashecho 'BUG = True' >> cart.py && git add cart.py && echo 'MORE = 1' >> cart.py
git status --short
git restore --staged cart.py && git status --short
git restore cart.py && echo "after restore: [$(git status --short)]"Output (Git 2.54.0 in GNU bash 5.3.9)MM cart.py M cart.py after restore: []
Checked against: Git reference manual: git-restore
How to practice with this page
- Before reading each output, say what you expect git log or git status to show.
- Draw the commit graph for merge and rebase questions: interviewers often ask you to.
- Run the setup script yourself, then repeat each example and change one step.
- Know the safe choice on shared branches: revert, not reset or rebase.
Related interview prep and tools
CI/CD, infrastructure and operations questions.
Open →QuestionsLinux interview questionsShell commands with real output.
Open →QuestionsTerraform interview questionsInfrastructure as code, checked against the docs.
Open →RoleSoftware engineer interview questionsRole-specific interview questions.
Open →Free toolResume summary generatorDraft a summary for a Software Engineer role; the title is filled in, you add your experience.
Open →Free toolLinkedIn headline generatorWrite a headline for a Software Engineer search.
Open →Disclosure: TryHired makes HIRED, a paid job-search program, so we have a conflict of interest when we mention it. This question bank is free, with no TryHired signup.
Questions people ask
What Git topics come up often in interviews?
Is Git the same as GitHub?
Should I practice Git on the command line or in an IDE?
How this page is built
TryHired wrote these questions and answers. Every command block on this page was run with Git 2.54.0 in GNU bash 5.3.9 when the page was built (2026-11-15): each example starts in a fresh temporary directory, runs the setup script shown above and then the commands, in one shell. The output shown is the real output, and the build stops if any example fails. The question list is general practice: it does not predict what a particular employer will ask.
- Interviewers ask different questions for different roles and levels.
- Answers are short explanations, not complete references.
- Examples ran with Git for Windows in GNU bash. The commands and their output are the same on Linux and macOS for these examples; the setup turns off line-ending conversion, which otherwise differs on Windows.
- Git reference manual: git-bisect (read 2026-11-15)
- Git reference manual: git-cherry-pick (read 2026-11-15)
- Git reference manual: git-fetch (read 2026-11-15)
- Git reference manual: glossary (read 2026-11-15)
- Git reference manual: gitignore (read 2026-11-15)
- Git reference manual: git-merge (read 2026-11-15)
- Git reference manual: git-pull (read 2026-11-15)
- Git reference manual: git-rebase (read 2026-11-15)
- Git reference manual: git-reflog (read 2026-11-15)
- Git reference manual: git-reset (read 2026-11-15)
- Git reference manual: git-restore (read 2026-11-15)
- Git reference manual: git-revert (read 2026-11-15)
- Git reference manual: git-stash (read 2026-11-15)
- Git reference manual: git-switch (read 2026-11-15)
- Git reference manual: git-tag (read 2026-11-15)