Skip to content
Tutorials Center
My learning

Topics

Lesson 78 of 78 ยท Practice and Review

Git Interview Questions and Answers

Fifty Git interview questions with concise answers and runnable demos: merge vs rebase, reset modes, internals, workflows and recovery, from basic to advanced.

Git interviews tend to circle around the same themes: do you understand the model (snapshots, three areas, pointers), can you choose between similar commands (merge or rebase, reset or revert), and can you get yourself out of trouble. This page collects fifty questions in the order most interviews take them, with answers short enough to say out loud in under a minute.

Each answer states the idea first, then the detail an experienced interviewer is listening for. Five small runnable demos back up the claims that are easiest to get wrong. For every topic there is a lesson that goes deeper; the links are in the answers. A strong answer always names a trade-off ("rebase gives a linear history, but rewrites commits"), so practice saying both halves.

If a question asks "what is the difference between X and Y", answer with when you would use each one. Interviewers care more about your judgment than the definition.

Fundamentals

1. What is Git, and what does "distributed" mean? Git is a version control system that stores the full history of a project. Distributed means every clone holds the complete history, so you can commit, branch and inspect history offline, and any clone can serve as a backup. A central server is a convention, not a requirement. See How Git Works.

2. How is Git different from GitHub? Git is the tool that runs on your machine. GitHub (and GitLab, Bitbucket) are services that host Git repositories and add pull requests, issues and CI. See GitHub Basics.

3. Does Git store diffs or snapshots? Conceptually snapshots: every commit points to a complete tree of the project at that moment. Unchanged files are shared, because identical content has the same object ID. Physically, packfiles use delta compression to save space, but that is a storage detail.

4. What are the three areas of Git? The working tree (your files), the index or staging area (what the next commit will contain) and the repository (committed history). git add moves changes from the first to the second, git commit from the second to the third.

5. What is the staging area for? It lets you build a commit deliberately: stage related changes, leave others out, even stage part of a file with git add -p. See Staging Changes.

6. What does a commit contain? A pointer to a tree (the snapshot), the parent commit(s), author and committer with timestamps, and the message. Its ID is a hash of all of that, so changing anything produces a different commit.

7. What is the difference between git init and git clone? init creates an empty repository in the current folder. clone copies an existing repository including all history, sets up the remote origin and checks out the default branch.

8. What is HEAD? A pointer to where you are: normally a branch name (which points at a commit), so new commits move that branch. When it points directly at a commit, you are in detached HEAD state. See References and HEAD.

9. What is a branch, technically? A file containing a 40-character commit ID (a movable pointer). Creating one costs almost nothing, which is why Git encourages many short branches.

10. How do you see what changed? git status for the overview, git diff for unstaged edits, git diff --staged for what will be committed, git log and git show for history. See Status, Diff and Seeing Changes.

Bash Demo: the three areas in one status line

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Branching and merging

11. What does git merge do? It combines another branch's history into the current one. If the current branch is an ancestor, Git fast-forwards (moves the pointer). Otherwise it makes a merge commit with two parents. See Merging Branches.

12. What is a fast-forward? A merge where no new commit is needed because the target branch has not diverged. --no-ff forces a merge commit; --ff-only refuses to merge unless it can fast-forward.

13. What is a three-way merge? Git compares the two branch tips against their common ancestor (the merge base). A change made on only one side is taken; a change on both sides to the same lines is a conflict. See How Merge, Rebase and Diff Work Inside.

14. How do you resolve a merge conflict? Open the files with conflict markers, edit them to the final content, remove the markers, git add them, then git commit (or git merge --continue). git merge --abort returns to the state before the merge. See Resolving Merge Conflicts.

15. Why do conflicts happen, and how do you reduce them? Two people change the same lines, or one edits what another moved or deleted. Keep branches short-lived, merge main into them often, commit small, and avoid mass reformatting.

16. Merge versus rebase? Merge preserves history exactly and adds a merge commit. Rebase replays your commits on a new base, giving a linear history but new commit IDs. Merge for shared branches, rebase to tidy private ones. See Rebasing.

17. What is the golden rule of rebasing? Never rebase commits that others have already pulled. Rewriting shared history gives every teammate duplicate commits and painful conflicts.

18. What does git rebase --onto do? It moves a range of commits onto a different base: git rebase --onto new-base old-base branch. Useful to cut a branch off the branch it was started from.

19. What is git cherry-pick? It copies the change of one commit onto the current branch as a new commit. Good for backporting a fix; overused, it creates duplicate commits that make later merges noisy. See Cherry-Pick.

20. Squash merge versus normal merge? A squash merge turns all commits of a branch into one commit on the target and records no merge relationship. History is tidy, but the branch's individual commits are lost and the branch looks unmerged to Git.

Bash Demo: the same work, merged and rebased

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Undoing and rewriting

21. How do you undo the last commit but keep the changes? git reset --soft HEAD~1 keeps them staged, git reset HEAD~1 (mixed) keeps them unstaged.

22. What are the three modes of git reset? --soft moves the branch only; --mixed (default) also resets the index; --hard also overwrites the working tree. Only --hard can destroy uncommitted work. See git reset in Depth.

23. Reset versus revert? Reset moves the branch pointer and rewrites history; revert adds a new commit that undoes an old one and leaves history alone. Use revert on anything that is shared. See Reverting Commits Safely.

24. How do you fix the last commit message? git commit --amend -m "New message". It replaces the commit with a new one, so avoid it after pushing.

25. How do you add a forgotten file to the last commit? Stage it and run git commit --amend --no-edit.

26. How do you discard changes to one file? git restore file (older: git checkout -- file). Uncommitted edits cannot be recovered afterwards.

27. How do you unstage a file? git restore --staged file (older: git reset HEAD file).

28. What is the reflog? A local log of where HEAD and each branch pointed over time. After a bad reset or rebase, git reflog shows the old commit so you can return with git reset --hard HEAD@{n}. Entries expire after about 90 days (30 for unreachable ones). See The Reflog.

29. How do you squash the last four commits? git rebase -i HEAD~4 and mark the later ones squash or fixup, or non-interactively git reset --soft HEAD~4 followed by a commit. See Interactive Rebase.

30. What is git stash? A stack of saved working-state snapshots so you can switch tasks with a clean tree. stash push -u includes untracked files, stash pop re-applies and drops, stash apply keeps the stash. See Stashing.

31. How do you remove a committed secret? First rotate the secret, because it is compromised once pushed. Then rewrite history (git filter-repo or filter-branch), force-push, and ask collaborators to re-clone. Prevention is better: .gitignore and secret scanning hooks.

32. What is git clean? It deletes untracked files. Run git clean -nd for a dry run first; add -x to also delete ignored files.

Bash Demo: reset versus revert

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Remotes and workflows

33. What is origin? The default name Git gives to the remote you cloned from. It is only a name for a URL; you can rename it or add more remotes.

34. Fetch versus pull? fetch downloads new commits and updates remote-tracking branches such as origin/main without touching your branches. pull is fetch followed by a merge (or a rebase with --rebase). Many professionals fetch first and look. See push and pull.

35. What is a remote-tracking branch? A local, read-only reference like origin/main recording where the remote branch was at your last fetch. You never commit on it directly.

36. Why is my push rejected? The remote has commits you do not have (non-fast-forward). Fetch, integrate with merge or rebase, and push again. Forcing would erase their commits.

37. --force versus --force-with-lease? Both overwrite the remote branch. The lease variant aborts if the remote moved since your last fetch, so you cannot wipe out work you have not seen. See Fixing Common Git Mistakes.

38. What does -u in git push -u origin feature do? It sets the upstream so later git push and git pull on that branch need no arguments, and git status can report ahead and behind counts.

39. What is a pull request? A hosting feature, not a Git concept: a request to merge one branch into another with discussion, review and CI checks. See Pull Requests and Code Review.

40. What is a fork? A server-side copy of a repository under your account. You push to your fork and open a pull request to the original (upstream). See Forks and Upstreams.

41. Name some branching strategies. Trunk-based development (short branches, merge to main daily), GitHub Flow (branch, pull request, merge, deploy), and Git Flow (long-lived develop and release branches). Teams that deploy continuously favor trunk-based. See Collaboration Workflows.

42. What are submodules? A pointer from one repository to a specific commit of another, so a project can embed a dependency. They are precise but awkward (separate commits, extra update commands). See Submodules and Subtrees.

Bash Demo: fetch changes nothing local, pull does

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Internals

43. What are the four object types? Blob (file content), tree (a directory: names, modes and IDs of blobs and trees), commit (tree, parents, metadata) and annotated tag (a named, signed or described pointer to another object). See Git Objects.

44. How does Git name objects? By the SHA-1 hash (SHA-256 in newer repositories) of a header plus the content. The same content always gets the same ID, which gives deduplication and tamper detection.

45. What is detached HEAD, and how do you leave it? HEAD points at a commit instead of a branch, for example after git checkout <hash> or checking out a tag. New commits belong to no branch and become unreachable once you switch away. To keep them: git switch -c new-branch.

46. What happens during git commit? Git turns the index into tree objects, creates a commit object pointing at that tree and at the current commit as parent, then moves the current branch to the new commit. Details in The Index, Packfiles and Garbage Collection.

47. What is the difference between HEAD~1 and HEAD^? For a single-parent commit, none. ~n goes back n generations along first parents; ^n picks the n-th parent of a merge commit. HEAD^2 is the merged-in branch of a merge. See Revision Syntax.

48. What does git gc do? It packs loose objects into packfiles with delta compression, prunes unreachable objects older than the expiry, and packs references. Git runs it automatically from time to time. See Repository Maintenance and Performance.

49. How does git bisect work? It performs a binary search over history: you mark a good and a bad commit, Git checks out the middle one, you test, and the range halves. With git bisect run script the testing is automatic. See Finding Bugs with git bisect.

50. What are hooks? Scripts in .git/hooks (or a directory set with core.hooksPath) that Git runs at events such as pre-commit, commit-msg and pre-push. They are local and not cloned, so teams share them through a tracked folder or tooling. See Git Hooks.

Bash Demo: detached HEAD and rescuing a commit

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Demo: object types and a blob hash

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

More questions interviewers like

51. Describe the everyday Git workflow and the lifecycle of a file. You edit files in the working tree, stage them with git add, record them with git commit, and share with git push (after git pull or git fetch brings in others' work). A file moves through four states: untracked (Git does not know it), unmodified (tracked, same as the last commit), modified (changed in the working tree) and staged (marked for the next commit). git status always tells you which state each file is in. See Staging Changes.

52. What is a bare repository, and how does it differ from a non-bare one? A non-bare repository has a working tree plus a .git folder and is where you work. A bare repository (git init --bare) has no working tree, only the contents of .git, and is meant as a shared central copy that people push to. Pushing to the checked-out branch of a non-bare repository is refused by default for exactly that reason. See Remotes.

53. What advantages does Git have over Subversion? Full local history and offline work, cheap branching and merging (a branch is a pointer, not a directory copy), speed (most operations are local), content-hash integrity, the staging area, no single point of failure, and a much richer ecosystem of tools. Subversion's strengths are a simpler mental model, path-based permissions and handling of huge binary trees. See Git vs Other Version Control Systems.

54. What is a commit hash, and why does it matter? A 40-character SHA-1 (or 64-character SHA-256 in new repositories) computed from the commit's content: tree, parents, author, committer and message. It identifies the commit uniquely, lets you abbreviate it (git show a1b2c3d), and guarantees integrity: changing any detail, including an old ancestor, changes every hash after it.

55. What does git blame do? It shows, for each line of a file, the commit and author that last changed it. Use git blame -L 10,20 file for a range and -w or -C to ignore whitespace changes or detect moved lines. See Blame and Search.

56. Give a use case for git cherry-pick. Copying a single bug-fix commit from main onto a release branch without merging everything else. Cherry-pick creates a new commit with the same change but a different hash. See Cherry-Pick.

57. What is the difference between git diff, git diff --staged and git status? git diff shows unstaged changes (working tree against index), git diff --staged shows what the next commit will contain (index against HEAD), and git status summarises which files are in which state without showing the line changes. See Status and Diff.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A teammate already pulled your last commit, and it is wrong. Which fix is best?

    revert adds a new commit that undoes the old one, so nobody's history is rewritten.
  2. 2. What is the difference between git fetch and git pull?

    pull = fetch + merge (or rebase). Fetch never touches your own branches.
  3. 3. You are in detached HEAD state and made a commit. How do you keep it?

    A commit made while detached belongs to no branch. Creating a branch at it (or finding it later through the reflog) keeps it reachable.
  4. 4. Why does the same file content always produce the same blob ID?

    Because the blob ID depends on nothing but its content, identical files are stored once and corruption is detectable.
  5. 5. Which statement about rebase is correct?

    Each replayed commit has a different parent and therefore a different hash. Rewriting commits others have is what causes trouble.

Summary

  • Know the model: snapshots, three areas, branches as pointers, content-addressed objects. Every other answer follows from it.
  • For paired commands (merge or rebase, reset or revert, fetch or pull, force or lease), give the difference and when you would pick each, always weighing shared against private history.
  • Recovery is a strength: the reflog, ORIG_HEAD, reset --soft and branches made at a lost commit rescue almost everything that was committed.
  • Plumbing knowledge (cat-file, ls-tree, hash-object, commit-tree) shows depth and is easy to demonstrate with a few commands.
  • Practice aloud with the cheat sheet, the basic problems and the advanced problems.

That completes the Git tutorial. Keep experimenting in throwaway repositories, read your own git log often, and remember that nearly everything you commit can be recovered.