Home › Interview questions › Git
Git Interview Questions & Answers
69 Git interview questions in 4 levels — junior to senior, plus 13 production scenarios. Read 3 free answers with real-world analogies.
Free sample answers
What is Git, and what problem does version control actually solve?
One-liner
Git tracks every change made to a set of files over time, letting you see exactly what changed, who changed it, and go back to any previous version whenever you need to.
Real-world analogy
It's like a video game with unlimited save points — you can always go back to an earlier save if something goes wrong, instead of being stuck with whatever the current state happens to be.
Detailed explanation
- Without version control, teams often end up with files named 'final_v2_actually_final.docx,' with no real way to know exactly what changed between versions.
- Git keeps a complete history of every change, letting you compare any two points in time and see exactly what was added, removed, or modified.
- Because the full history is stored, you can confidently experiment or make a risky change, knowing you can always get back to a known-good state.
Scenario
A developer accidentally breaks a working feature while making a change, and without version control, there'd be no clean way to get back to the last working version. With Git, they simply check the log, find the last good commit, and can compare or revert to it directly.
Best practices
- Commit your work regularly, rather than making one giant change and committing it all at once.
- Write a clear commit message describing what actually changed and why.
- Get comfortable with git log early — it's how you actually see your project's history.
What is a detached HEAD state in Git, and how does someone actually end up in one?
One-liner
A detached HEAD means you've checked out a specific commit directly, rather than a branch, so any new commits you make won't actually belong to any branch and could easily be lost if you switch away without saving them.
Real-world analogy
It's like standing on a specific numbered step of a staircase rather than actually being attached to a room at the top — you can look around from there, but if you walk away without noting exactly where you were, you might not easily find that specific spot again.
Detailed explanation
- Running git checkout <commit-hash> (rather than a branch name) puts you into a detached HEAD state, since you're now pointing directly at a specific commit instead of a branch.
- Any new commits made in this state exist, but aren't attached to any actual branch, meaning they can become genuinely difficult to find again once you check out something else.
- If you want to actually keep work done in a detached HEAD state, creating a new branch from that exact point before switching away preserves it properly.
Scenario
A developer checks out an old commit to look at some previous code, makes a small experimental change, and then switches back to their main branch without thinking. Later, they realize that experimental change is genuinely gone, since it was never attached to any actual branch — creating a branch before switching away would have preserved it properly.
Best practices
- Create a new branch immediately if you make any change you actually want to keep while in a detached HEAD state.
- Understand that checking out a specific commit hash (rather than a branch name) puts you in this exact state.
- Check git status, which clearly warns you when you're currently in a detached HEAD state.
You've already pushed a commit containing a sensitive secret to a shared remote. How do you respond?
One-liner
Rotate the exposed secret immediately, since it's now genuinely compromised regardless of what you do to the repository afterward, and only then consider rewriting history to actually remove it, understanding that anyone who already pulled has the old, exposed version.
Real-world analogy
It's like discovering a spare key was accidentally mailed out to a mailing list — you don't just try to recall the letter, you actually change the locks, since you genuinely can't be sure who's already opened it.
Detailed explanation
- Rotating the actual secret is the single most important, immediate step, since it neutralizes the real risk regardless of anything else you do to the repository afterward.
- Rewriting history to remove the secret (using a tool like git filter-repo) is a genuinely good additional step, but it's disruptive, since every existing clone needs to actually be replaced with the rewritten version.
- Treat any secret that's ever been pushed to a shared remote as permanently compromised, since Git history persists and can be retrieved even after a later fix.
Scenario
A team discovers a database password was committed and pushed weeks earlier. Rotating the password immediately closes the actual real exposure, and the team decides rewriting history isn't worth the disruption at this point, since the credential itself has already been fully rotated and is no longer valid regardless of what remains in the old history.
Best practices
- Rotate the exposed secret immediately as your first, most important action.
- Weigh whether rewriting history is genuinely worth the disruption, now that the actual credential itself is no longer valid.
- Add a pre-commit hook or secret-scanning tool to prevent this exact class of mistake from happening again.
A preview of the 69 questions in the Git guide
Junior (20)
- What is Git, and what problem does version control actually solve?
- What is a Git repository, and what's actually inside one?
- What's the difference between git init and git clone?
- What are the three main areas in Git — working directory, staging area, and repository — and how does a change actually move between them?
+ 16 more Junior questions, with full answers, in the guide.
Mid-level (20)
- What's the practical difference between rebasing and merging, and when would you actually choose one over the other?
- What is interactive rebase, and what can you actually do with it?
- What does git cherry-pick actually do, and what's a genuine practical use for it?
- What are Git tags, and how are they actually different from branches?
+ 16 more Mid-level questions, with full answers, in the guide.
Senior (16)
- How would you design a branching strategy for a large team with many developers working simultaneously?
- How would you decide between a monorepo and multiple separate repositories for a large organization?
- How would you manage Git at scale for a repository that's grown genuinely very large, with a long history and many large files?
- Why would an organization require signed commits, and how does commit signing actually work?
+ 12 more Senior questions, with full answers, in the guide.
Scenario (13)
- You accidentally committed a sensitive file (like an API key) and haven't pushed yet. How do you fix it before it ever reaches the remote?
- You've already pushed a commit containing a sensitive secret to a shared remote. How do you respond?
- You're resolving a merge conflict, but the conflict markers themselves seem to be causing further confusion in the file. How do you make sure you've actually resolved it correctly?
- You're in a detached HEAD state and made a change you want to keep, but you're not sure how to actually save it properly. What do you do?
+ 9 more Scenario questions, with full answers, in the guide.
Get the full DevOps / SRE Interview Guide