Top 30 Git Interview Questions & Answers
Top 30 Git interview questions and answers for developers: commits, branches, merge vs rebase, resolving conflicts, reset vs revert, stash and workflows.
Git Basics
Q1. What is Git?
A distributed version control system. Every developer has the full history locally, so most work is fast and possible offline, and changes are shared through remotes such as GitHub.
Q2. What is the difference between Git and GitHub?
Git is the version control tool. GitHub is a hosting service for Git repositories that adds pull requests, issues, code review and CI. GitLab and Bitbucket are alternatives.
Q3. What are the three states of a file in Git?
Modified (changed in the working directory), staged (added to the index with git add) and committed (saved in the repository history).
Q4. What is the staging area?
The index: a place where you collect the exact changes for the next commit, so one commit can contain only part of your edits.
Q5. What is the difference between git fetch and git pull?
git fetch downloads new commits from the remote without changing your branch. git pull fetches and then merges or rebases them into your current branch.
Q6. What is the difference between git clone and git init?
git init creates a new empty repository in the current folder. git clone copies an existing remote repository, with its full history.
Q7. What is a commit hash?
A unique ID, a SHA-1 or SHA-256 hash of the commit's content and parent, such as a1b2c3d. Changing anything in history produces new hashes.
Q8. What is HEAD?
A pointer to the commit you have checked out, usually through the current branch. A detached HEAD points directly at a commit, not a branch.
Q9. What is .gitignore?
A file listing patterns, such as node_modules/ or .env, that Git should not track. It does not affect files already committed; remove them with git rm --cached.
Q10. What makes a good commit message?
A short summary line in the imperative mood, such as "Fix login redirect loop", then a blank line and an explanation of why the change was made.
Branching, Merging & Rebasing
Q11. What is a branch in Git?
A lightweight movable pointer to a commit. Creating one is instant, so you can isolate each feature or fix and merge it later.
Q12. What is the difference between git merge and git rebase?
Merge joins two branches with a merge commit and keeps history as it happened. Rebase replays your commits on top of another branch, which gives a straight history but rewrites commit hashes.
Q13. When should you not rebase?
Never rebase commits that others have already pulled, such as on a shared branch. It rewrites history and forces everyone else to fix their copies.
Q14. What is a fast-forward merge?
When the target branch has no new commits, Git just moves its pointer forward to the other branch, with no merge commit. --no-ff forces a merge commit anyway.
Q15. How do you resolve a merge conflict?
Open the conflicting files, choose the right content between the <<<<<<<, ======= and >>>>>>> markers, remove the markers, then git add the files and finish the merge or rebase.
Q16. What is interactive rebase?
git rebase -i lets you reorder, squash, edit, reword or drop commits before sharing them, to clean up a branch's history.
Q17. What is squashing commits?
Combining several commits into one, with interactive rebase or a squash merge on GitHub, so a feature lands as a single clean commit.
Q18. What does git cherry-pick do?
It applies the changes from a specific commit onto the current branch as a new commit, for example to copy a hotfix to a release branch.
Q19. What are common branching strategies?
Trunk-based development, with short-lived branches merged into main often, is common today. GitHub Flow uses a feature branch per change plus pull requests. Git Flow adds develop, release and hotfix branches.
Q20. What is a pull request?
A request on GitHub or a similar host to merge one branch into another. It is where code review, discussion and CI checks happen before merging.
Undoing Changes & Advanced Git
Q21. What is the difference between git reset and git revert?
git revert creates a new commit that undoes an earlier one, so history is kept and it is safe on shared branches. git reset moves the branch pointer back and rewrites history, for local work.
Q22. What is the difference between reset --soft, --mixed and --hard?
--soft moves the branch and keeps changes staged. --mixed (the default) keeps them unstaged. --hard throws the changes away completely.
Q23. How do you undo the last commit but keep the changes?
git reset --soft HEAD~1 removes the commit and leaves its changes staged, ready to commit again.
Q24. How do you change the last commit?
Stage any extra changes and run git commit --amend, which also lets you edit the message. Avoid this on commits already pushed and shared.
Q25. What does git stash do?
It saves uncommitted changes and cleans the working directory, so you can switch branches. git stash pop restores the latest stash.
Q26. What is git reflog?
A local log of every position HEAD has been at, including after resets and rebases. It is how you recover commits that seem lost.
Q27. How do you discard changes to a file?
git restore <file> discards working-directory changes, and git restore --staged <file> unstages a file. Older Git used git checkout -- <file>.
Q28. What does git bisect do?
It finds the commit that introduced a bug with a binary search: you mark commits good or bad, and Git checks out the midpoint until it finds the first bad commit.
Q29. What is the difference between git push --force and --force-with-lease?
--force overwrites the remote branch regardless. --force-with-lease refuses if the remote has commits you have not seen, so you do not erase a teammate's work.
Q30. How do you remove a secret that was committed?
Rotate the secret first, since it must be considered leaked. Then rewrite history with git filter-repo or BFG and force-push, and ask everyone to re-clone.