Top 50 Frequently Asked Git Interview Questions & Answers

A Git interview tests mental models, collaboration, recovery, and safe history management. These 50 questions cover the repository states and judgment calls that arise in real code reviews, release processes, and incident recovery.

Questions are original study examples, not questions from a live hiring assessment. Adapt your examples to your own work and verify tool behavior using an authorized practice environment.

Git fundamentals and the working tree

1. What does Git track?

Git stores snapshots of tracked file content and the history that connects those snapshots. It tracks changes through commits, while the working tree lets you modify files before choosing what should be staged for the next commit.

2. How is Git different from a centralized version control system?

Every ordinary Git clone contains repository history, enabling local commits and investigation without constant server access. Teams still agree on remotes and shared branch policies, so distributed storage does not eliminate collaboration rules.

3. What do the working tree, index, and HEAD represent?

The working tree contains editable files; the index, or staging area, describes what the next commit will record; HEAD identifies the current commit or branch tip. The three can differ, which explains many Git surprises.

4. What is the purpose of git status?

It compares the working tree and index with the current commit, showing staged changes, unstaged changes, and untracked files. Run it before destructive commands to understand exactly which state you are about to modify.

5. How do git add and git commit differ?

git add copies selected changes into the index. git commit records the staged snapshot and a message in repository history. Edits made after staging are not included automatically unless they are staged again.

6. What is a commit hash?

It is an object identifier derived from repository object content and format. It lets Git locate a precise commit; a short prefix works when unambiguous. A hash is not a human-friendly version or proof that code is safe.

7. What does git diff show by default?

Without extra options, git diff typically shows unstaged differences between the working tree and index. Use git diff –staged to inspect what the next commit will contain, and compare commits explicitly when reviewing history.

8. Why are .gitignore rules useful?

They keep new matching untracked files from being offered for tracking, such as build outputs and local configuration. Ignoring a path does not automatically remove a file already committed; use a deliberate tracking change for that.

9. What is the difference between git clone and git init?

git clone creates a local repository from an existing one and usually configures its source as a remote. git init initializes a repository in a directory, with no remote added unless you configure one.

10. How can you examine a commit without changing files?

Use git show with a commit identifier for its metadata and patch, or git log for history. Inspection commands are preferable to checkout or reset when your goal is only to understand a change.

Branches and integrating work

11. What is a Git branch?

A branch is a movable reference to a commit. It makes independent development possible without copying every project file into a separate history. The branch name is not the commit itself; it advances as new commits are made.

12. How do you create and switch to a branch?

git switch -c feature-name creates the branch and checks it out. Confirm the base branch and working-tree status first; changes already present in the working tree may carry into the new branch or block the switch.

13. What does a fast-forward merge mean?

When the target branch is an ancestor of the incoming tip, Git may advance its reference without creating a separate merge commit. This preserves the existing linear path; team policies can still require a merge commit.

14. How is a merge commit different from a regular commit?

A merge commit has multiple parent commits and records an integration point between histories. A regular commit normally has one parent. Either can represent a valid workflow depending on review and traceability needs.

15. What does git rebase do?

Rebase reapplies a sequence of commits onto a different base, producing new commit identities. It can make a local feature history easier to review, but rewriting already shared commits may disrupt collaborators.

16. When should you avoid rebasing?

Avoid rewriting commits that others have based their work on unless the team explicitly coordinates the operation. A private branch can be rebased more safely, but conflicts and changed commit IDs still require careful verification.

17. How do you resolve a merge conflict?

Inspect the conflicted files, understand both changes, edit the desired result, run tests, stage resolved files, and continue or complete the operation. Do not resolve conflicts simply by choosing 'ours' for every file.

18. What is the role of a pull request?

It provides a platform-managed proposal for review, discussion, automated checks, and controlled integration. A pull request is not a core Git object; different hosting services implement it differently.

19. How does cherry-pick differ from merge?

Cherry-pick applies the patch from selected commits to your current branch and creates new commits. Merge integrates branch history more broadly. Cherry-picking is useful for a small targeted fix but can duplicate changes across histories.

20. How can you see branches containing a commit?

git branch –contains <commit> lists local branches that include the commit in their ancestry. Use -r or -a when inspecting remote-tracking branches; fetched remote state may be stale.

Remotes and collaboration

21. What is a Git remote?

A remote is a named reference to another repository location. The name origin is conventional, not magical. Fetching or pushing uses the configured URL and permissions, subject to branch protections and server rules.

22. How is fetch different from pull?

git fetch downloads remote updates and adjusts remote-tracking references without merging them into the current branch. git pull performs a fetch followed by an integration strategy, such as merge or rebase, depending on configuration.

23. What does git push do?

It sends local objects and requests a remote reference update. A push may be rejected because the remote has newer commits, a branch is protected, or your credentials lack permission. Never assume push always overwrites remote history.

24. What are tracking branches?

A local branch can have an upstream reference used for status comparisons and default push or pull behavior. Remote-tracking references such as origin/main record what was last fetched; they do not update themselves until communication occurs.

25. Why might a push be rejected as non-fast-forward?

The remote branch tip includes history not present in your proposed update. Fetch and inspect the divergence before choosing merge, rebase, or another coordinated resolution. Force-pushing can discard colleagues' visible work.

26. What is the safe use of force-with-lease?

–force-with-lease attempts a forced update only when the remote reference is at the expected value. It reduces accidental overwrites but is not a substitute for policy, backup, or communication on shared branches.

27. What is the difference between HTTPS and SSH remotes?

Both can transport Git data. HTTPS commonly uses credential helpers or tokens; SSH uses keys and host verification. Choose an approved authentication method and never embed personal access tokens in committed remote URLs.

28. How can you inspect a remote configuration?

Use git remote -v to list names and URLs, or git remote show origin for more details. Confirm whether fetch and push URLs differ before altering credentials or migrating a repository.

29. What is a protected branch?

Protection is usually enforced by the hosting service, requiring reviews, checks, or restricted updates. Git itself does not impose identical server policies everywhere; verify your repository's current rules before attempting a release.

30. How do you recover from an accidental push to the wrong branch?

First assess visibility and impact. If the commit reached a shared branch, prefer a reviewed corrective commit or revert over silently rewriting shared history. Notify collaborators and follow incident or change-management procedures.

Undoing changes and recovering history

31. How does git restore differ from git reset?

git restore is designed to restore working-tree or staged file content. git reset moves the current branch or adjusts index state, depending on mode. Both can discard data in certain forms, so preview the scope first.

32. What does git revert do?

It creates a new commit that reverses the effect of an earlier commit where possible. For a shared branch, this often preserves a clearer audit trail than rewriting history with reset.

33. When can git reset –hard be dangerous?

It replaces the index and tracked working-tree files with the chosen commit and can discard uncommitted changes. Check status, back up important work, and understand whether recovery through reflog or another copy is possible before using it.

34. What does git reflog record?

Reflog records local updates to references such as HEAD, helping locate commits after branch moves or resets. It is local, expires under repository policies, and is not a guaranteed permanent backup.

35. How do you recover a deleted local branch?

If its commits remain reachable through reflog or another reference, identify the desired commit and create a new branch pointing to it. Avoid garbage collection or further destructive operations until the history is recovered.

36. What is git stash useful for?

Stash temporarily records working-tree and index changes so you can switch tasks. It is convenient for short interruptions but should not replace committed checkpoints or reliable backups of important work.

37. What is the difference between stash apply and pop?

apply reapplies a saved stash while keeping the stash entry. pop attempts to apply and then remove the entry if successful. Conflicts can occur in either case; inspect the result before deleting stash records.

38. How can you correct the most recent commit message?

For a local, unpublished commit, git commit –amend can update its message and creates a new commit identity. Coordinate with the team before amending a commit already shared with others.

39. How should you remove an accidental secret commit?

Treat the secret as compromised and rotate or revoke it immediately. Removing a value from a later commit does not erase earlier copies; use your organization's incident and history-remediation procedure.

40. What is the safest response to an unexpected clean command?

Do not run git clean -fd automatically. Inspect untracked files and use git clean -n as a preview. Untracked work may be the only copy of valuable files and might not appear in Git history.

Investigation, releases, and automation

41. What does git bisect help identify?

It performs a binary search over commits to locate a change associated with a regression. Its results depend on a reliable test that classifies each candidate commit as good, bad, or untestable.

42. How does git blame help with investigation?

It displays the most recent commit associated with each line. It provides a trail for understanding a change, not proof of responsibility for a defect. Review earlier context and related commits before drawing conclusions.

43. Why are annotated tags useful?

An annotated tag stores metadata about a release marker and can be signed. Teams use them to identify tested release points. A lightweight tag is only a reference; neither type guarantees the tagged build was deployed.

44. What are Git hooks?

Hooks are scripts triggered by selected local or server Git events. They can run checks, but local hooks may be skipped or absent; server-side policies and CI checks provide stronger shared enforcement.

45. Why is a small commit preferable for code review?

A focused commit explains one logical change, making it easier to test, discuss, revert, and trace. Large mixed-purpose commits conceal intent and increase the chance that a necessary correction also undoes unrelated work.

46. How do you inspect a file as it existed in an earlier commit?

Use git show <commit>:path/to/file to read the historical blob without switching the whole working tree. Paths and revisions must be correct; shell quoting may be required for special characters.

47. What is the purpose of git log –graph?

It displays commit relationships, especially branch and merge structure. Combining –oneline and –decorate can make a complex history more readable, but a simplified graph may hide commits depending on filters.

48. How do submodules work?

A parent repository records a reference to a particular commit in another repository. Cloning the parent may require explicit submodule initialization; updating the referenced commit is a separate change to review.

49. What is a Git worktree?

It allows multiple working directories linked to one repository, each with its own checked-out branch or state. It can be useful when reviewing a hotfix while preserving an ongoing feature branch.

50. What is a sound Git interview demonstration?

Describe an actual branch workflow: create a feature branch, make a focused commit, fetch updates, resolve a staged conflict, open a review, and explain recovery options. Interviewers value reasoning about data loss and collaboration more than memorized flags.

Study with official material

Consult issuer and project-maintained documentation, not leaked or memorized answer sets: official Git command documentation, Pro Git branching guide.

How to prepare next

Practice in a disposable repository. Explain the state before and after every command, especially when it can remove uncommitted work or rewrite a shared branch.

img