Git
03 / 06

Interview Questions

Git Interview Questions

Common Git questions asked in technical interviews - from fundamentals to advanced concepts. Understanding the "why" behind these answers sets you apart from candidates who only memorize commands.

1. What is Git and how does it differ from SVN?

Git is a distributed version control system (DVCS) where every developer has a full copy of the repository including complete history. SVN is centralized - there is one central server and developers only have working copies. Git enables offline work, faster operations (most commands are local), non-linear branching workflows, and greater resilience since there is no single point of failure.

2. What is the difference between git merge and git rebase?

Both integrate changes from one branch into another, but differently. git merge creates a new "merge commit" that has two parents, preserving the complete history of both branches. git rebase rewrites commits by replaying them on top of the target branch, producing a linear history as if the feature was developed after main. Rule of thumb: rebase local/private branches for clean history; never rebase shared/public branches because it rewrites SHAs and causes problems for collaborators.

3. What is the difference between git fetch and git pull?

git fetch downloads remote changes and updates remote-tracking branches (origin/main etc.) but does NOT modify your working directory or local branches. It is safe to run anytime. git pull is git fetch followed by git merge (or git rebase with --rebase flag) - it immediately integrates the remote changes into your current branch. Use git fetch when you want to review changes before merging; use git pull when you are ready to integrate.

4. Explain the three trees in Git (working directory, staging area, repository)

Git manages three "trees": (1) Working Directory - the actual files on disk that you edit. (2) Staging Area (Index) - a preview of the next commit; git add moves changes here. (3) Repository (HEAD) - the committed snapshot history stored in .git/. git status shows the difference between these three. git diff shows working directory vs staging area. git diff --staged shows staging area vs last commit.

5. What does git reset --soft vs --mixed vs --hard do?

All three move HEAD to a different commit but differ in how they handle the staging area and working directory. --soft: moves HEAD only; staged and working directory are untouched - your changes stay staged, ready for a new commit. --mixed (default): moves HEAD and clears the staging area; your changes are still in the working directory but unstaged. --hard: moves HEAD, clears staging area, and discards all working directory changes - effectively throws away all uncommitted work. Never use --hard carelessly; use the reflog to recover if needed.

6. What is a detached HEAD state?

Normally HEAD points to a branch name (like main), which in turn points to the latest commit. In detached HEAD state, HEAD points directly to a commit SHA instead of a branch. This happens when you checkout a specific commit, tag, or remote branch. You can make commits in detached HEAD, but they will be lost unless you create a branch before switching away (git checkout -b new-branch). It is useful for exploring history or testing an older version.

7. How do you resolve a merge conflict?

When Git cannot automatically merge changes, it marks conflicts in the file with conflict markers (<<<<<<, =======, >>>>>>>). To resolve: (1) Open conflicting files and edit them to the desired state, removing conflict markers. (2) Run git add on each resolved file to mark it resolved. (3) Run git commit to complete the merge. You can also use git mergetool to open a visual merge tool. To abort and return to pre-merge state, use git merge --abort.

8. What is git stash and when would you use it?

git stash temporarily shelves uncommitted changes so you can switch context without committing incomplete work. Common scenarios: you are mid-feature and an urgent bug comes in, you accidentally started work on the wrong branch, or you want to pull the latest remote changes but have local modifications that would conflict. Stashes are stored in a stack (LIFO). git stash pop restores the most recent stash and removes it; git stash apply restores without removing so you can apply the same changes to multiple branches.

9. What is the difference between git revert and git reset?

git revert creates a new commit that undoes the changes of a specific commit, preserving the original commit in history. It is safe for shared branches because it does not rewrite history. git reset moves the branch pointer backward, effectively removing commits from history - this rewrites history and should only be used on local commits that have not been pushed. If you need to undo a commit that is already on a shared remote branch, always use git revert.

10. What is git bisect and when is it useful?

git bisect is a debugging tool that uses binary search to find the exact commit that introduced a bug. You tell it a "good" commit (bug absent) and a "bad" commit (bug present). Git checks out the midpoint commit; you test it and mark it good or bad. Git repeats, halving the search space each time. For 1000 commits, it finds the culprit in about 10 steps. You can also automate it with git bisect run <test-script>, where the script exits 0 for good and non-zero for bad.

Keep your own version of these notes — editable, searchable, and organised by your stack.

Start free