Git and GitHub course menu
Intermediate Git Interview Questions and Answers
Intermediate Git interview questions with answers: rebase vs merge, reset vs revert, stash, cherry-pick, fast-forward merges, tags, squash merges, branching workflows and pull request practice.
On this page
- History
- 1. What is the difference between merge and rebase?
- 2. What is the golden rule of rebasing?
- 3. What is the difference between reset and revert?
- 4. Explain the three modes of git reset.
- 5. What is a fast-forward merge?
- 6. What is a squash merge?
- 7. What is the difference between amend and rebase -i?
- Everyday tools
- 8. What is git stash?
- 9. What is cherry-pick, and when do you use it?
- 10. How do you resolve a conflict during a rebase?
- 11. What is detached HEAD?
- 12. What is the difference between annotated and lightweight tags?
- 13. What is the difference between git diff, git diff --staged and git diff HEAD?
- Teamwork
- 14. Describe a typical pull request workflow.
- 15. Compare GitHub Flow, Git Flow and trunk-based development.
- 16. What makes a good commit?
- 17. Your push was rejected. What do you do?
- 18. What is the difference between --force and --force-with-lease?
- 19. You committed to main by mistake. How do you fix it?
- 20. What is an upstream branch?
These questions test whether you understand how Git behaves in real team work, not just the commands.
History#
1. What is the difference between merge and rebase?#
Show solution
Both combine work from two branches. merge keeps the history as it happened, and adds a merge commit if the branches diverged. rebase replays your commits on top of the other branch, giving a straight line, but it creates new commits with new IDs. Rebase your own branches, and do not rebase commits others have based work on.
2. What is the golden rule of rebasing?#
Show solution
Do not rebase commits that have been shared. Rebasing replaces commits with new ones, so anyone who built on the old ones ends up with duplicated, conflicting history.
3. What is the difference between reset and revert?#
Show solution
git reset moves the branch pointer back, removing commits from the branch, so it rewrites history. git revert adds a new commit that undoes an earlier one, and history stays intact. Use revert on shared branches.
4. Explain the three modes of git reset.#
Show solution
All three move the branch. --soft keeps the changes staged. --mixed (the default) keeps them in your files, unstaged. --hard discards them from the staging area and your files.
5. What is a fast-forward merge?#
Show solution
When the current branch has no commits of its own since the other branch started, Git just moves the pointer forward, and no merge commit is created. --no-ff forces a merge commit anyway.
6. What is a squash merge?#
Show solution
It combines all the commits of a branch into one commit on the target branch. It keeps main tidy, with one commit per pull request, but loses the individual commits on the target branch.
7. What is the difference between amend and rebase -i?#
Show solution
git commit --amend replaces only the latest commit. git rebase -i can edit, reorder, combine or delete any recent commits. Both rewrite history.
Everyday tools#
8. What is git stash?#
Show solution
It saves uncommitted changes and cleans the working tree, so that you can switch tasks. git stash pop restores them. Use -u to include untracked files. Stashes are local, and not pushed.
9. What is cherry-pick, and when do you use it?#
Show solution
It copies one commit to the current branch as a new commit. It is mostly used to bring a specific fix to a release branch without merging other work.
10. How do you resolve a conflict during a rebase?#
Show solution
Edit the file, run git add, then git rebase --continue, not git commit. Git stops at each commit that conflicts. git rebase --abort cancels and returns to the original state.
11. What is detached HEAD?#
Show solution
HEAD points directly to a commit rather than a branch, which happens when you check out a tag or an old commit. New commits are not on any branch. Run git switch -c name to keep them.
12. What is the difference between annotated and lightweight tags?#
Show solution
A lightweight tag is just a name for a commit. An annotated tag (git tag -a) is a full object with a message, author and date, and it is the right choice for releases. Tags are not pushed by default; use git push origin --tags.
13. What is the difference between git diff, git diff --staged and git diff HEAD?#
Show solution
git diff compares your files with the staging area. git diff --staged compares the staging area with the last commit. git diff HEAD compares your files with the last commit, so it shows all uncommitted changes.
Teamwork#
14. Describe a typical pull request workflow.#
Show solution
Update main, create a branch, commit small changes, and push. Open a pull request, let CI run, get a review, and push any fixes to the same branch. Merge it, delete the branch, and pull the new main.
15. Compare GitHub Flow, Git Flow and trunk-based development.#
Show solution
GitHub Flow uses short-lived branches off an always-deployable main. Git Flow adds develop, release and hotfix branches, which suits scheduled releases. In trunk-based development, everyone merges into main at least daily, and feature flags hide unfinished work. Teams that deploy often usually choose one of the first or last.
16. What makes a good commit?#
Show solution
It does one logical thing, it does not break the build, and its message says why the change was made. The subject line is short and in the imperative, like "Fix crash on empty cart".
17. Your push was rejected. What do you do?#
Show solution
Someone else pushed first. Run git pull (with merge or rebase), fix any conflicts, test, and push again. Do not use --force on a shared branch.
18. What is the difference between --force and --force-with-lease?#
Show solution
--force overwrites the remote branch regardless. --force-with-lease refuses if the remote has commits you have not fetched, so it protects teammates' work. Use it after rebasing your own branch.
19. You committed to main by mistake. How do you fix it?#
Show solution
If it has not been pushed: git switch -c new-branch to keep the commits, then git switch main and git reset --hard origin/main. If it has been pushed to a shared branch, use git revert.
20. What is an upstream branch?#
Show solution
The remote branch that a local branch tracks, set with git push -u. It lets plain git push and git pull work, and it lets git status report ahead and behind counts. In fork workflows, upstream is also the usual name for the remote of the original project.
Next: Advanced Questions.
Finished this lesson?