50 Git Interview Questions and Answers

Updated · 14 min read

Almost every developer interview includes a few Git questions, and for some roles a whole round. Interviewers rarely want a list of commands. They want to know that you understand what Git is doing, so that you won't lose work, break main or panic at a merge conflict. This page has 50 questions that interviewers really ask, from a first campus round to a senior engineering panel, with model answers. For questions that start with "what would you do if...", see Git Scenario Questions.

How to Practice

  1. Pick a level with the buttons below: Easy is fresher and campus level, Medium is for 1 to 5 years of experience, and Hard is senior level, where interviewers check whether you know how Git works inside.
  2. Answer out loud in under 2 minutes before opening the model answer.
  3. Compare your answer with the key points in the model answer.
  4. Missed something? Every answer ends with a link to the lesson that explains it.

Easy Questions (1 to 20)

The basics every interviewer checks first.

1. What is Git?

Git is a free, open source distributed version control system. It records snapshots of a project's files over time, so you can see who changed what and when, go back to any earlier version, and work on several versions at once with branches. "Distributed" means every clone has the complete history, so most commands work offline. Revise: What Is Git.

2. What is the difference between Git and GitHub?

Git is the version control program that runs on your computer. GitHub is a website that hosts Git repositories and adds collaboration features: pull requests, issues, code review, CI with Actions and access control. GitLab and Bitbucket are similar services. You can use Git without any of them. Revise: Git vs GitHub, GitLab and Bitbucket.

3. What is a repository?

A project folder whose history Git tracks. The history and Git's data live in the hidden .git folder at the top of the project; the files you edit are the working tree. git init creates a repository, git clone copies one. Revise: Git Init: Creating a Repository.

4. What is a commit?

A saved snapshot of the whole project at one moment, with an author, a date, a message and a link to its parent commit. Each commit has a unique ID, a hash of its contents, such as 6bceff0. Revise: Git Commit.

5. What are the three areas in Git?

The working tree (the files you edit), the staging area or index (what will go into the next commit), and the repository (the commits in .git). git add copies changes from the working tree to the staging area, and git commit records the staging area as a commit. Revise: Working Tree, Staging Area and Repository.

6. What is the difference between git add and git commit?

git add chooses which changes go into the next commit by putting them in the staging area. git commit saves everything staged as one permanent commit. Staging lets you split your work into several focused commits. Revise: Git Add: Staging Changes.

7. What does .gitignore do?

It lists files and folders Git should not track, such as build output (target/), IDE files and secrets. It only affects untracked files: a file already committed must be removed with git rm --cached first. Revise: Git .gitignore.

8. What does git status show?

The current branch, how it compares with its upstream (ahead/behind), which changes are staged, which are not, and which files are untracked. It also says when a merge or rebase is in progress, and suggests the next commands. Revise: Git Status and File States.

9. How do you see the history of a project?

git log lists commits, newest first. git log --oneline shows one line per commit, --graph --all draws the branches, -p shows each change, and options such as --author, --since and -- <file> filter it. Revise: Git Log: Viewing History.

10. What is a branch?

A movable name that points to a commit. New commits on a branch move it forward. Branches let you develop features or fixes separately and merge them later, and they cost almost nothing: a branch is a 41-byte file. Revise: Git Branch.

11. What is HEAD?

A pointer to what you have checked out, normally the current branch (.git/HEAD contains ref: refs/heads/main). New commits go where HEAD points. HEAD~1 means the commit before it. Revise: HEAD and Detached HEAD.

12. What is the difference between git fetch and git pull?

git fetch downloads new commits from the remote and updates remote-tracking branches such as origin/main, without changing your work. git pull is git fetch followed by a merge (or rebase) into your current branch. Fetch is always safe; pull changes your branch. Revise: Git Fetch.

13. What does git push do?

It uploads your local commits to a remote and moves the remote branch to match. It is refused if the remote has commits you don't have, so that nobody's work is overwritten. git push -u origin <branch> also sets the upstream. Revise: Git Push.

14. What is the difference between a clone and a fork?

A clone is a Git copy of a repository on your computer. A fork is a copy of a repository on the hosting platform, under your account, used when you can't push to the original: you push to your fork and open a pull request. Revise: Forking a Repository.

15. What is a merge conflict?

When two branches change the same lines differently, Git can't decide which version to keep, so it stops the merge and marks the lines with <<<<<<<, ======= and >>>>>>>. You edit the file, keep the right content, git add it and commit. Revise: Resolving Merge Conflicts.

16. What does git stash do?

It saves your uncommitted changes and gives you a clean working tree, so you can switch branches or pull. git stash pop brings the changes back. Stashes are a stack: git stash list shows them. Revise: Git Stash.

17. What is a tag?

A permanent name for one commit, usually a release such as v1.2.0. Unlike a branch, a tag doesn't move. Annotated tags (git tag -a) also store the tagger, date and a message. Push them with git push origin <tag>. Revise: Git Tag.

18. What is the difference between git diff and git diff --staged?

git diff shows changes in the working tree that aren't staged yet. git diff --staged (or --cached) shows what is staged, that is, what the next commit will contain. git diff HEAD shows both together. Revise: Git Diff: Comparing Changes.

19. How do you undo changes to a file you haven't staged?

git restore <file> replaces it with the last committed (or staged) version. The change is lost, so be sure. The older form is git checkout -- <file>. Revise: Git Restore.

20. What is origin?

The default name Git gives to the remote you cloned from. It is just a name for a URL; git remote -v shows it, and you can rename it or add others, such as upstream. Revise: Git Remote.

Medium Questions (21 to 40)

Everyday team work: branches, history and fixing mistakes.

21. What is the difference between merge and rebase?

Both bring changes from one branch into another. Merge keeps history as it happened and adds a merge commit joining the two lines. Rebase replays your commits on top of the other branch, creating new commits with new IDs and a straight line. Rebase gives cleaner history but rewrites commits, so don't rebase commits others already use. Revise: Merge vs Rebase.

22. What is a fast-forward merge?

When the branch you merge into has no new commits of its own, Git just moves its pointer forward to the other branch's tip, with no merge commit. --no-ff forces a merge commit anyway; --ff-only refuses anything but a fast-forward. Revise: Fast-Forward vs Merge Commit.

23. Explain git reset --soft, --mixed and --hard.

All move the current branch to another commit. --soft keeps the undone changes staged. --mixed (the default) keeps them in the working tree, unstaged. --hard throws them away completely. Only --hard can lose uncommitted work. Revise: Git Reset: Soft, Mixed and Hard.

24. What is the difference between git reset and git revert?

reset moves the branch back, removing commits from it (rewriting history). revert adds a new commit that undoes an earlier one, leaving history intact. Use revert for commits already pushed and shared; reset for local ones. Revise: Git Revert.

25. Why were git switch and git restore added?

git checkout did two unrelated jobs, switching branches and overwriting files, and a typo could turn one into the other. Since Git 2.23, switch only changes branches and restore only restores files. checkout still works and isn't going away. Revise: Git Switch and Checkout.

26. What is a detached HEAD, and is it dangerous?

HEAD points straight at a commit instead of a branch, after git checkout <commit> or inside a submodule. Looking around is fine. Commits made there belong to no branch and are easy to lose, so create a branch (git switch -c <name>) to keep them. Revise: HEAD and Detached HEAD.

27. Walk me through resolving a merge conflict.

Run git status to see the conflicted files. Open each one, read both sides between the markers, write the correct result and remove the markers. Run the tests, git add each file, then git commit (or git rebase --continue). If it goes wrong, git merge --abort starts over. Revise: Resolving Merge Conflicts.

28. What is an interactive rebase used for?

git rebase -i <base> lists your commits and lets you reorder, reword, squash, fixup, edit or drop them before sharing, to turn messy work-in-progress commits into a clean series. Revise: Interactive Rebase.

29. What does "squash and merge" do?

It combines all commits of a pull request into one new commit on the target branch. History stays short, one commit per feature, but the individual commits of the branch are not kept on main. Revise: Pull Requests and Merge Requests.

30. What is git cherry-pick?

It copies a commit from another branch onto the current one, as a new commit with the same changes. Typical use: bringing a bug fix from main to a release branch. -x records the original commit ID in the message. Revise: Git Cherry-Pick.

31. What is the difference between --force and --force-with-lease?

Both overwrite the remote branch. --force overwrites whatever is there, even commits a teammate just pushed. --force-with-lease only overwrites if the remote is still where you last saw it, and fails otherwise. Add --force-if-includes for extra safety. Revise: Force Push Safely.

32. What is an upstream branch?

The remote branch a local branch is connected to, such as origin/main for main. It lets plain git pull and git push know where to go, and git status show ahead/behind counts. Set it with git push -u or git branch -u. Revise: Upstream and Remote-Tracking Branches.

33. What is the difference between git stash pop and git stash apply?

Both restore a stash. pop also deletes it from the stash list if it applied cleanly; apply keeps it, so you can apply it again elsewhere. Revise: Git Stash.

34. What is the difference between a lightweight and an annotated tag?

A lightweight tag is just a name for a commit. An annotated tag is a full object with the tagger, date and message, and can be signed. Use annotated tags for releases; git describe uses them by default. Revise: Git Tag.

35. How does git bisect work?

It finds the commit that introduced a bug by binary search. You mark one good and one bad commit; Git checks out the middle, you test and mark it, and it halves the range each time. 1,000 commits take about 10 steps. git bisect run <test script> automates it. Revise: Git Bisect.

36. What does git blame show?

For each line of a file, the commit, author and date that last changed it. Use it to find why a line exists, then read that commit's message and pull request. -w ignores whitespace changes and --ignore-revs-file skips formatting commits. Revise: Git Blame.

37. Compare GitHub Flow, Gitflow and trunk-based development.

GitHub Flow: short feature branches from main, pull requests, deploy from main. Gitflow: long-lived develop and main, plus feature, release and hotfix branches, suited to scheduled releases. Trunk-based: everyone integrates into main at least daily, with feature flags, suited to continuous delivery. Revise: Git Workflows.

38. What are protected branches?

A hosting platform setting that stops direct pushes or force pushes to important branches such as main, and can require pull request reviews, passing CI checks and signed commits before merging. Revise: Git Best Practices.

39. How do you remove a file from Git but keep it on disk?

git rm --cached <file>, add it to .gitignore, and commit. The file stays in your folder but leaves the repository from that commit on (it remains in older commits). Revise: Git Remove and Move.

40. What is .gitattributes used for?

Per-path rules: line endings (text=auto, eol=lf), marking files as binary, diff drivers (diff=java), merge strategies for specific files, export-ignore for archives, and Git LFS tracking. Unlike core.autocrlf, it is committed, so it applies to everyone. Revise: Git .gitattributes and Line Endings.

Hard Questions (41 to 50)

How Git works inside: senior-level and Deep Dive questions.

41. Describe Git's object model.

Git is a content-addressable store of four object types: blobs (file contents), trees (folder listings that name blobs and other trees), commits (a tree, parents, author, committer and message) and annotated tags. Each object is stored under the hash of its contents. Branches and tags are refs: names that point to commits. Revise: Git Objects: Blob, Tree, Commit and Tag.

42. Does Git store diffs or snapshots?

Snapshots. Each commit points to a tree describing every file as it was. Unchanged files point to the same blob as before, so snapshots are cheap. Diffs are computed when you ask for them. Inside packfiles, Git does store some objects as deltas, but that is a storage detail. Revise: How Git Works: Snapshots, Not Differences.

43. How is a commit ID computed?

It is the SHA-1 hash (or SHA-256 in a SHA-256 repository) of the object's type, size and content: the tree ID, parent IDs, author and committer with timestamps, and message. Changing anything, including a parent, changes the ID, which is why history can't be altered silently. Revise: Build a Commit by Hand.

44. What is the index really?

A binary file, .git/index, listing every tracked path with its mode, blob ID and file system details (size, timestamps) used to detect changes quickly. git add writes blobs and updates entries; git commit turns the index into trees. During a merge conflict, it holds three stages per file. Revise: The Index and Refs.

45. Why does rebase change commit IDs?

A rebased commit has a different parent, and usually a different tree and committer date. Since the ID is a hash of all of that, it is a new commit. The old commits still exist, reachable through the reflog, until garbage collection. Revise: Git Rebase.

46. How do you recover a commit after git reset --hard?

git reflog lists every position HEAD has had. Find the commit before the reset and run git reset --hard <id>, or git branch rescue <id>. Unreachable commits are kept for at least 30 days by default. For lost stashes, git fsck --unreachable helps. Revise: Git Reflog.

47. How does Git find the merge base, and why does it matter?

The merge base is the best common ancestor of the two commits (git merge-base a b). A three-way merge compares each side with it: a part changed on only one side is taken, a part changed differently on both is a conflict. In criss-cross histories there can be several bases, which ort merges into a virtual base. Revise: How Git Merges.

48. What are packfiles and what does git gc do?

New objects are stored as separate compressed "loose" files. A packfile stores many objects in one file, many as deltas against similar objects, with an index for fast lookup. git gc packs loose objects, packs refs, expires old reflog entries and deletes unreachable objects older than two weeks. Revise: Packfiles and Garbage Collection.

49. Submodule or subtree: how do you choose?

A submodule pins another repository's commit; clones must fetch it separately, and updating needs extra commands. A subtree copies the other project into a folder; clones need nothing extra, and git subtree can pull and push changes. For versioned Java libraries, a package manager (Maven, Gradle) beats both. Revise: Git Submodules.

50. How do partial clone and sparse checkout help with huge repositories?

Partial clone (--filter=blob:none) downloads all commits but fetches file contents only when needed. Sparse checkout (git sparse-checkout set <folders>) puts only some folders in the working tree. Together, you download and see only the part of a monorepo you work on, while keeping full history. Revise: Shallow and Partial Clones.

Before the Interview

  1. Explain the three areas and the object model in your own words, with a drawing. Many follow-up questions build on them.
  2. Practice recovery once for real: make a branch, git reset --hard, and get it back with the reflog.
  3. Prepare one story about a Git problem you solved: a conflict, a lost commit or a bad merge.
  4. Try the scenarios on Git Scenario Questions, and the commands on the Git Cheat Sheet.
  5. Rehearse against the clock with the Git Mock Interview: 20 more questions in 40 minutes, with a timer and a scorecard.

Was this tutorial helpful?