Autocoder

Git Interview Questions and Answers with Commands

Last updated 2026-11-19

Git questions are asked in nearly every software interview, from freshers to senior engineers, and they are practical: how do you undo a commit that has already been pushed, what is the difference between merge and rebase, how do you resolve a conflict, how do you recover a branch you deleted. The best answers say what the command does to the three areas (working directory, staging area, repository) and name the safe option when history has been shared.

Every command here works on current Git versions. Git is also the base of CI and deployment workflows, so it connects with DevOps preparation and with the shell skills in Linux commands interview questions. For general round preparation see how to crack a technical interview.

Three areasWorking directory, staging area (index), repository
Object typesBlob, tree, commit, tag, all addressed by hash
Rule of thumbRewrite history only on commits that are not shared; use revert on shared branches

What reset modes do

Git Interview Questions and Answers with Commands: questions with worked answers

  1. 1

    What is Git and how is it different from GitHub?

    easy

    Git is a distributed version control system: every clone holds the full history, so most operations (commit, branch, log, diff) are local and fast, and work continues offline. GitHub, GitLab and Bitbucket are hosting services that store remote Git repositories and add pull requests, issue tracking, code review and CI. Git is the tool, and these are services built around it.

  2. 2

    Explain the working directory, staging area and repository.

    easy

    The working directory holds the files you edit. The staging area (index) holds the snapshot you are preparing for the next commit, filled by git add, which lets you commit only part of your changes. The repository (the .git directory) stores committed history. git status shows the differences between them, git diff compares the working directory with the index, and git diff --staged compares the index with the last commit.

  3. 3

    What is the difference between git fetch and git pull?

    easy

    git fetch downloads new commits and branch pointers from the remote into remote-tracking branches such as origin/main, but does not change your local branches or files, so it is safe. git pull is a fetch followed by a merge (or a rebase with --rebase) of the upstream branch into your current branch. Fetch first and inspect with git log HEAD..origin/main when you want to see what changed before integrating.

  4. 4

    Compare git merge and git rebase. When would you use each?

    medium

    Merge joins two histories with a new merge commit, preserving the real branching history and never rewriting commits. Rebase replays your commits on top of another branch, producing a linear history, but it creates new commits with new hashes. Rebase local feature branches to keep history clean before opening a pull request, and use merge (or squash merge) for integrating into shared branches. Never rebase commits that others have pulled.

    bash
    git switch feature
    git rebase main          # replay feature commits onto latest main
    git switch main
    git merge --no-ff feature  # keep a merge commit
  5. 5

    What is the difference between git reset, git revert and git checkout (or restore)?

    medium

    reset moves the current branch pointer to another commit, rewriting history locally: use it for local, unpushed work. revert creates a new commit that undoes an earlier commit, leaving history intact, so it is the safe choice for pushed commits. checkout or restore (git restore file) discards changes in a file or switches the working tree to another commit or branch without moving the branch pointer.

    bash
    git reset --soft HEAD~1    # undo last commit, keep changes staged
    git reset --hard HEAD~1    # undo last commit and discard changes
    git revert a1b2c3d         # new commit that cancels a1b2c3d
    git restore src/app.py     # discard unstaged edits in one file
  6. 6

    How do you undo the last commit?

    medium

    If it has not been pushed: git reset --soft HEAD~1 keeps the changes staged, --mixed keeps them unstaged and --hard throws them away. To only fix the message or add forgotten files, use git commit --amend. If it was already pushed to a shared branch, use git revert HEAD so nobody's history is rewritten. If you must rewrite a pushed personal branch, use git push --force-with-lease, which refuses if someone else pushed in the meantime.

  7. 7

    How do you resolve a merge conflict?

    medium

    A conflict arises when both branches change the same lines. Git marks the file with conflict markers: the lines between <<<<<<< and ======= are yours (HEAD), those between ======= and >>>>>>> are theirs. Edit the file to the intended result and remove the markers, git add the file, then complete with git commit (merge) or git rebase --continue. git merge --abort or git rebase --abort returns to the state before. Tools like git mergetool or the editor's three-way view help on large conflicts.

    text
    <<<<<<< HEAD
    timeout = 30
    =======
    timeout = 60
    >>>>>>> feature
    # edit to the final value, delete the markers, then:
    #   git add config.ini && git commit
  8. 8

    What does git stash do and how do you use it?

    medium

    It saves your uncommitted changes (tracked files, and untracked with -u) on a stack and restores a clean working directory, so you can switch branches or pull without committing half-done work. git stash push -m "wip", git stash list, git stash pop (apply and remove) or git stash apply (apply and keep), and git stash drop to remove an entry.

  9. 9

    What is cherry-pick and when is it useful?

    medium

    git cherry-pick <commit> applies the change introduced by a single commit onto the current branch as a new commit. It is used to bring a bug fix from the main branch to a release branch without merging everything else, or to rescue one commit from an abandoned branch. It can produce conflicts and duplicates the change under a different hash, so overuse makes history confusing.

  10. 10

    What is the difference between a fork, a clone and a branch?

    medium

    A clone is a local copy of a repository with its full history. A fork is a server-side copy of someone else's repository under your account on a hosting service, used to contribute to projects you cannot write to directly, with a pull request back to the original (upstream). A branch is a lightweight movable pointer to a commit inside one repository, used to isolate work.

  11. 11

    What is HEAD, and what is a detached HEAD?

    medium

    HEAD is a pointer to the current commit, normally via the branch you have checked out. A detached HEAD means HEAD points directly at a commit, not a branch, which happens when you check out a commit hash or a tag. New commits made there belong to no branch and can be lost; to keep them, create a branch with git switch -c new-branch before leaving.

  12. 12

    You deleted a branch or lost commits after a hard reset. How can you recover them?

    medium

    git reflog lists where HEAD and branches have been, including commits no longer reachable from any branch, for roughly 90 days by default. Find the commit hash before the mistake and run git branch recovered <hash> or git reset --hard <hash>. The reflog is local, so it only helps in the clone where the action happened.

    bash
    git reflog
    # a1b2c3d HEAD@{3}: commit: add login form
    git branch recovered a1b2c3d
  13. 13

    How does git bisect help find a bug?

    medium

    Bisect does a binary search through history. You mark a bad commit (usually HEAD) and a known good one, Git checks out the middle commit, you test and mark it good or bad, and it halves the range each time, so among 1000 commits about 10 tests find the culprit. git bisect run ./test.sh automates it, using the script's exit code (0 good, non-zero bad).

    bash
    git bisect start
    git bisect bad HEAD
    git bisect good v1.2.0
    git bisect run ./run_tests.sh
    git bisect reset
  14. 14

    What is .gitignore and how do you stop tracking an already committed file?

    medium

    .gitignore lists patterns of untracked files Git should not add (build output, dependencies, secrets such as .env). It does not affect files already tracked. To stop tracking a committed file while keeping it on disk, run git rm --cached path and commit, and add the pattern to .gitignore. A secret that was already pushed stays in history, so rotate the credential, and only then consider rewriting history with a tool such as git filter-repo.

  15. 15

    How does Git store data internally?

    medium

    Git is a content-addressable store. A blob holds file contents, a tree holds a directory listing of names mapped to blobs and subtrees, a commit points to a tree, its parent commit(s), the author and a message, and a tag names a commit. Each object is stored under the hash of its content (SHA-1, with SHA-256 supported), so identical content is stored once and any change alters the hashes up the chain, which makes history tamper-evident. A branch is just a file holding a commit hash.

  16. 16

    What is the difference between git merge --squash and a normal merge, and what does an interactive rebase offer?

    medium

    A squash merge combines all the feature branch's changes into a single new commit on the target branch and does not record the branch's commits as parents, giving a tidy history at the cost of losing individual commits. git rebase -i HEAD~4 lets you reorder, edit, squash (combine) or drop the last four commits and reword messages before sharing them, rewriting those commits.

  17. 17

    Compare Git workflows: feature branching, Gitflow and trunk-based development.

    hard

    Feature branching: each change has a short-lived branch merged by pull request into main. Gitflow adds long-lived develop, release and hotfix branches, suited to versioned releases but heavy. Trunk-based development has all developers integrate small changes into main at least daily, often behind feature flags, and relies on strong automated tests and CI; it reduces merge pain and fits continuous deployment. Teams choose by release cadence and test confidence.

  18. 18

    What are Git hooks and how are they used?

    hard

    Hooks are scripts in .git/hooks that Git runs at certain points: pre-commit (run linters or tests before a commit is created), commit-msg (validate the message format), pre-push, and on the server side pre-receive and post-receive. Because .git/hooks is not versioned, teams share them with tools like Husky or pre-commit, or set core.hooksPath to a tracked directory. Server-side enforcement in CI is still required because client hooks can be skipped.

Frequently asked questions

What is the difference between git add . and git add -A?

In current Git both stage new, modified and deleted files across the whole working tree when run from the repository root. git add . limits itself to the current directory and below, so they differ only when run in a subdirectory, and git add -u stages only tracked files.

What is a pull request?

A request on a hosting service to merge a branch into another branch, which opens a place for code review, discussion and automated checks before the merge. The term is from GitHub, GitLab calls it a merge request.

What is a bare repository?

A repository with no working directory, only the Git data, used as a central remote that people push to and pull from. It is created with git init --bare.

How do I change the commit message of an older commit?

For the latest commit use git commit --amend. For an older unpushed one, use git rebase -i, mark it reword, and edit the message. This rewrites that commit and those after it, so avoid it on shared history.

Related guides

More in CS Fundamentals Interview Questions

See all in this section