Git is used by every software team, so Git questions appear in developer, DevOps and tester interviews. These Git interview questions cover repositories, commits and branches for freshers, then merge vs rebase, resolving conflicts, undoing changes, branching strategies and recovering lost work for experienced developers.
30 questions · Updated
Reading answers is not the same as saying them.Practise Git questions out loud and get a score, what you missed and a model answer for each one.
Practise free with AIGit Interview Questions: 30 questions with model answers on this page.
Basic Git interview questions
Fundamentals, definitions and simple scenarios. Good for freshers and warm-ups.
1. What is Git, and how is it different from GitHub?
Git is a distributed version control system that tracks changes to files, so every developer has a full copy of the project history and can work offline. GitHub, GitLab and Bitbucket are hosting platforms for Git repositories that add collaboration features such as pull requests, code review, issues and CI/CD.
2. What are the working directory, staging area and repository?
Answer flow · 4 steps
1
The working directory holds your files as you edit them.
2
The staging area (index) holds the changes you have chosen for the next commit with git add.
3
The repository stores committed snapshots with git commit.
4
This lets you commit related changes together even if you edited many files.
3. What is the difference between git fetch and git pull?
git fetch downloads new commits and branches from the remote but does not change your working branch, so you can review changes first. git pull runs fetch and then merges (or rebases, if configured) the remote branch into your current branch.
4. What are the basic Git commands for daily work?
git clone copies a remote repository, git status shows changed files, git add stages changes, git commit -m saves a snapshot, git pull brings in remote changes, git push uploads commits, git switch -c creates and switches to a branch, git log shows history and git diff shows changes. git init creates a new repository.
5. What is .gitignore?
Answer flow · 3 steps
1
.gitignore lists files and patterns Git should not track, such as node_modules, build output, log files, .env files with secrets and editor settings.
2
Patterns support wildcards, directories and negation with !.
3
It only affects untracked files; a file already committed must be removed from the index with git rm --cached to stop tracking it.
6. What makes a good commit message?
Answer flow · 3 steps
1
A short summary line under about 50 to 72 characters in the imperative mood, such as "Fix login redirect after password reset", then a blank line and a body explaining what changed and why if needed.
2
Each commit should contain one logical change.
3
Many teams use Conventional Commits, such as feat:, fix: and docs:, which can generate changelogs automatically.
7. How do you view history and changes?
git log shows commits, with useful options such as --oneline, --graph, --author, -p (show changes) and -- path for one file. git diff shows unstaged changes, git diff --staged shows staged changes, git show <commit> shows one commit, and git blame shows who last changed each line, which helps understand why code was written.
8. How do you create and switch branches?
git branch lists branches, git switch -c feature/login creates a branch and switches to it (git checkout -b does the same in older Git), git switch main goes back, and git branch -d deletes a merged branch. Branches are cheap pointers to commits, so creating one for every feature or fix is normal.
9. What is HEAD?
Answer flow · 3 steps
1
HEAD is a pointer to the commit you currently have checked out, usually through the current branch, so it moves forward as you commit.
2
HEAD~1 means the parent commit and HEAD~3 three commits back.
3
If you check out a specific commit instead of a branch, you are in a detached HEAD state.
10. What are origin and upstream?
A remote is a named link to another copy of the repository. origin is the default name for the repository you cloned from. When you fork a project, upstream conventionally refers to the original repository, so you fetch updates from upstream and push to your fork (origin). git remote -v lists remotes, and an upstream branch is the remote branch your local branch tracks.
11. How does a pull request workflow work?
You create a branch, commit your changes, push the branch and open a pull request (merge request on GitLab) describing what changed and why. Automated checks run tests and linting, teammates review the code and request changes, you push fixes, and once approved the PR is merged into the main branch, often with a squash or merge commit, and the branch is deleted.
Intermediate Git interview questions
Applied problems, trade-offs and questions about your own projects.
12. What is the difference between merge and rebase?
Answer flow · 3 steps
1
Merge combines branches by creating a merge commit, preserving the exact history of both.
2
Rebase moves your commits on top of another branch, rewriting them to create a straight, linear history.
3
I rebase my own local feature branch to keep it up to date, but never rebase shared branches that others have pulled, because it rewrites history they depend on.
13. How do you resolve a merge conflict?
Answer flow · 3 steps
1
Git marks conflicting sections in the files with <<<<<<<, ======= and >>>>>>>.
2
I open each file, decide what the correct combined code should be, remove the markers, then run tests, stage the files with git add and complete the merge or continue the rebase.
3
Talking to the other developer helps when it is unclear which change is right.
14. What is the difference between git reset, git revert and git restore?
git revert creates a new commit that undoes an earlier commit, which is safe for shared branches. git reset moves the branch pointer back: --soft keeps changes staged, --mixed keeps them unstaged and --hard discards them, so it should only be used on local commits. git restore discards changes in the working directory or unstages files.
15. What is git stash?
git stash saves uncommitted changes temporarily and cleans the working directory, so you can switch branches, for example to fix an urgent bug. git stash pop reapplies the latest stash and removes it, git stash apply keeps it, and git stash list shows saved stashes. Including -u stashes untracked files too.
16. What is interactive rebase used for?
Answer flow · 3 steps
1
git rebase -i lets you rewrite a series of local commits before sharing them: reorder commits, squash several into one, reword messages, edit a commit or drop one.
2
It is used to turn messy work-in-progress commits into a clean history before opening a pull request.
3
It rewrites commit hashes, so it should only be used on commits that others have not based work on.
17. What is the difference between a fast-forward merge, a merge commit and a squash merge?
Answer flow · 3 steps
1
A fast-forward merge simply moves the branch pointer forward when there are no diverging commits, leaving a linear history.
2
A merge commit (--no-ff) records that a branch was merged and keeps all its commits.
3
A squash merge combines all the branch's commits into a single new commit on the target branch, giving a clean history but losing individual commits.
18. How do you change the last commit?
git commit --amend replaces the last commit with a new one, letting you fix the message or add forgotten files that you have staged. git commit --amend --no-edit keeps the message. Because it creates a new commit hash, you should only amend commits that have not been pushed, or force-push with --force-with-lease on your own branch.
19. What are tags, and how are they used for releases?
Answer flow · 4 steps
1
A tag is a fixed name for a specific commit, used to mark releases such as v2.1.0.
2
Annotated tags (git tag -a v2.1.0 -m "Release 2.1.0") store the author, date and message and are recommended for releases; lightweight tags are simple pointers.
3
Tags must be pushed explicitly with git push origin v2.1.0.
4
Versions usually follow semantic versioning.
20. What is a detached HEAD, and how do you recover from it?
Answer flow · 3 steps
1
Detached HEAD means you checked out a commit, tag or remote branch directly instead of a local branch, so new commits do not belong to any branch and can be lost when you switch away.
2
If you made commits you want to keep, create a branch at that point with git switch -c new-branch.
3
If you switched away already, git reflog shows the commit hash.
21. What are Git hooks?
Answer flow · 3 steps
1
Hooks are scripts Git runs automatically at certain events, such as pre-commit (before a commit is created), commit-msg (to check the message format) and pre-push.
2
Teams use them to run linters, formatters and quick tests, often managed with Husky and lint-staged in JavaScript projects or the pre-commit framework.
3
Server-side checks in CI are still needed because local hooks can be skipped.
22. What do you do if you accidentally commit a secret?
Answer flow · 3 steps
1
First treat the secret as compromised and rotate it immediately, because it may already have been copied.
2
Then remove it from the files, and if it was pushed, rewrite history with git filter-repo or BFG and force-push, and ask collaborators to re-clone.
3
Prevent repeats with .gitignore, secret scanning tools such as gitleaks in pre-commit hooks and GitHub push protection.
High level Git interview questions
System design, deep internals, leadership and tough follow-ups.
23. Which branching strategies have you used?
Answer flow · 3 steps
1
GitHub Flow uses a main branch that is always deployable and short-lived feature branches merged through pull requests, which suits continuous deployment.
2
Git Flow has develop, feature, release and hotfix branches, which suits scheduled releases.
3
Trunk-based development has everyone merge small changes to main frequently, using feature flags to hide unfinished work, which suits mature CI/CD.
24. How do you recover a commit or branch you accidentally deleted?
Answer flow · 3 steps
1
git reflog lists where HEAD and branches have pointed recently, including commits no longer on any branch.
2
I find the commit hash in the reflog and recreate the branch with git branch recovered <hash>, or reset to it.
3
Reflog entries are kept for about 90 days by default, so most mistakes can be undone.
25. What are git cherry-pick and git bisect?
git cherry-pick applies a specific commit from one branch onto another, for example copying a bug fix to a release branch. git bisect finds the commit that introduced a bug using binary search: you mark a known good and a known bad commit, and Git checks out commits in between for you to test, or runs a test script automatically, until it identifies the culprit.
26. How does Git store data internally?
Answer flow · 3 steps
1
Git is a content-addressable store of objects identified by their SHA hash: blobs store file contents, trees store directory listings pointing to blobs and other trees, commits point to a tree, parent commits, author and message, and annotated tags point to commits.
2
Branches and tags are simply files containing a commit hash.
3
Because identical content has the same hash, Git stores it only once.
27. How do you work with very large repositories?
Use partial clone (--filter=blob:none) to download file contents only when needed, shallow clones (--depth) for CI, sparse checkout to check out only the folders you work on, Git LFS for large binary files such as images and videos, and git maintenance for background optimisation. Monorepo tools add build caching on top.
28. What is git worktree?
Answer flow · 3 steps
1
git worktree lets you check out several branches of the same repository into different folders at the same time, sharing one .git store.
2
You can, for example, fix an urgent bug on main in a second folder without stashing or committing unfinished work in your feature branch.
3
It is lighter than cloning the repository again.
29. Why sign commits, and how?
Answer flow · 3 steps
1
Anyone can set any name and email in Git, so signatures prove that a commit really came from a trusted person or system.
2
Commits and tags can be signed with GPG, SSH keys or S/MIME (git config commit.gpgsign true), and platforms such as GitHub show them as verified.
3
Some organisations require signed commits on protected branches as part of supply chain security.
30. How do you protect the main branch in a team?
Use branch protection rules or rulesets: require pull requests with at least one approval, require status checks such as tests and linting to pass, require branches to be up to date, block force pushes and deletion, optionally require signed commits and linear history, and use CODEOWNERS so the right people automatically review changes to their areas.
Ready to test yourself?Pick your topics and level, answer by voice or text, and get instant feedback. Free.