← Back to Interview Prep
🐙

Git & Version Control Q&A

30 scenario-based Git interview questions covering basics, merge vs rebase, resolving conflicts, branching strategies, and advanced commands.

10 Git Basics10 Merge & Rebase10 Advanced & Workflows
1

Git Basics & Branching

Q1.What is Git and how is it different from SVN?

  • Git is a Distributed Version Control System (DVCS). Every developer has a full local copy of the repository history.
  • SVN (Subversion) is a Centralized Version Control System (CVCS). Developers only check out the files they are working on from a central server.
  • Git allows full offline operation, faster branching/merging, and eliminates the single point of failure inherent in SVN.

Q2.Explain the three main states/areas in Git.

  • 1. Working Directory: The actual files on your local file system that you are modifying.
  • 2. Staging Area (Index): A file that stores information about what will go into your next commit. You add files here using `git add`.
  • 3. Local Repository (.git directory): Where Git stores the metadata and object database for your project. You save changes here using `git commit`.

Q3.What is the difference between `git pull` and `git fetch`?

  • `git fetch` downloads commits, files, and refs from a remote repository into your local repo, but it does NOT integrate them into your working copy. It lets you see what others have done.
  • `git pull` is essentially `git fetch` followed immediately by `git merge`. It downloads the changes and automatically merges them into your current branch.

Q4.What does `git clone` do?

  • It creates a complete local copy of a remote repository.
  • It downloads all commits, branches, and files, sets up a remote tracking branch (usually named `origin`), and checks out the default branch (usually `main` or `master`).

Q5.How do you create a new branch and switch to it in one command?

  • Using the older command: `git checkout -b <branch-name>`
  • Using the newer (Git 2.23+) command: `git switch -c <branch-name>`

Q6.What is the purpose of the `.gitignore` file?

  • It tells Git which untracked files and directories to ignore and never commit to the repository.
  • Commonly used to ignore compiled binaries (.class, .exe), log files, environment variables (.env), and dependency folders (node_modules, venv).

Q7.What is a "bare" repository in Git?

  • A bare repository is a Git repository that has no working directory. It only contains the `.git` folder contents.
  • It is typically used as a central remote repository (like on GitHub or a server) where developers push to and pull from, rather than a place where code is actively edited.

Q8.What does `git status` show you?

  • It shows the state of the working directory and the staging area.
  • It tells you which changes have been staged, which haven't, and which files aren't being tracked by Git. It is the most common command to check your current state.

Q9.How do you undo a `git add` before committing?

  • To unstage a specific file: `git restore --staged <file>` or `git reset HEAD <file>`
  • To unstage all files: `git restore --staged .` or `git reset`

Q10.What is a commit hash in Git?

  • A 40-character hexadecimal string that uniquely identifies a specific commit.
  • It is a SHA-1 hash generated based on the contents of the commit (the files, author, message, and parent commit). This ensures data integrity—if anything changes, the hash changes.
2

Merge, Rebase & Conflict Resolution

Q11.What is the difference between `git merge` and `git rebase`?

  • Both integrate changes from one branch into another.
  • `git merge` takes the two endpoints and creates a new "merge commit" that ties them together. It preserves the exact history of both branches (creates a diamond shape in history).
  • `git rebase` moves the base of your branch to the tip of the target branch. It rewrites project history by creating brand new commits for each commit in your original branch, resulting in a cleaner, linear history.

Q12.When should you NOT use `git rebase`?

  • The golden rule of rebasing: "Never rebase commits that have been pushed to a public repository that others are working on."
  • Because rebase rewrites history (changes commit hashes), if others have based their work on the old commits, their repos will become out of sync, leading to a confusing mess of merge conflicts.

Q13.How do you resolve a merge conflict?

  • 1. Git pauses the merge/rebase and marks conflicted files.
  • 2. Open the files. Git injects conflict markers (`<<<<<<<`, `=======`, `>>>>>>>`).
  • 3. Manually edit the file to keep the desired code and remove the markers.
  • 4. Save the file, run `git add <file>` to mark it resolved.
  • 5. Run `git commit` (if merging) or `git rebase --continue` (if rebasing).

Q14.What is a "Fast-Forward" merge?

  • A fast-forward merge occurs when there is a linear path from the current branch tip to the target branch.
  • Instead of creating a new merge commit, Git simply moves the current branch pointer forward to point at the same commit as the target branch.

Q15.How can you force a merge commit even if a fast-forward is possible?

  • Use the `--no-ff` flag: `git merge --no-ff <branch>`
  • This is often used to preserve the historical grouping of a feature branch, so you can see exactly when a feature was merged, even if it could have been fast-forwarded.

Q16.What does `git cherry-pick` do?

  • It allows you to pick an arbitrary commit from anywhere in the repository and apply its changes to your current branch.
  • Useful if you made a bug fix on the wrong branch, or if you need to backport a specific fix to an older release branch without merging the entire feature branch.

Q17.If you realize your last commit message had a typo, how do you fix it?

  • If you haven't pushed yet, run: `git commit --amend -m "New correct message"`
  • This replaces the last commit with a new one. (Do not amend commits that have already been pushed).

Q18.How do you squash multiple commits into one?

  • Use Interactive Rebase: `git rebase -i HEAD~N` (where N is the number of commits).
  • An editor opens. Change `pick` to `squash` (or `s`) for all commits except the first one.
  • Save and close. Another editor opens to allow you to write a single, combined commit message.

Q19.What is a merge conflict and why does it happen?

  • A merge conflict happens when two branches have modified the exact same lines in a file, or if one branch deleted a file while the other modified it.
  • Git cannot automatically determine which change is correct, so it stops the merge and requires manual human intervention.

Q20.What happens if you type `git rebase --abort`?

  • It completely cancels the ongoing rebase operation.
  • Your branch is returned to the exact state it was in before you started the rebase, undoing any conflict resolutions you may have made so far.
3

Advanced Git Commands & Workflows

Q21.What is `git stash` used for?

  • It temporarily saves changes in your working directory and staging area that are not yet ready to be committed, leaving you with a clean working directory.
  • Useful when you need to quickly switch branches to fix a bug, but don't want to commit half-done work.
  • Use `git stash pop` to apply the stashed changes back later.

Q22.How do you undo a commit that has already been pushed to a public remote?

  • Use `git revert <commit-hash>`.
  • Unlike `reset`, which rewrites history, `revert` creates a NEW commit that applies the exact inverse of the changes introduced by the target commit.
  • This is the safe way to undo public changes without breaking other developers' repositories.

Q23.Explain the difference between `git reset --soft`, `--mixed`, and `--hard`.

  • All three move the branch pointer backward.
  • `--soft`: Moves the pointer. Leaves files in Working Directory AND Staging Area intact (ready to commit).
  • `--mixed` (default): Moves the pointer. Unstages files (empties Staging Area), but leaves changes in Working Directory.
  • `--hard`: Moves the pointer and COMPLETELY WIPES all changes in Staging Area and Working Directory. (Dangerous!)

Q24.What is `git bisect`?

  • A tool that uses binary search to find the exact commit that introduced a bug.
  • You tell it a known "good" commit and a known "bad" commit. Git checks out a commit halfway between them.
  • You test the code and tell Git if it's good or bad. It repeats the process, halving the search space each time, until it pinpoints the faulty commit.

Q25.What is a detached HEAD state?

  • Normally, HEAD points to a branch reference (like `main`).
  • A detached HEAD means HEAD is pointing directly to a specific commit hash rather than a branch.
  • Happens if you run `git checkout <commit-hash>`. Any commits made in this state will be orphaned and garbage collected once you switch away, unless you create a new branch from them.

Q26.What are Git hooks?

  • Scripts that Git executes automatically before or after events such as commit, push, and receive.
  • Found in the `.git/hooks` directory. Commonly used for pre-commit (running linters/tests before allowing a commit) or pre-push (ensuring code passes CI).

Q27.What is Git Flow?

  • A popular branching strategy.
  • It defines two long-lived branches: `master` (production-ready) and `develop` (latest development).
  • It uses short-lived branches for `features` (branch from develop), `releases` (branch from develop to prepare for prod), and `hotfixes` (branch from master to fix urgent prod bugs).

Q28.How do you recover a dropped stash or a deleted branch?

  • Use `git reflog` (Reference logs).
  • Git records every movement of HEAD in the local repository for a few weeks.
  • You can find the SHA-1 hash of the commit where the deleted branch or dropped stash was located, and use `git checkout -b <branch-name> <hash>` to recover it.

Q29.What is a shallow clone?

  • Running `git clone --depth 1 <url>`.
  • It downloads only the latest commit of the repository, not the entire history.
  • Used to save bandwidth and disk space, especially in CI/CD pipelines where the full history is not needed to build the code.

Q30.How can you find out who modified a specific line of code?

  • Use `git blame <file>`.
  • It annotates each line in the given file with information from the revision which last modified the line, showing the author, timestamp, and commit hash.

🐙 Pro Tip for Git Interviews

Interviewers love to ask about the golden rule of rebasing. Always emphasize that you never rebase commits that have been pushed to a shared public branch!

← Back to All Interview Guides