Git Notes
Prepare for Git interview questions covering basics, branching, merging, rebasing, reset, and advanced topics. Includes answers and explanations.
This comprehensive collection covers Git interview questions from basic to advanced, organized by difficulty level.
Beginner Level
Q: What is Git and why is it used? Git is a distributed version control system that tracks file changes, enables team collaboration, supports non-linear development through branching, and maintains complete project history.
Q: What is the difference between git add and git commit? git add moves changes to the staging area (preparation). git commit takes a snapshot of staged changes and permanently records them in the repository history.
Q: What is a branch in Git? A branch is a lightweight pointer to a specific commit. It represents an independent line of development, allowing work to proceed in isolation without affecting other branches.
Q: What is HEAD in Git? HEAD is a pointer to the current branch reference, which in turn points to the last commit on that branch. It represents "where you are now" in the repository.
Q: What is the staging area? The staging area (index) is an intermediate zone between the working directory and the repository. It holds changes marked for the next commit, allowing selective commits.
Intermediate Level
Q: Explain merge vs rebase. Merge creates a new merge commit combining two branches (preserves history). Rebase replays commits on top of another branch (creates linear history but rewrites SHAs). Merge is safe for shared branches; rebase is for local cleanup.
Q: What is a merge conflict and how do you resolve it? A conflict occurs when two branches modify the same lines. Git marks the file with conflict markers (<<<, ===, >>>). Resolve by editing the file, choosing the correct version, removing markers, staging, and committing.
Q: What is git stash used for? Stash temporarily saves uncommitted changes so you can switch branches or pull updates on a clean working directory. Use git stash pop to re-apply.
Q: What is the difference between git reset and git revert? Reset moves HEAD backward, removing commits (rewrites history). Revert creates a new commit that undoes changes (preserves history). Use revert on shared branches, reset on local branches.
Q: How do you undo the last commit?
- Keep changes staged:
git reset --soft HEAD~1 - Keep changes unstaged:
git reset HEAD~1 - Discard changes:
git reset --hard HEAD~1 - On shared branch:
git revert HEAD
Advanced Level
Q: Explain the git reflog. Reflog records every time HEAD changes (commits, resets, checkouts). It is a safety net for recovering lost commits after hard resets, bad rebases, or deleted branches. Entries expire after 90 days.
Q: What is git bisect? A binary search tool for finding the commit that introduced a bug. Mark a good and bad commit, then Git checks out the midpoint for testing. Each step halves the search space — O(log n) efficiency.
Q: Explain git cherry-pick. Cherry-pick applies changes from a specific commit onto the current branch, creating a new commit with the same changes but different SHA. Used for backporting fixes or selective integration.
Q: What is the difference between fetch and pull? Fetch downloads remote changes to tracking branches without modifying your working directory. Pull does fetch + merge, updating your current branch. Fetch is safer for review before integration.
Q: How do Git objects work internally? Git has four object types: blobs (file content), trees (directory listings), commits (snapshot pointers + metadata), and tags (named commit references). All are identified by SHA-1 hashes of their content.
Interview Questions
- What happens when you type
git commit? Git takes a snapshot of the staging area, creates a commit object with tree reference, parent reference, author info, and message, then moves the branch pointer forward.
- Can you explain detached HEAD? It occurs when HEAD points directly to a commit instead of a branch. Commits made in this state will be lost when you switch branches unless you create a branch first.
- What is force push and when is it acceptable? Force push (
--force) overwrites remote history. Only acceptable on personal feature branches after rebase. Never on shared branches. Always prefer--force-with-lease.
- How would you remove a sensitive file from the entire git history? Use
git filter-repo --invert-paths --path fileor BFG Repo-Cleaner. Simple deletion only removes from current state, not history.
- Explain the three-way merge algorithm. Git finds the common ancestor of two branches, then determines what changed in each branch relative to the ancestor. Non-conflicting changes are combined automatically; conflicts require manual resolution.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Git Interview Questions - Comprehensive Preparation Guide.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, interview, preparation, questions, git interview questions - comprehensive preparation guide
Related Git & GitHub Topics