Interview Q&A All Levels Git

Git Interview Questions and Answers (2026) Part 01

30 real-world Git scenario-based interview questions and answers covering branching strategies, merge/rebase, undoing changes, conflict resolution, Git internals, hooks, submodules, security, monorepos, and troubleshooting — Senior DevOps Engineer Edition.

April 16, 2026 12 min read 30 Questions DB
Level:

Basic Interview Questions

In Git, origin refers to the default name given to the remote repository from which the local repository was cloned. It is used as a reference to control fetches, pulls, and pushes.
The .gitignore file tells Git which files and folders to ignore when tracking changes. It prevents unnecessary files — such as logs, temporary files, or compiled artifacts — from being committed to the repository, keeping it clean and focused on important source files only.
A Version Control System (VCS) records the work of developers collaborating on projects. It maintains a history of code changes, allowing developers to add features, fix bugs, and run tests safely. If required, they can restore a previous working version, ensuring project integrity.

Using Git provides several advantages:

  1. Supports teamwork by allowing multiple developers to collaborate on the same project simultaneously.
  2. Each developer holds a full local copy of the repository, improving performance and enabling offline work.
  3. Free, open-source, and widely supported across platforms.
  4. Suitable for projects of all sizes and types.
  5. Each repository contains a single .git directory managing the entire history.

Git usually handles merges automatically, but conflicts arise when:

  • Two branches edit the same line of a file differently.
  • One branch deletes a file that another branch has modified.

In such cases, Git cannot decide which change to keep and requires manual resolution.

In Git, the Index (also called the Staging Area) is a temporary holding space for changes before they are committed to the repository. It allows you to selectively prepare specific modifications from your working directory before permanently saving them as part of the project history.

Use git commit --amend to modify the most recent commit:

  1. Make your changes to the files.
  2. Stage them with git add.
  3. Run the amend command:
git commit --amend

Git will open your default editor to update the commit message. Save and close to apply.

Note: Avoid amending commits that have already been pushed to a shared remote, as it rewrites history.

  • Rename the current branch:
git branch -m <new_branch_name>
  • Rename a different branch:
git branch -m <old_branch_name> <new_branch_name>
  • Push the renamed branch to remote and update tracking:
git push origin -u <new_branch_name>
git push origin --delete <old_branch_name>

Intermediate Interview Questions

Featuregit fetchgit pull
PurposeDownloads changes from remote but doesn’t apply themDownloads and merges changes into your current branch
Working DirectoryNo changes — safe to inspect before mergingChanges are applied immediately
Use CaseWhen you want to review changes before mergingWhen you want to update your branch quickly
WorkflowOften followed by git merge or git rebaseCombines fetch + merge in one step

Using git fetch with manual merge:

git fetch origin
git log origin/main       # inspect incoming changes
git merge origin/main     # apply after review

Using git pull directly:

git pull origin main

Pull Requests (PRs) are a core part of collaborative development:

  • Code Review: Team members can review code before it is merged, ensuring higher quality and catching bugs early.
  • Collaboration: Developers can discuss, comment, and suggest changes within the PR itself.
  • Version Control: Tracks proposed changes clearly and keeps the main branch stable.
  • Accountability: Maintains a clear record of who approved and merged each change.

git stash temporarily saves uncommitted changes so you can switch context — such as moving to another branch — without losing your in-progress work. Stashed changes can be reapplied later.

CommandPurpose
git stashSave current uncommitted changes
git stash listView all saved stashes
git stash popReapply the most recent stash and remove it from stash list
git stash applyReapply a stash without removing it from history
git stash drop stash@{n}Delete a specific stash entry

Use git revert — it creates a new commit that undoes the changes, preserving history:

  1. Switch to the target branch:
git checkout <branch_name>
  1. Find the commit hash:
git log
  1. Revert the commit:
git revert <commit-hash>
  1. Git opens an editor to confirm the revert message. Save and close.

  2. Push the revert commit:

git push origin <branch_name>

Why git revert over git reset? Revert is safe for shared repositories because it does not rewrite history.

Parametergit resetgit revert
HistoryMoves HEAD to a previous commit, removing commits from historyCreates a new commit that undoes changes, keeping history intact
SafetyRisky on shared/public branchesSafe to use on shared repositories
Use CaseUndoing local, unpushed commitsUndoing commits that are already pushed
git refloggit log
Tracks all movements of HEAD and branch pointer changesDisplays the commit history of the current branch
Shows references even if they are no longer part of any branchOnly shows commits reachable from the current branch
Can help recover lost commits or deleted branchesDoes not track changes outside the commit chain
Useful for disaster recovery and debuggingUsed for reviewing past changes and audit trails

HEAD is a special pointer in Git that always refers to the current position in the repository:

  • Points to the latest commit on the currently checked-out branch.
  • When you switch branches, HEAD automatically updates to the tip of the new branch.
  • A detached HEAD state occurs when HEAD points directly to a specific commit instead of a branch — common when checking out a tag or old commit.

git tag -a creates an annotated tag, which stores additional metadata alongside the tag reference:

  • Tagger’s name and email
  • Date and time of tagging
  • A custom descriptive message

Create an annotated tag:

git tag -a v1.0 -m "Version 1.0 Release"

Push the tag to remote:

git push origin v1.0

Annotated tags are preferred over lightweight tags for marking official releases because of their stored metadata.

ConceptHEADWorking TreeIndex (Staging Area)
DefinitionReference to the current commit or branchDirectory containing your actual project filesBuffer between working tree and repository
ContainsPointer stored in .git/HEADUnstaged edits and new filesChanges staged with git add, ready to commit
Location.git/HEADYour local project folder.git/index
  1. Identify conflicting files:
git status
  1. Open the conflicted file. Git marks conflicts like this:
<<<<<<< HEAD
Your changes
=======
Incoming changes
>>>>>>> feature-branch
  1. Manually edit the file to keep your changes, the incoming changes, or a combination of both. Remove all conflict markers.

  2. Stage the resolved file:

git add <file>
  1. Commit the merge:
git commit
  1. If rebasing, continue with:
git rebase --continue
#git mergegit rebase
How it worksCombines two branches and creates a merge commitReapplies commits from one branch on top of another
HistoryPreserves both branches’ full historyRewrites commit history to produce a linear timeline
Merge commitYes — a merge commit is createdNo — history is rewritten without merge commits
Best forCollaborative branches where history mattersPersonal branches or cleanup before opening a PR

Rule of thumb: Use merge to preserve context in team workflows; use rebase to maintain a clean, readable history on feature branches.

git push origin --delete <branch_name>

To also remove the local tracking reference:

git fetch --prune

git cherry-pick applies changes from a specific commit to the current branch without merging the entire source branch.

Key use cases:

  • Selective commit application: Pick only the commits you need.
  • Bug fixes: Backport a specific fix to another branch (e.g., a release branch).
  • No full branch merge required: Incorporate isolated changes cleanly.
  • New commit: The picked changes are applied as a new commit on the current branch.
git cherry-pick <commit-hash>

Advanced Interview Questions

A Git hook is a script that runs automatically at specific points in the Git workflow. They live inside .git/hooks/ and can be written in any scripting language.

HookTriggerCommon Use
pre-commitBefore a commit is createdRun linters or unit tests
commit-msgAfter commit message is writtenEnforce commit message format
post-commitAfter a commit is completedTrigger notifications or logging
pre-pushBefore pushing to remoteRun full test suite

Example — pre-commit hook to run ESLint:

#!/bin/sh
npx eslint . || exit 1

Make hooks executable:

chmod +x .git/hooks/pre-commit

For team-wide hooks, consider tools like Husky to version-control them alongside your project.

Use git reset with the appropriate flag depending on what you want to keep:

  • Keep changes staged (soft reset):
git reset --soft HEAD~1
  • Keep changes in working directory but unstaged (mixed reset — default):
git reset --mixed HEAD~1
  • Discard all changes completely (hard reset):
git reset --hard HEAD~1

Use --hard with caution — changes are permanently lost unless recoverable via git reflog.

  1. Start an interactive rebase for the last N commits:
git rebase -i HEAD~<number-of-commits>
  1. In the editor, leave the first commit as pick and change subsequent ones to squash (or s):
pick   a1b2c3d  First commit message
squash e4f5g6h  Second commit message
squash i7j8k9l  Third commit message
  1. Save and close. Git opens another editor to combine the commit messages — edit as needed and save.

  2. Force-push if the branch was already pushed:

git push origin <branch_name> --force-with-lease

Use a soft reset to move HEAD back while leaving your files and staged changes intact:

git reset --soft <commit-hash>

Your commits are undone, but all the changes remain staged — ready to be re-committed or reorganized.

CommandWhat it resetsAffects tracked filesAffects untracked files
git reset --hardHEAD, index, and working directory to a specific commit✅ Yes — discards modifications❌ No — leaves untracked files untouched
git clean -fdOnly the working directory❌ No — ignores tracked files✅ Yes — removes untracked files and directories

Combined usage — to fully clean a repository:

git reset --hard HEAD
git clean -fd

A file in Git moves through four primary states:

StateDescription
UntrackedNewly created file not yet known to Git
ModifiedFile has been edited since the last commit
StagedFile added via git add, queued for the next commit
CommittedFile permanently saved to repository history via git commit

Full lifecycle example:

# Create a new file
touch feature.js

# Stage it
git add feature.js

# Commit it
git commit -m "Add feature.js"

# Modify it
vim feature.js

# Stage and commit the update
git add feature.js
git commit -m "Update feature.js"

Each git commit creates an immutable snapshot of all staged files at that point in time.

Git provides three reset modes, each with a different scope of impact:

TypeCommandBehaviour
Softgit reset --soft <commit>Moves HEAD only — index and working directory remain unchanged. Changes stay staged.
Mixedgit reset --mixed <commit>Moves HEAD and clears the index — changes remain in the working directory as unstaged.
Hardgit reset --hard <commit>Moves HEAD, clears the index, and discards working directory changes completely.

Decision guide:

  • Use --soft to re-commit with a different message or grouping.
  • Use --mixed to unstage changes and re-evaluate what to commit.
  • Use --hard only when you are certain you want to throw away all changes.

Git tags are named pointers to specific commits — most commonly used to mark release points (e.g., v1.0, v2.1). Unlike branches, tags do not move; they permanently reference an exact commit.

Types of tags:

TypeDescription
LightweightA simple pointer to a commit, no extra metadata
AnnotatedStores tagger name, email, date, and a message — recommended for releases

Common tag commands:

CommandPurpose
git tag v1.0Create a lightweight tag
git tag -a v2.0 -m "Release 2.0"Create an annotated tag
git tagList all tags
git show v2.0View tag details
git push origin v2.0Push a specific tag to remote
git push origin --tagsPush all local tags to remote

git bisect uses binary search through commit history to efficiently pinpoint the commit that introduced a bug — without manually checking each one.

  1. Start bisecting:
git bisect start
  1. Mark the current (bad) commit:
git bisect bad
  1. Mark a known good commit:
git bisect good <commit-hash>
  1. Git checks out a commit halfway between. Test your code, then tell Git the result:
git bisect good   # if the bug is not present
git bisect bad    # if the bug is present
  1. Repeat until Git identifies the exact commit that introduced the bug.

  2. End the session:

git bisect reset

For large repositories, git bisect can find the culprit commit in O(log n) steps — dramatically faster than manual search.

Add More Questions to This Guide

Know questions that should be here? Share them and help the community!

Open Google Form