Git and GitHub course menu
Advanced Git Interview Questions and Answers
Advanced Git interview questions with answers: blobs, trees and commits, the reflog, bisect, hooks, removing secrets from history, monorepos and shallow clones, CI and merge strategies.
On this page
- Internals
- 1. How does Git store data?
- 2. Does Git store differences or snapshots?
- 3. Why does changing an old commit change all the later IDs?
- 4. What is the index?
- 5. What do git gc and packfiles do?
- Recovery and debugging
- 6. What is the reflog, and how do you use it?
- 7. How would you recover a deleted branch?
- 8. What is git bisect?
- 9. How do you find when a line of code was changed?
- 10. A secret was pushed to a public repository. What do you do?
- Merging in depth
- 11. What is a three-way merge?
- 12. What do "ours" and "theirs" mean during a rebase?
- 13. What is the difference between two-dot and three-dot diffs?
- 14. How do you revert a merge commit?
- 15. What is git rerere?
- Scale and automation
- 16. How do you handle a very large repository?
- 17. Monorepo or many repositories?
- 18. Submodules or subtrees?
- 19. How would you enforce standards on a team?
- 20. How do you verify who made a commit?
Senior interviews look at how Git works inside, how you recover from problems, and how you run Git in a large team.
Internals#
1. How does Git store data?#
Show solution
As a content-addressed database of four object types: blobs (file contents), trees (folders), commits (a tree, parents and metadata) and tags. Each object's ID is a hash of its content. Branches and HEAD are small references pointing to commits.
2. Does Git store differences or snapshots?#
Show solution
Snapshots. Each commit points to a complete tree, and unchanged files reuse the same blobs. Differences are calculated when you ask for them. Packfiles do use delta compression, but only as a storage optimisation.
3. Why does changing an old commit change all the later IDs?#
Show solution
A commit's ID depends on its content, including its parent's ID. A new ID for one commit changes its child, and so on along the chain. This makes history tamper-evident, and it is why rebasing creates new commits.
4. What is the index?#
Show solution
The binary file .git/index, which implements the staging area. It lists the paths and blob IDs for the next commit, plus file metadata so that Git can quickly detect changes.
5. What do git gc and packfiles do?#
Show solution
git gc packs loose objects into packfiles using delta compression, and removes unreachable objects once they are old enough. Git runs it automatically from time to time.
Recovery and debugging#
6. What is the reflog, and how do you use it?#
Show solution
A local log of every position of HEAD and of each branch. After a bad reset or rebase, find the earlier position with git reflog, and return with git reset --hard HEAD@{n}, or make a branch there. It is local only, and entries expire.
7. How would you recover a deleted branch?#
Show solution
Find its last commit in git reflog, or in the output of git branch -D, then run git branch name <id>. If someone else has a copy, or it is still on the remote, you can fetch it from there.
8. What is git bisect?#
Show solution
A binary search through history for the commit that introduced a bug. You mark one good and one bad commit, and Git checks out commits in between until it finds the first bad one. git bisect run <command> automates it with a test.
9. How do you find when a line of code was changed?#
Show solution
git blame shows the last commit for each line. git log -S "text" finds commits that added or removed the text. git log -L 10,20:file follows the history of a range of lines.
10. A secret was pushed to a public repository. What do you do?#
Show solution
Revoke or rotate the secret first, because it must be assumed to be compromised. Then, if needed, remove it from history with git filter-repo, force-push, and have everyone re-clone. Add prevention: secret scanning, push protection, .gitignore and hooks.
Merging in depth#
11. What is a three-way merge?#
Show solution
Git compares the two branch tips with their common ancestor (the merge base). If only one side changed a part, it takes that change. If both changed it differently, that is a conflict. ort is Git's default merge strategy.
12. What do "ours" and "theirs" mean during a rebase?#
Show solution
They seem reversed. In a merge, "ours" is your current branch. During a rebase, Git is replaying your commits onto the other branch, so "ours" is that base, and "theirs" is your commit being applied.
13. What is the difference between two-dot and three-dot diffs?#
Show solution
git diff main..feature compares the two tips directly, the same as git diff main feature. git diff main...feature compares feature with its merge base with main, so it shows only what the branch changed. For git log, main..feature lists commits in feature that are not in main.
14. How do you revert a merge commit?#
Show solution
git revert -m 1 <merge-commit>, where -m 1 keeps the first parent's side. Note that merging the same branch again later will not bring those changes back unless you revert the revert first.
15. What is git rerere?#
Show solution
"Reuse recorded resolution". When enabled with rerere.enabled, Git remembers how you resolved a conflict, and applies the same fix if it appears again, which helps with long rebases.
Scale and automation#
16. How do you handle a very large repository?#
Show solution
Use shallow clones (--depth 1) in CI, partial clones (--filter=blob:none), sparse checkout to fetch only some folders, Git LFS for big binaries, and scheduled maintenance with git maintenance.
17. Monorepo or many repositories?#
Show solution
A monorepo makes shared code and atomic changes across projects easy, but needs tooling to keep builds and clones fast. Many repositories give clear ownership and separate releases, but make cross-project changes and shared versions harder. The right choice depends on team structure and tooling.
18. Submodules or subtrees?#
Show solution
A submodule stores a pointer to a commit in another repository, so its code stays separate, but everyone needs extra commands to update it. A subtree copies the code into your repository, which is simpler for users but harder to contribute back.
19. How would you enforce standards on a team?#
Show solution
Protect main with required reviews and status checks, run linting and tests in CI, use CODEOWNERS for required reviewers, and add local hooks for quick feedback. Hooks can be skipped, so CI must be the real gate.
20. How do you verify who made a commit?#
Show solution
Author details can be set to anything, so sign commits with GPG or SSH keys (git commit -S). GitHub marks verified commits, and branch rules can require them.
Finished this lesson?