Git shows up in almost every DevOps interview, and it shows up for a good reason. Version control sits underneath everything a DevOps engineer touches: infrastructure as code, pipeline definitions, application source, runbooks, and configuration. An interviewer who asks about Git is not testing whether you memorized a list of commands. They are testing whether you understand how teams ship software safely, how you recover when something goes wrong, and whether you can explain your decisions clearly when the pressure is on.
The questions in this guide are the ones that actually come up in DevOps interviews, from junior roles through senior and platform positions. Each comes with a model answer that shows the reasoning behind it, not just a one-line definition, so you can answer in your own words, handle the follow-up question that comes next, and connect the answer to real work you have done.
Read each question, try answering it out loud before reading the model answer, and pay attention to the commands and examples. If you are still building your Git foundations, our Git for DevOps engineers guide covers the daily workflow in detail.
Core Concepts Every Interviewer Tests First
Every Git interview starts with the fundamentals: do you understand what Git actually is, rather than just knowing which commands to type? A candidate who can explain the model behind the commands sounds stronger than one who can only list them.
What is Git, and how is it different from a centralized version control system?
Git is a distributed version control system, and that one word, distributed, is the heart of the answer. In a centralized system, a single server holds the history; if it goes down, nobody can look at history, compare versions, or commit work. In Git, every clone holds the full history, so you can commit, branch, merge, and inspect history entirely offline, needing the network only to share work. For a DevOps engineer, that means a lost central server can be rebuilt from any clone. Git also stores snapshots rather than differences: each commit records the state of every file, with unchanged files referenced rather than copied, which is why Git stays fast even on large repositories.
Explain the working tree, the staging area, and the repository. What is HEAD?
This is the most common opening question, and it is worth answering precisely. The working tree is the set of files you see and edit on disk. The staging area, also called the index, is where you assemble the next commit: when you run git add, you copy the current state of a file into the staging area. The repository is the hidden .git directory, which holds the complete history of commits, branches, tags, and configuration. HEAD is a pointer to the commit you currently have checked out, usually the tip of your current branch. Walk through a change: edit a file in the working tree, stage it with git add, and record it with git commit. That three-stage model underpins every later question about resetting and undoing mistakes.
# See the state of all three areas at once
git status
# Stage a file, then record it as a commit
git add deploy/terraform/main.tf
git commit -m "Add staging environment variables"
What is the difference between git fetch and git pull?
This question appears in almost every interview because it separates candidates who understand remote tracking from those who only type commands. git fetch downloads new commits, branches, and tags from a remote and updates your remote-tracking branches, such as origin/main, but it never changes your working tree or your local branches. It is safe to run at any time because it cannot create a conflict. git pull is git fetch followed by a merge, or a rebase if you configured it that way, which updates your current branch with the new commits. Fetch often and pull deliberately: fetching lets you inspect changes with git log origin/main before integrating them, which is the caution you want when merging a production pipeline or a Terraform configuration.
Branching and Merging: The Questions That Separate Candidates
Branching is where Git interviews get interesting: anyone can create a branch, but interviewers want to know whether you understand what a branch really is, how merges work, and how you handle conflicts. Branching strategy directly affects how safely a team releases software, so this is where a DevOps candidate can stand out.
What is a branch in Git, really?
A branch is a lightweight movable pointer to a commit. That is the whole mechanism, and saying it this way shows you know Git does not copy files when you branch. Creating a branch costs almost nothing, which is why Git workflows encourage branching freely for every feature, fix, and experiment. Committing on a branch moves the pointer forward; switching branches updates your working tree to match. In older systems a branch is a full directory copy, which makes branching expensive. In Git it is so cheap that the question is never whether to branch, but how to organize the branches you create.
What is the difference between a merge and a rebase?
This is the classic Git interview question, and the model answer explains the trade-off rather than picking a side. A merge joins two lines of history with a new merge commit, preserving the full history of both branches exactly as it happened. A rebase takes the commits from your branch and replays them one by one on top of another branch, producing a straight, linear history with no merge commit. Merging is honest about history and safe on shared branches, because it never rewrites commits. Rebasing produces a cleaner, easier-to-read history, but it creates new hashes, so never rebase commits others have based work on. Rule of thumb: rebase your own private feature branch, merge finished work into shared branches, and follow your team’s convention, because consistency matters more than the choice itself.
# Merge a finished feature branch into main
git switch main
git merge feature/add-health-check
# Rebase a private feature branch onto the latest main
git switch feature/add-health-check
git rebase main
How do you resolve a merge conflict?
Conflicts are inevitable in real teams, and interviewers want to see a calm, methodical approach. The mechanics are straightforward: Git marks the conflicting regions with markers, you open the file, decide which changes to keep, remove the markers, stage the file, and complete the merge. But the stronger half of the answer is the judgment. You should never blindly accept one side or the other, and you should never resolve a conflict in code you do not understand without talking to the person who wrote it. For infrastructure code the stakes are higher: a badly resolved conflict in a Terraform file or pipeline definition can change what gets deployed. Check the intent of both changes with git log and git diff, talk to the author when needed, and run tests or a plan step before committing the resolution. You can also mention git merge --abort, which cancels the merge entirely and returns you to the state before you started, which is the right move when you realize you are in over your head.
Rewriting History: Rebase, Amend and Interactive Rebase
Rewriting history is where confident Git users distinguish themselves. The commands are powerful, and interviewers want to know whether you understand exactly what each one does, and when it is safe to use. Rewriting shared history without a second thought is a red flag for any interviewer hiring for a role that touches production.
How do you change the message or contents of your most recent commit?
The answer is git commit --amend, which replaces the most recent commit with a new one holding your staged changes and corrected message: the classic case is a forgotten file or a typo spotted seconds after committing. The warning matters: amend creates a new commit with a new hash, so if you already pushed the original, you will need to force-push, and anyone who pulled the old commit will have a problem. On a private branch, amend freely. On a shared branch, think twice.
What is interactive rebase, and how would you use it to clean up a messy branch?
Interactive rebase, started with git rebase -i, opens an editor where you can reorder commits, squash several into one, reword messages, or drop a commit entirely. Take a feature branch with eight commits, several of them fix-ups like “fix typo”: before opening a pull request you squash them into two or three clean commits, each telling a coherent story. Interviewers love this question because it connects Git skill to review quality: reviewers can follow three well-described commits, but not twenty that interleave half-finished ideas. Interactive rebase rewrites hashes, so like amend it is for private branches only, never for commits already merged into a shared branch.
# Clean up the last four commits on your branch
git rebase -i HEAD~4
# In the editor, change "pick" to "squash" or "fixup"
# for the commits you want to fold into the one above them
What is the golden rule of rebasing?
The golden rule is: never rebase commits that exist outside your own repository, meaning commits pushed to a shared branch that other people may have based work on. Rebasing rewrites commit hashes, so if someone else pulled the originals and you replace them with rewritten versions, their repository ends up holding both versions of the same changes. The official Git documentation explains this in its “recovering from upstream rebase” scenario. The exception is your own short-lived feature branch, where rebasing onto the latest main is normal and expected.
What is the difference between git push –force and git push –force-with-lease?
This question is a favorite for DevOps roles because it tests safety thinking. A plain force push overwrites the remote branch with your local history no matter what has changed on the remote since you last fetched. If a colleague pushed a commit in the meantime, a force push silently discards it. --force-with-lease is the safe version: it only overwrites the remote branch if it still points at the commit you last saw, and it refuses if someone else pushed in the meantime. That refusal is the whole point, because it protects your colleagues’ work from being destroyed by your rewrite. Use --force-with-lease every time you force-push, and note that many teams protect main on the hosting platform so force pushes are impossible there, which is the correct safeguard for a branch that deploys to production.
Recovery and Debugging: Undoing Mistakes Without Panic
Every engineer makes mistakes with Git. Interviewers want to know whether you can recover calmly, without losing work or making things worse. These questions test your knowledge of Git’s safety nets, and your judgment about which one to reach for.
What is the difference between git reset –soft, –mixed, and –hard?
This is one of the most frequently asked Git interview questions. All three move HEAD to a different commit, and they differ in what else they touch. --soft moves HEAD but leaves the staging area and the working tree untouched, so your changes remain staged and ready to commit again. Use it to redo a commit, for example to combine several recent commits into one. --mixed is the default: it moves HEAD and resets the staging area to match, but leaves your working tree files alone, so your changes become unstaged but nothing is lost from disk. Use it to uncommit and re-stage more carefully. --hard moves HEAD, resets the staging area, and overwrites your working tree files to match, which discards uncommitted changes permanently. Use it only when you are certain you want to throw work away, and never run it without first checking git status for uncommitted work you need. The official git-reset reference documents these modes exactly this way, including the default of --mixed.
# Undo the last commit but keep the changes staged
git reset --soft HEAD~1
# Undo the last commit and unstage the changes (default mode)
git reset --mixed HEAD~1
# Discard the last commit AND all uncommitted changes
git reset --hard HEAD~1
What is the difference between git revert and git reset?
Reset moves a branch pointer backwards and can erase commits, while revert creates a brand new commit that undoes an earlier commit, leaving the original in history. That difference decides which one you use: reset is for private work nobody else has seen, revert is for shared history others have already pulled. For example, if a bad configuration change was merged to main and deployed, you do not reset main, because other engineers have already pulled it. You revert the commit, producing a clean, reviewable change, and the history honestly records both the mistake and the fix.
You deleted a branch by accident, or a reset removed commits you needed. How do you get them back?
Experienced Git users smile at this question, because Git rarely loses anything. The reflog is a log of every position HEAD has pointed to, including commits no longer reachable from any branch. Running git reflog shows those positions, and once you find the hash of the lost commit you can restore it with git reset --hard to that hash, or create a new branch at it. For a deleted branch, the same approach works: find the hash of its tip in the reflog and run git branch recovered-branch <hash>. Two honest limits: the reflog is local, so it cannot help if the commits were never in your clone, and entries eventually expire, so recover promptly. Run git reflog before doing anything else after a mistake, because every further command can make recovery harder.
Workflows and Collaboration: How Teams Actually Use Git
Knowing Git commands is not the same as knowing how a team uses Git. The branching workflow a team chooses decides how code gets reviewed, how releases are cut, and how quickly a bad change can be rolled back, so the strongest answers connect Git mechanics to team outcomes.
Compare Git Flow, GitHub Flow, and trunk-based development. Which would you choose?
Git Flow uses long-lived develop and main branches plus feature, release, and hotfix branches, with strict rules about what merges where. It suits teams shipping versioned releases on a fixed schedule while maintaining several versions at once. GitHub Flow is simpler: everything branches from main, is reviewed in a pull request, and merges back to a main that is always deployable. It suits teams that deploy continuously. Trunk-based development goes further: developers commit small changes to main, or to very short-lived branches lasting a day or less, behind feature flags. It suits mature teams with strong tests and continuous deployment. A model answer does not declare one winner: it explains the trade-off between structure and speed, and says the choice depends on release cadence, team size, and test-suite strength. The strongest closing point for a DevOps interview is that trunk-based development only works when the pipeline is trustworthy, because with no release branch to hide behind, a broken main means a broken deployment.
What makes a good pull request?
A good pull request is small, focused, and reviewable. Small means it does one thing, so a reviewer can hold the whole change in their head. Focused means it does not mix a feature with a refactor and a formatting change. Reviewable means a clear title, a description explaining why the change exists rather than just what it does, and a link to the issue it addresses. A strong answer also covers the process: keep the branch up to date with main so the review reflects the real merge, respond to comments with new commits rather than rewriting history mid-review, and treat CI checks as a gate rather than a suggestion. For infrastructure changes, the answer should mention including the output of a plan or dry run in the description, because a reviewer cannot meaningfully approve a Terraform change without seeing what it will do. Our GitHub Actions tutorial walks through building the workflow that runs those checks on every pull request.
How do you keep secrets out of a Git repository?
This is close to a pass-or-fail question for DevOps candidates, because leaked secrets are among the most common and damaging mistakes in the field. Start with prevention: secrets belong in a secret manager or environment variables, never in code, and .gitignore should exclude credential files, environment files, and private keys from the start. The next layer is detection: pre-commit hooks and repository scanning can catch a secret before it is committed, and hosting platforms can scan pushed commits for exposed credentials. Most important is what to do when a secret does get committed: removing it from the latest commit is not enough, because it remains in history, and anyone who cloned the repository still has it. The correct response is to revoke and rotate the credential immediately, then remove it from history, then tell the team. A candidate who says they would just delete the file and commit the deletion has misunderstood the problem, and interviewers will notice.
Git in CI/CD and DevOps Pipelines
For a DevOps engineer, Git is not just a developer tool. It is the trigger, the source of truth, and the audit trail for the whole delivery pipeline. Interviewers want to know whether you think of Git as part of the system, not as a separate tool developers use before the real work starts.
How does a CI/CD pipeline use Git?
A pipeline starts when Git reports a change: a push, a pull request, or a new tag is an event, and the CI system responds by checking out the code at that exact commit. The commit hash makes it reproducible: the pipeline records which commit it built, so a failed deployment traces back to a precise state of the code, and a successful one can be rebuilt from the same commit at any time. That is why pipelines check out a specific commit rather than a branch name, and why the deployment artifact is tagged with the commit hash or a version derived from it. Our CI/CD pipeline guide explains each stage from commit to production in detail.
What is a shallow clone, and why do pipelines use it?
A shallow clone, created with git clone --depth 1, downloads only the most recent commit instead of the full history. Pipelines use it because it is fast: a large repository with years of history can take minutes to clone in full, while a shallow clone takes seconds, and a pipeline that runs hundreds of times a day saves a meaningful amount of time and network traffic. The trade-off: a shallow clone cannot run commands that need history, such as git log, git describe, or git bisect, and tools that compute a version from the commit count will fail. So shallow clones are the right default for build jobs that only need current code, while jobs that need history, such as a release job generating a changelog, should use a full clone or a greater fetch depth.
# Fast checkout for a build job: latest commit only
git clone --depth 1 https://github.com/example/app.git
# A release job needs history, so fetch more of it
git fetch --unshallow
git describe --tags
What is GitOps, and how is it different from running scripts from a pipeline?
GitOps uses Git as the single source of truth for a system’s desired state, with an automated agent that continuously compares live state against what Git declares and reconciles any difference. The key difference from a traditional pipeline is direction: a traditional pipeline runs a script once when a push happens and then stops, while a GitOps agent keeps watching, so a manual change to the live system is detected as drift and either restored or alerted on. Interviewers ask this because it tests whether you understand declarative thinking, which is central to modern DevOps. The benefits follow from the model: every change goes through a pull request, so it is reviewed and auditable; rolling back means reverting a commit, a workflow every engineer knows; and the repository history is the system history. The honest limitation is that GitOps fits declarative systems such as Kubernetes well, and imperative one-off operations poorly. For the definitive reference, the Pro Git book covers branching, distributed workflows, and internals in depth, free to read online.
Frequently Asked Questions
How should I prepare for a Git interview as a DevOps engineer?
Practice in a real repository, not from a memorized list: create branches, cause a merge conflict and resolve it, rewrite a messy branch with interactive rebase, delete a branch and recover it from the reflog, and run a bisect on a repository with a planted bug. Practice explaining each step out loud, because the interview tests your explanation as much as your commands. And connect answers to your own experience: interviewers remember “we used trunk-based development and it worked because our pipeline caught regressions early” far better than a recited definition.
What is the difference between git fetch and git pull?
Fetch downloads new commits and updates your remote-tracking branches, but never touches your working tree or local branches. Pull is fetch followed by a merge or rebase into your current branch. Fetch first to inspect changes before integrating them; pull when you are ready to bring them into your work.
Should I merge or rebase?
Merge finished work into a shared branch, because it preserves full history and never rewrites commits others have seen. Rebase your own private feature branch to keep it tidy and up to date with main. Follow your team’s convention either way: a team that mixes the two without a rule ends up with a history nobody can read.
How do I recover a deleted branch or a lost commit?
Run git reflog to find the hash of the lost commit, then create a new branch at that hash with git branch recovered-branch <hash> or reset your current branch to it. Act quickly, because reflog entries expire, and avoid further history-changing commands until you have recovered what you need.
What Git topics matter most for DevOps roles?
Branching strategy, merge versus rebase judgment, recovery with reset, revert, and reflog, and Git in CI/CD: shallow clones, tags and releases, secrets handling, and GitOps. Interviewers care less about obscure commands than about whether your Git habits make deployments safer, rollbacks easier, and incidents shorter.
Conclusion
Git interview questions are rarely about Git alone. A question about merging is really about how you work with a team; a question about reset is really about how you handle mistakes; a question about pipelines is really about whether you understand that the repository is the foundation of the whole delivery system. The candidates who do best answer each question with the command, the reasoning behind it, and a short story from real work.
Use this guide as a practice plan rather than a script. Work through the commands in a test repository until they feel natural, prepare one or two real examples for each section, and go into the interview ready to explain your thinking. If you can do that, the Git portion becomes one of the easiest parts of the day, and a strong signal that you will be safe with their code, their pipelines, and their production systems.



