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 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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
–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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Consult issuer and project-maintained documentation, not leaked or memorized answer sets: official Git command documentation, Pro Git branching guide.
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.