OneHub Technical Interview Masterclass: Git Advanced
Total Questions: 145 Curriculum: Core Fundamentals (12), Applied Intermediate (12), Principal Advanced (12) + expanded sections
🏗️ Section 1: Core Fundamentals
Q1: What is the difference between git merge and git rebase? When do you use each?
Technical Answer:
merge joins two histories and preserves the branch topology by creating a merge commit when needed. rebase replays your commits on top of another branch, rewriting commit IDs to produce a linear history. Use merge when preserving history matters; use rebase for local cleanup before sharing work.
Code/Syntax Justification:
git checkout feature && git rebase main
# vs
git checkout main && git merge feature
🔍 Behind the Question: History shape and collaboration discipline. ❌ The Red Flags: Rebasing branches other people already pulled.
Q2: What is Git reflog, and how can it save you after a bad reset or rebase?
Technical Answer: Reflog tracks where HEAD and refs have pointed over time, even if commits are no longer reachable from a branch tip. It is the fastest recovery tool after a mistaken reset, rebase, or branch move because it lets you find prior commit hashes and restore them.
Code/Syntax Justification:
git reflog
git reset --hard HEAD@{1}
🔍 Behind the Question: Recovery skills and reference history. ❌ The Red Flags: Thinking deleted commits are always gone immediately.
Q3: What does git reset do, and how is it different from git revert?
Technical Answer:
reset moves branch pointers and can change the index and working tree depending on mode. revert creates a new commit that undoes an earlier commit without rewriting history. Reset is for local, private cleanup; revert is for safe public history reversal.
Code/Syntax Justification:
git reset --soft HEAD~1
git revert abc1234
🔍 Behind the Question: History rewriting vs safe undoing. ❌ The Red Flags: Using hard reset on shared branches.
Q4: What is a commit, branch, and tag in Git internals terms?
Technical Answer: A commit is an immutable object that points to a tree and parent commit(s). A branch is a movable reference to a commit, while a tag is usually a fixed reference to a specific commit. Git is a content-addressed object store, so refs are just pointers into the DAG of objects.
Code/Syntax Justification:
git show-ref
git cat-file -p <commit-sha>
🔍 Behind the Question: Object model fundamentals. ❌ The Red Flags: Treating branches as copies of code.
Q5: What is git bisect, and how does it isolate regressions?
Technical Answer: git bisect performs a binary search through history to find the first bad commit. You mark a known good commit and a known bad commit, and Git checks the midpoint repeatedly until the culprit is identified. It is one of the most efficient ways to trace regressions.
Code/Syntax Justification:
git bisect start
git bisect bad
git bisect good v1.0.0
🔍 Behind the Question: Debugging discipline and automation. ❌ The Red Flags: Manually guessing the regression point.
Q6: What is a fast-forward merge and when does it happen?
Technical Answer: A fast-forward merge happens when the target branch has not diverged, so Git can simply move the branch pointer forward without creating a merge commit. It keeps history linear but does not preserve branch structure. It is common when integrating a short-lived feature branch that was based on the current tip.
Code/Syntax Justification:
git merge feature
🔍 Behind the Question: Merge mechanics and history shape. ❌ The Red Flags: Not understanding when merge commits are created.
Q7: How do hooks and core.hooksPath support repository policy?
Technical Answer: Hooks let you run scripts on repository events like pre-commit, commit-msg, or pre-push. They are useful for formatting, linting, or blocking bad commits before they are pushed. core.hooksPath lets teams standardize where hooks are loaded from so they can be versioned and shared more reliably.
Code/Syntax Justification:
git config core.hooksPath .githooks
🔍 Behind the Question: Automation and developer workflow enforcement. ❌ The Red Flags: Treating hooks as a server-side replacement for CI.
Q8: What does git rebase --onto do?
Technical Answer: --onto lets you transplant a commit range from one base onto another base. It is useful when a branch accidentally includes commits from another branch or when you need to move a series of commits onto a different target. It is one of Git's most powerful history editing tools.
Code/Syntax Justification:
git rebase --onto main feature-base feature
🔍 Behind the Question: Advanced history rewriting. ❌ The Red Flags: Using rebase without understanding the commit range being moved.
🔧 Section 2: Applied Intermediate
Q9: What is the purpose of git cherry-pick, and when is it risky?
Technical Answer: Cherry-pick copies one or more commits from another branch and applies them as new commits. It is useful for hotfixes or backporting one fix to a release branch, but risky because it duplicates patch history and can make future merges harder if the same logic exists in multiple places.
Code/Syntax Justification:
git checkout release
git cherry-pick abc1234
🔍 Behind the Question: Selective history application and release management. ❌ The Red Flags: Using cherry-pick instead of merging the right branch.
Q10: How do you resolve a merge conflict correctly?
Technical Answer: First understand why the conflict happened, then choose the correct semantic resolution rather than blindly keeping one side. After editing the files, stage them and finish the merge or rebase. The right answer often depends on whether both changes are compatible, not just on text similarity.
Code/Syntax Justification:
git status
git add conflicted-file.py
git merge --continue
🔍 Behind the Question: Conflict resolution judgment. ❌ The Red Flags: Accepting all incoming changes without reading them.
Q11: What is interactive rebase used for?
Technical Answer: Interactive rebase lets you rewrite a series of commits before publishing them. Common uses include squashing fixup commits, reordering commits, editing commit messages, and splitting commits. It is a cleanup tool for local history, not a collaboration tool for shared branches.
Code/Syntax Justification:
git rebase -i HEAD~5
🔍 Behind the Question: Commit hygiene and history editing. ❌ The Red Flags: Using it on already-shared commits without coordination.
Q12: What are Git hooks and when would you use them?
Technical Answer: Hooks are scripts triggered by Git events like pre-commit, pre-push, and post-merge. They are commonly used for formatting, linting, tests, commit message validation, and policy enforcement. Hooks improve consistency, but repository-managed hooks or CI should back them up because local hooks are optional by default.
Code/Syntax Justification:
#!/bin/sh
npm test
🔍 Behind the Question: Workflow automation and enforcement. ❌ The Red Flags: Relying only on developer-installed local hooks.
🚀 Section 3: Principal Advanced
Q13: How does git bisect help find the commit that introduced a bug?
Technical Answer:
bisect performs a binary search over history by marking a known good commit and a known bad commit, then testing midpoint commits until the first bad change is identified. It is one of the most effective debugging tools in Git when a regression is hard to isolate manually.
Code/Syntax Justification:
git bisect start
git bisect bad
git bisect good v1.2.0
🔍 Behind the Question: Efficient root-cause isolation. ❌ The Red Flags: Manually checking commits one by one on large histories.
Q14: What is the difference between git reset --soft, --mixed, and --hard?
Technical Answer:
--soft moves HEAD and keeps index and working tree intact. --mixed resets the index but leaves working tree changes unstaged. --hard resets HEAD, index, and working tree, discarding uncommitted changes. The deeper point is understanding which parts of the three-tree model you are changing.
Code/Syntax Justification:
git reset --soft HEAD~1
git reset --mixed HEAD~1
git reset --hard HEAD~1
🔍 Behind the Question: Git state model and safe undo operations.
❌ The Red Flags: Using --hard without understanding data loss.
Q15: What are fast-forward merges and why do they matter?
Technical Answer: A fast-forward merge happens when the target branch tip is simply advanced to the source branch commit because there is no divergent history. It keeps history simple but does not preserve the fact that a branch existed. Teams may disable FF-only merges if they want explicit merge commits for traceability.
Code/Syntax Justification:
git merge --ff-only feature
🔍 Behind the Question: Branch policy and history shape. ❌ The Red Flags: Confusing fast-forward with rebase.
Q16: How do you reason about remote refs and force pushes safely?
Technical Answer:
Remote branches are references stored on the server, and force-pushing rewrites them. Safe workflows use --force-with-lease instead of --force because it refuses to overwrite remote work you have not seen. This matters when rebasing or amending history on shared branches.
Code/Syntax Justification:
git push --force-with-lease origin feature
🔍 Behind the Question: Collaboration safety and distributed state. ❌ The Red Flags: Using plain force push casually.
🧠 Section 4: Expert Deep Dives
Q17: What is Git's object model?
Technical Answer: Git stores data as blobs, trees, commits, and tags in a content-addressed object database. Blobs hold file contents, trees model directories, commits point to trees and parent commits, and tags can annotate important commits. Understanding this helps explain why history is cheap to branch and why commits are immutable.
Code/Syntax Justification:
git cat-file -t <sha>
git cat-file -p <sha>
🔍 Behind the Question: Internal storage and DAG structure. ❌ The Red Flags: Thinking Git stores diffs as the primary object.
Q18: What are refs and how do branches map to them?
Technical Answer:
Refs are named pointers to commit hashes, such as refs/heads/main, refs/remotes/origin/main, and refs/tags/v1.0.0. A local branch is just a movable ref that advances as you commit. Remote-tracking branches show your last known view of the remote, not the remote's current live state.
Code/Syntax Justification:
git show-ref
git rev-parse refs/heads/main
🔍 Behind the Question: Pointer semantics and branch bookkeeping. ❌ The Red Flags: Treating a branch as a separate copy of files.
Q19: What happens internally when you run git reset?
Technical Answer:
reset can move the current branch tip and optionally update the index and working tree depending on mode. --soft only moves HEAD, --mixed also resets the index, and --hard resets everything to the target commit. The important part is knowing which of the three areas changes.
Code/Syntax Justification:
git reset --soft HEAD~1
git reset --mixed HEAD~1
git reset --hard HEAD~1
🔍 Behind the Question: Three-tree model and destructive operations.
❌ The Red Flags: Using --hard without understanding what will be lost.
Q20: How does git revert differ from git reset in shared history?
Technical Answer:
revert creates a new commit that applies the inverse of an earlier commit, which preserves history and is safe for public branches. reset rewrites the branch pointer and is usually only safe before others depend on that history. Use revert when you need an auditable undo in shared repositories.
Code/Syntax Justification:
git revert abc1234
🔍 Behind the Question: Safe history correction. ❌ The Red Flags: Undoing public commits with reset as a default habit.
Q21: How do you use git stash effectively?
Technical Answer: Stash temporarily shelves uncommitted changes so you can switch branches or clean the working tree without losing work. It is useful for context switching, but it is not a long-term storage mechanism. Good practice is to name stashes and apply them deliberately rather than letting them accumulate indefinitely.
Code/Syntax Justification:
git stash push -m "wip auth refactor"
git stash pop
🔍 Behind the Question: Working tree management and context switching. ❌ The Red Flags: Treating stash as a substitute for commits.
Q22: What is interactive rebase used for in a professional workflow?
Technical Answer: Interactive rebase lets you rewrite local commit history by squashing, reordering, editing messages, or splitting commits. It is commonly used before opening a pull request to make history easier to review. The key rule is to avoid rewriting commits that others already pulled unless your team explicitly coordinates it.
Code/Syntax Justification:
git rebase -i HEAD~5
🔍 Behind the Question: History hygiene and reviewability. ❌ The Red Flags: Using rebase as a cleanup tool on shared branches.
Q23: How do Git hooks support code quality, and what are their limits?
Technical Answer: Hooks can block bad commits, enforce formatting, validate messages, or run tests before push. They are helpful because they catch issues early, but they are local by default and can be bypassed, so CI still needs to enforce the real policy. Shared hook paths make team adoption much more consistent.
Code/Syntax Justification:
git config core.hooksPath .githooks
🔍 Behind the Question: Developer workflow automation. ❌ The Red Flags: Depending on local hooks as the only quality gate.
Q24: How does git bisect scale debugging of regressions?
Technical Answer: Bisect performs a binary search over history, dramatically reducing the number of commits you need to test. You mark one good commit and one bad commit, then Git steps through the midpoint until the first bad change is found. It is especially valuable when the failure is intermittent or buried in a large history.
Code/Syntax Justification:
git bisect start
git bisect bad
git bisect good v2.0.0
🔍 Behind the Question: Efficient root cause discovery. ❌ The Red Flags: Manually checking every commit in sequence.
Q25: What is a merge strategy, and when would you choose recursive vs ours/theirs behavior?
Technical Answer:
Merge strategies decide how Git combines divergent histories. The default recursive strategy handles common branching cases well, while ours or theirs style approaches are used when one side should intentionally win in a specific conflict scenario. The interview point is that merge strategy reflects policy, not just tooling.
Code/Syntax Justification:
git merge -s recursive feature
🔍 Behind the Question: Conflict policy and history integration. ❌ The Red Flags: Treating merge conflicts as purely mechanical text edits.
Q26: What is the difference between a remote branch and a remote-tracking branch?
Technical Answer:
A remote branch exists on the server, while a remote-tracking branch is your local snapshot of that remote branch's last known state. For example, origin/main in your repo is not the actual remote branch, but a reference updated by fetch. This distinction matters when reasoning about stale state and push safety.
Code/Syntax Justification:
git fetch origin
git branch -r
🔍 Behind the Question: Distributed synchronization semantics. ❌ The Red Flags: Assuming your local remote-tracking branch is always current.
Q27: What problem do submodules solve, and what are the tradeoffs?
Technical Answer: Submodules let one repository reference a specific commit in another repository. They are useful for vendoring shared dependencies or separating large components, but they add operational complexity because clone, update, and CI workflows become more fragile. Many teams prefer subtree or package-based dependency management unless submodules are clearly justified.
Code/Syntax Justification:
git submodule add https://example.com/lib.git libs/lib
git submodule update --init --recursive
🔍 Behind the Question: Cross-repository dependency management. ❌ The Red Flags: Adding submodules without a maintenance strategy.
Q28: How do you safely clean up commits before sharing a branch?
Technical Answer: Use interactive rebase to squash fixups, rewrite messages, and reorder commits locally before the branch is public. After cleanup, inspect the diff against the target branch to ensure the logical changes still match the review story. The main goal is readable history without changing behavior unexpectedly.
Code/Syntax Justification:
git rebase -i origin/main
git diff origin/main...HEAD
🔍 Behind the Question: Review-friendly history preparation. ❌ The Red Flags: Cleaning history without re-checking the final patch set.
Q29: What is the three-tree model in Git?
Technical Answer: Git tracks the working tree, the index, and the HEAD commit. Many commands are just moving data between those three states, which explains why reset, checkout, and restore behave differently.
Code/Syntax Justification:
git status
🔍 Behind the Question: Added for deeper coverage and interview readiness. ❌ The Red Flags: Weak or vague answers that fail to justify design choices.
Q30: How do remote-tracking branches work?
Technical Answer: Remote-tracking branches like origin/main are local references that remember the last fetched state of a remote branch. They are not your local branch, but a read-only pointer used for comparison and merging.
Code/Syntax Justification:
git fetch origin
🔍 Behind the Question: Added for deeper coverage and interview readiness. ❌ The Red Flags: Weak or vague answers that fail to justify design choices.
Q31: What is the difference between git switch and git checkout?
Technical Answer: Switch is the clearer command for branch movement, while restore is for file restoration. Checkout is the older multifunction command that handled both, which made it harder to understand and safer commands were introduced.
Code/Syntax Justification:
git switch feature
🔍 Behind the Question: Added for deeper coverage and interview readiness. ❌ The Red Flags: Weak or vague answers that fail to justify design choices.
Q32: What is stash used for?
Technical Answer: Stash temporarily saves uncommitted work so you can switch context without committing incomplete changes. It is useful for quick interrupts, hotfixes, or partial cleanup.
Code/Syntax Justification:
git stash push -m 'wip'
🔍 Behind the Question: Added for deeper coverage and interview readiness. ❌ The Red Flags: Weak or vague answers that fail to justify design choices.
Q33: How do git add -p and patch mode help?
Technical Answer: Patch mode lets you stage hunks selectively instead of whole files, which is great for splitting clean commits and reviewing changes carefully. It supports better commit hygiene.
Code/Syntax Justification:
git add -p
🔍 Behind the Question: Added for deeper coverage and interview readiness. ❌ The Red Flags: Weak or vague answers that fail to justify design choices.
Q34: What is the difference between a merge commit and a squash merge?
Technical Answer: A merge commit preserves the complete branch history while a squash merge compresses all changes into one new commit. Squash merges create a simpler history but lose commit-level detail.
Code/Syntax Justification:
git merge --squash feature
🔍 Behind the Question: Added for deeper coverage and interview readiness. ❌ The Red Flags: Weak or vague answers that fail to justify design choices.
Q35: How do hooks support commit quality?
Technical Answer: Hooks can enforce formatting, linting, commit message policy, or security checks before changes are accepted. They are useful guardrails but should complement CI rather than replace it.
Code/Syntax Justification:
pre-commit hook
🔍 Behind the Question: Added for deeper coverage and interview readiness. ❌ The Red Flags: Weak or vague answers that fail to justify design choices.
Q36: Why is --force-with-lease safer than --force?
Technical Answer: Force-with-lease refuses to overwrite a remote ref if it has moved unexpectedly. That protects teammates from accidentally losing newer commits when rewriting history.
Code/Syntax Justification:
git push --force-with-lease
🔍 Behind the Question: Added for deeper coverage and interview readiness. ❌ The Red Flags: Weak or vague answers that fail to justify design choices.
Q37: What is a detached HEAD?
Technical Answer: A detached HEAD means HEAD points directly to a commit instead of a branch ref. It is useful for inspection or temporary experiments, but you must create a branch if you want to keep new work.
Code/Syntax Justification:
git checkout <sha>
🔍 Behind the Question: Added for deeper coverage and interview readiness. ❌ The Red Flags: Weak or vague answers that fail to justify design choices.
Q38: How does git bisect run automate debugging?
Technical Answer: Bisect run repeatedly checks out commits and runs a test command until the first bad commit is found. It is powerful for regressions because the search is logarithmic rather than linear.
Code/Syntax Justification:
git bisect run pytest
🔍 Behind the Question: Added for deeper coverage and interview readiness. ❌ The Red Flags: Weak or vague answers that fail to justify design choices.
Q39: What is the purpose of git blame?
Technical Answer: Blame maps lines to commits so you can see where a change came from and who last touched it. It is a diagnostic tool, not a judgment tool.
Code/Syntax Justification:
git blame file.py
🔍 Behind the Question: Added for deeper coverage and interview readiness. ❌ The Red Flags: Weak or vague answers that fail to justify design choices.
Q40: How do submodules differ from subtree merges?
Technical Answer: Submodules track an external repository as a separate dependency, while subtree merges vendor the code into your repo history. Submodules preserve boundaries but can be harder to manage.
Code/Syntax Justification:
git submodule add <url>
🔍 Behind the Question: Added for deeper coverage and interview readiness. ❌ The Red Flags: Weak or vague answers that fail to justify design choices.
Q41: What is the object database in Git?
Technical Answer: Git stores blobs, trees, commits, and tags as content-addressed objects. The SHA identifies object content, and refs point to commits that link the rest of the graph together.
Code/Syntax Justification:
git cat-file -p <sha>
🔍 Behind the Question: Added for deeper coverage and interview readiness. ❌ The Red Flags: Weak or vague answers that fail to justify design choices.
Q42: How do you safely recover a deleted branch?
Technical Answer: If a branch was deleted but its commits still exist, reflog or other refs can help recover the commit tip. After finding the commit, you create a new branch from it.
Code/Syntax Justification:
git branch restore <sha>
🔍 Behind the Question: Added for deeper coverage and interview readiness. ❌ The Red Flags: Weak or vague answers that fail to justify design choices.
Q43: What is the difference between rebase and merge-merges?
Technical Answer: Plain rebase linearizes a branch, while rebase-merges preserves some merge structure when rewriting history. It is useful when dependent branches or feature stacks must keep topology.
Code/Syntax Justification:
git rebase --rebase-merges main
🔍 Behind the Question: Added for deeper coverage and interview readiness. ❌ The Red Flags: Weak or vague answers that fail to justify design choices.
Q44: Why should you inspect git log --graph?
Technical Answer: The graph view makes branch topology and merge points visible, which helps during review, debugging, and release analysis. It is one of the fastest ways to understand a repo’s shape.
Code/Syntax Justification:
git log --graph --oneline --decorate --all
🔍 Behind the Question: Added for deeper coverage and interview readiness. ❌ The Red Flags: Weak or vague answers that fail to justify design choices.
Q45: How do you revert a merge commit correctly?
Technical Answer: Reverting a merge commit requires choosing the mainline parent so Git knows which side to keep as the baseline. This is different from reverting a normal commit because merges have multiple parents.
Code/Syntax Justification:
git revert -m 1 <merge-sha>
🔍 Behind the Question: Added for deeper coverage and interview readiness. ❌ The Red Flags: Weak or vague answers that fail to justify design choices.
Q46: What is the use of git worktree?
Technical Answer: Worktree lets you check out multiple branches from the same repository at once without cloning again. It is useful for parallel maintenance work or comparing branches side by side.
Code/Syntax Justification:
git worktree add ../bugfix bugfix
🔍 Behind the Question: Added for deeper coverage and interview readiness. ❌ The Red Flags: Weak or vague answers that fail to justify design choices.
Q47: What are Git notes used for?
Technical Answer: Git notes let you attach metadata to commits without changing the commit itself. They are useful for annotations, audit data, or extra review context.
Code/Syntax Justification:
git notes add -m 'reviewed'
🔍 Behind the Question: Added for deeper coverage and interview readiness. ❌ The Red Flags: Weak or vague answers that fail to justify design choices.
Q48: How do you keep large Git histories manageable?
Technical Answer: Use shallow clones where appropriate, prune stale refs, enforce good merge strategy, and avoid unnecessary binary churn. Repository health is part of engineering productivity.
Code/Syntax Justification:
git gc
🔍 Behind the Question: Added for deeper coverage and interview readiness. ❌ The Red Flags: Weak or vague answers that fail to justify design choices.
🎯 Section 4: Deep Coverage
Q49: What is the difference between git fetch and git pull?
Technical Answer: fetch downloads remote refs and objects without changing your working branch, while pull does a fetch plus an integration step like merge or rebase. Use fetch when you want to inspect changes first; use pull when you explicitly want to integrate them.
Code/Syntax Justification:
git fetch origin
git pull --rebase origin main
🔍 Behind the Question: Remote synchronization discipline. ❌ The Red Flags: Pulling blindly without reviewing incoming commits.
Q50: What does git stash actually store?
Technical Answer: Stash saves a snapshot of the working tree and optionally the index so you can switch contexts without making a commit. It is best for temporary interruptions, not as a long-term branch substitute.
Code/Syntax Justification:
git stash push -u -m "wip: debug login"
🔍 Behind the Question: Short-term state preservation. ❌ The Red Flags: Treating stash like a feature branch history.
Q51: Why is git blame useful and when can it mislead you?
Technical Answer: blame helps identify the commit that last touched each line, which is useful for code archaeology and debugging. It can mislead when lines were moved, reformatted, or generated, so the result needs context rather than blind trust.
Code/Syntax Justification:
git blame -L 20,40 app/service.py
🔍 Behind the Question: Code archaeology and accountability. ❌ The Red Flags: Using blame as a substitute for understanding design history.
Q52: What is the purpose of .gitignore versus .git/info/exclude?
Technical Answer: .gitignore is versioned and shared with the team, while .git/info/exclude is local-only ignore configuration. Use .gitignore for project-wide rules and info/exclude for personal workspace clutter.
Code/Syntax Justification:
node_modules/
.env
🔍 Behind the Question: Ignore rule scope. ❌ The Red Flags: Committing machine-specific ignore rules into the repo.
Q53: How does git clean differ from git reset?
Technical Answer: reset changes tracked file state and branch pointers, while clean removes untracked files and directories. clean is much more destructive for generated artifacts, so it should be used carefully and previewed first.
Code/Syntax Justification:
git clean -fdn
git clean -fd
🔍 Behind the Question: Working tree hygiene and safety. ❌ The Red Flags: Running a destructive clean without a dry run.
Q54: What problem does git restore solve?
Technical Answer: restore provides a clearer way to discard or recover file content without overloading reset for every use case. It separates working tree restoration from branch movement, which makes commands less ambiguous.
Code/Syntax Justification:
git restore --staged file.txt
git restore file.txt
🔍 Behind the Question: Command clarity and modern Git usage.
❌ The Red Flags: Using reset for file-level undo when restore is clearer.
Q55: Why would you use signed commits or tags?
Technical Answer: Signed commits and tags provide cryptographic trust that the object came from a verified key holder. They matter in release engineering and security-sensitive workflows where provenance is important.
Code/Syntax Justification:
git tag -s v2.0.0 -m "release 2.0.0"
🔍 Behind the Question: Supply-chain trust and provenance. ❌ The Red Flags: Assuming commit hashes alone prove identity.
Q56: What is a detached HEAD state?
Technical Answer: Detached HEAD means HEAD points directly to a commit rather than a branch ref. It is useful for inspection or temporary experiments, but commits made there are easy to lose unless you create a branch.
Code/Syntax Justification:
git checkout v1.4.2
git switch -c hotfix
🔍 Behind the Question: Ref state and commit reachability. ❌ The Red Flags: Making important work in detached HEAD without saving it.
Q57: How do remote tracking branches work?
Technical Answer: Remote tracking branches record the last known state of a branch on a remote, such as origin/main. They let Git compare your local work to the remote and support clean fetch/merge workflows.
Code/Syntax Justification:
git branch -vv
🔍 Behind the Question: Remote reference bookkeeping. ❌ The Red Flags: Confusing local branches with remote-tracking refs.
Q58: What is the value of git range-diff?
Technical Answer: range-diff compares two series of commits, which is excellent after rebasing or when reviewing patch stack changes. It shows whether the logical patch set stayed equivalent even if commit IDs changed.
Code/Syntax Justification:
git range-diff main...feature main...feature-rebased
🔍 Behind the Question: Patch series review and rebase verification. ❌ The Red Flags: Reviewing rebased branches only by commit hashes.
Q59: When should you prefer rebase -i over a normal merge?
Technical Answer: Use interactive rebase when cleaning up your own local commits before sharing, such as squashing fixups or rewriting messages. Use merge when preserving branch topology or integrating shared work safely.
Code/Syntax Justification:
git rebase -i HEAD~4
🔍 Behind the Question: Commit hygiene and history policy. ❌ The Red Flags: Rewriting commits after teammates have built on them.
Q60: How do you safely amend a commit?
Technical Answer: Amending is fine before a commit is shared because it rewrites the last commit with updated content or message. After push, amend becomes a coordination problem because it changes history and can confuse collaborators.
Code/Syntax Justification:
git commit --amend --no-edit
🔍 Behind the Question: History rewriting discipline. ❌ The Red Flags: Amending public commits without coordination.
Q61: What does git submodule solve, and what pain does it create?
Technical Answer: Submodules pin one repo inside another at a specific commit, which can be useful for vendored dependencies or shared components. The tradeoff is extra workflow complexity because both the parent repo and nested repo must be updated intentionally.
Code/Syntax Justification:
git submodule update --init --recursive
🔍 Behind the Question: Multi-repo dependency management. ❌ The Red Flags: Adding submodules without a maintenance process.
Q62: What is git push --force-with-lease for?
Technical Answer: It safely updates a remote branch only if the remote still matches your expected state, reducing the risk of overwriting someone else’s work. It is preferred over plain force push when history rewriting is unavoidable.
Code/Syntax Justification:
git push --force-with-lease origin feature
🔍 Behind the Question: Safe history rewrites.
❌ The Red Flags: Using --force by default on shared branches.
Q63: How do you inspect staged changes before committing?
Technical Answer: Review staged diffs to ensure only intended content will be committed. This catches accidental file additions, generated artifacts, or incomplete edits before they reach history.
Code/Syntax Justification:
git diff --cached
🔍 Behind the Question: Commit quality control. ❌ The Red Flags: Committing without checking the index.
Q64: What is a merge strategy and why does it matter?
Technical Answer: Merge strategy determines how Git combines histories, especially in non-trivial cases like criss-cross merges or subtrees. Understanding the strategy matters when automation or repository policy depends on consistent merge behavior.
Code/Syntax Justification:
git merge -s ort feature
🔍 Behind the Question: Merge algorithm behavior. ❌ The Red Flags: Assuming all merges are identical under the hood.
Q65: What is the role of tags in release workflows?
Technical Answer: Tags mark immutable release points so deployment, changelog generation, and rollback references are stable. Annotated tags are usually preferred because they carry metadata and can be signed.
Code/Syntax Justification:
git tag -a v1.8.0 -m "release v1.8.0"
🔍 Behind the Question: Release management and version pinning. ❌ The Red Flags: Using branches instead of tags to identify releases.
Q66: How do you recover an accidentally deleted branch?
Technical Answer: Use reflog or other references to find the branch tip commit, then recreate the branch from that hash. The key is that the commit often still exists even after the branch ref is deleted.
Code/Syntax Justification:
git reflog
git branch recovered <sha>
🔍 Behind the Question: Git recovery and object retention. ❌ The Red Flags: Assuming branch deletion destroys the commits immediately.
Q67: Why is repository hygiene important for performance?
Technical Answer: Unused refs, stale objects, and large binaries slow clone, fetch, and local maintenance operations. Keeping the repository lean improves developer productivity and CI performance.
Code/Syntax Justification:
git prune
git gc --aggressive
🔍 Behind the Question: Repo health and operational cost. ❌ The Red Flags: Letting binary blobs accumulate unchecked.
Q68: What is the difference between origin/main and main?
Technical Answer: main is your local branch, while origin/main is the remote-tracking branch representing the last fetched state from the remote. They may diverge until you fetch, pull, or push.
Code/Syntax Justification:
git log main..origin/main --oneline
🔍 Behind the Question: Local versus remote branch state. ❌ The Red Flags: Treating remote-tracking refs as editable local branches.
Q69: How does git diff --word-diff help review?
Technical Answer: Word-level diff makes small textual edits easier to inspect, especially in docs or dense code where line diffs are noisy. It is a review aid, not a substitute for understanding semantic changes.
Code/Syntax Justification:
git diff --word-diff
🔍 Behind the Question: Review ergonomics. ❌ The Red Flags: Reading only line-level diffs for subtle text changes.
Q70: What is a patch file in Git workflows?
Technical Answer: A patch serializes one or more commits into text so changes can be transferred, reviewed, or applied elsewhere. It is useful for email-based workflows or offline code sharing.
Code/Syntax Justification:
git format-patch -1 HEAD
🔍 Behind the Question: Portable change distribution. ❌ The Red Flags: Confusing patches with branch history.
Q71: Why do teams use conventional commit messages?
Technical Answer: Conventional commits make history machine-readable for changelog generation, release automation, and commit scanning. They also force clearer intent in human review.
Code/Syntax Justification:
fix(auth): prevent session leak
🔍 Behind the Question: Release automation and history clarity. ❌ The Red Flags: Random, unstructured commit messages.
Q72: What is the downside of squashing everything?
Technical Answer: Squashing makes history cleaner but can hide the progression of decisions and make debugging harder. Teams often prefer squashing feature branches while preserving meaningful merges in mainline history.
Code/Syntax Justification:
git merge --squash feature
🔍 Behind the Question: History readability versus traceability. ❌ The Red Flags: Flattening all history regardless of debugging needs.
Q73: How do you audit who changed what in a file over time?
Technical Answer: Combine log, blame, and file history views to understand how a file evolved and where a change came from. This is essential when tracing regressions or ownership questions.
Code/Syntax Justification:
git log --follow -- path/to/file
🔍 Behind the Question: Code provenance and change tracking. ❌ The Red Flags: Looking only at the latest diff without history.
🧠 Section 4: Massive Expansion
Q74: What is git switch and why did Git add it?
Technical Answer:
git switch separates branch checkout from file checkout, making branch changes safer and clearer. It reduces the overload that git checkout used to have.
Code/Syntax Justification:
git switch feature/login
🔍 Behind the Question: Command ergonomics.
❌ The Red Flags: Using checkout for everything without knowing the newer split.
Q75: What is git restore used for?
Technical Answer:
restore is the modern way to discard or unstage file changes without switching branches. It makes intent explicit.
Code/Syntax Justification:
git restore --staged app.py
🔍 Behind the Question: Intent clarity. ❌ The Red Flags: Reaching for reset when restore is safer.
Q76: How does the Git object model support history integrity?
Technical Answer: Git stores content as hashed objects, so any change produces a different hash. That makes history content-addressed and tamper-evident.
Code/Syntax Justification:
git cat-file -p <sha>
🔍 Behind the Question: Content-addressed storage. ❌ The Red Flags: Thinking commit hashes are arbitrary labels.
Q77: What is the difference between HEAD, branch refs, and detached HEAD?
Technical Answer: HEAD points to the current checkout target. Usually it references a branch, but in detached HEAD it points directly to a commit.
Code/Syntax Justification:
git switch --detach <sha>
🔍 Behind the Question: Reference semantics. ❌ The Red Flags: Panicking when HEAD is detached.
Q78: What is the practical value of git stash?
Technical Answer: It shelves local work so you can switch contexts without committing unfinished changes. It is for short interruptions, not long-term branch management.
Code/Syntax Justification:
git stash push -m "wip"
🔍 Behind the Question: Context switching. ❌ The Red Flags: Using stash as a hidden branch strategy.
Q79: How do you inspect changes with git diff effectively?
Technical Answer:
Use diff to compare working tree, index, commits, or branches depending on the question you are answering. The key is knowing which two snapshots are being compared.
Code/Syntax Justification:
git diff --staged
🔍 Behind the Question: Snapshot comparison. ❌ The Red Flags: Reading diffs without knowing the compared states.
Q80: What is the index in Git?
Technical Answer: The index is the staging area between working tree and commit. It lets you build a commit from selected changes rather than the whole directory.
Code/Syntax Justification:
git add path/to/file
🔍 Behind the Question: Three-tree model. ❌ The Red Flags: Treating staging as an implementation detail.
Q81: How does git add -p improve commit quality?
Technical Answer: It lets you stage hunks interactively so unrelated changes can be split into clean commits. Better boundaries help review and bisect later.
Code/Syntax Justification:
git add -p
🔍 Behind the Question: Commit hygiene. ❌ The Red Flags: Creating giant mixed-purpose commits.
Q82: What problem does git worktree solve?
Technical Answer:
worktree lets you check out multiple branches into separate directories from one clone. It is useful when you need to work on a hotfix without disturbing the current branch.
Code/Syntax Justification:
git worktree add ../hotfix hotfix-branch
🔍 Behind the Question: Parallel branch workflows. ❌ The Red Flags: Cloning the repository repeatedly for each branch.
Q83: What is the difference between fast-forward and no-fast-forward merges?
Technical Answer: A fast-forward merge moves the branch pointer when no divergence exists. A no-fast-forward merge always creates a merge commit, preserving the branch join.
Code/Syntax Justification:
git merge --no-ff feature
🔍 Behind the Question: History topology choices. ❌ The Red Flags: Not knowing when a merge commit is intentional.
Q84: Why can rebasing shared branches be dangerous?
Technical Answer: Rebase rewrites commit IDs, which can invalidate other developers’ local history and create coordination problems. It is safest on private work.
Code/Syntax Justification:
git rebase main
🔍 Behind the Question: Collaboration safety. ❌ The Red Flags: Rebasing after teammates have built on your branch.
Q85: How does git cherry-pick interact with conflict resolution?
Technical Answer: It applies a commit’s patch onto another branch, so conflicts are resolved in the target branch context. The semantics may differ from the original branch.
Code/Syntax Justification:
git cherry-pick <sha>
🔍 Behind the Question: Selective backporting. ❌ The Red Flags: Assuming the same fix means the same merge result.
Q86: What is git bisect run used for?
Technical Answer: It automates bisect by running a script or test at each midpoint commit. That makes regression hunting scalable.
Code/Syntax Justification:
git bisect run npm test
🔍 Behind the Question: Automation in debugging. ❌ The Red Flags: Manually stepping through dozens of commits when a script exists.
Q87: What does git blame tell you and what does it not tell you?
Technical Answer: It shows the last commit that touched each line, which helps trace provenance. It does not explain intent or correctness.
Code/Syntax Justification:
git blame path/to/file
🔍 Behind the Question: Line provenance. ❌ The Red Flags: Using blame to assign responsibility without context.
Q88: What is a bare repository?
Technical Answer: A bare repository contains Git metadata only, without a checked-out working tree. It is commonly used as a central remote.
Code/Syntax Justification:
git init --bare repo.git
🔍 Behind the Question: Remote repository structure. ❌ The Red Flags: Confusing bare repos with empty repos.
Q89: How do you recover from an accidental hard reset?
Technical Answer:
Use reflog to locate the previous HEAD or branch tip, then reset or branch from that commit. Git records reference movement even after a reset.
Code/Syntax Justification:
git reflog
git reset --hard HEAD@{1}
🔍 Behind the Question: Disaster recovery. ❌ The Red Flags: Assuming the data is permanently lost immediately.
Q90: What is the difference between git clean and git reset?
Technical Answer:
reset changes tracked content state, while clean removes untracked files and directories. clean can delete local files permanently.
Code/Syntax Justification:
git clean -fd
🔍 Behind the Question: Working tree hygiene. ❌ The Red Flags: Running clean without checking what it removes.
Q91: What are submodules and why do they cause trouble?
Technical Answer: Submodules pin one repository inside another at a specific commit. They add complexity because the parent repo tracks a commit pointer rather than the full nested state.
Code/Syntax Justification:
git submodule update --init --recursive
🔍 Behind the Question: Repository composition. ❌ The Red Flags: Adding submodules without operational ownership.
Q92: What is git rev-parse used for?
Technical Answer: It resolves Git names, refs, and paths into canonical values like commit hashes or repository paths. It is useful in scripts.
Code/Syntax Justification:
git rev-parse HEAD
🔍 Behind the Question: Scriptable reference resolution. ❌ The Red Flags: Hardcoding abbreviated hashes in automation.
Q93: How does git fetch differ from git pull?
Technical Answer:
fetch downloads remote updates without changing your current branch, while pull fetches and then integrates them. Fetch is safer when you want to inspect first.
Code/Syntax Justification:
git fetch origin
🔍 Behind the Question: Remote synchronization semantics. ❌ The Red Flags: Pulling blindly when you only wanted to inspect.
Q94: What is git push --force-with-lease for?
Technical Answer: It updates a remote branch only if the remote still points to the expected commit, which helps avoid overwriting teammates’ work.
Code/Syntax Justification:
git push --force-with-lease
🔍 Behind the Question: Safe history rewriting. ❌ The Red Flags: Treating force push as routine collaboration.
Q95: What is the purpose of tags in release workflows?
Technical Answer: Tags mark important commits like releases so builds and deployments can reference immutable version points.
Code/Syntax Justification:
git tag -a v1.4.0 -m "release v1.4.0"
🔍 Behind the Question: Release marking. ❌ The Red Flags: Using branches instead of tags for release identity.
Q96: What is the difference between annotated and lightweight tags?
Technical Answer: Annotated tags are full Git objects with metadata, while lightweight tags are just refs to a commit.
Code/Syntax Justification:
git tag -a v2.0.0 -m "v2.0.0"
🔍 Behind the Question: Tag object semantics. ❌ The Red Flags: Using lightweight tags for important releases without understanding the tradeoff.
Q97: How do you inspect the exact changes introduced by a commit?
Technical Answer:
Use show to inspect the commit metadata and patch in one place.
Code/Syntax Justification:
git show <sha>
🔍 Behind the Question: Commit-level review. ❌ The Red Flags: Looking only at branch summaries instead of the patch.
Q98: What is git log --graph useful for?
Technical Answer: It visualizes branch topology and merge history so you can reason about how work flowed through the repository.
Code/Syntax Justification:
git log --graph --oneline --decorate --all
🔍 Behind the Question: History visualization. ❌ The Red Flags: Ignoring topology when debugging branch behavior.
Q99: How do you compare two branches correctly?
Technical Answer: Compare the merge base and branch tips to understand what is unique to each side.
Code/Syntax Justification:
git diff main...feature
🔍 Behind the Question: Branch divergence analysis. ❌ The Red Flags: Comparing only tip-to-tip when histories have diverged deeply.
Q100: What is a merge base?
Technical Answer: The merge base is the best common ancestor commit between two branches. Git uses it for three-way merges and comparisons.
Code/Syntax Justification:
git merge-base main feature
🔍 Behind the Question: Merge mechanics. ❌ The Red Flags: Not understanding what Git compares during a merge.
Q101: What does git rerere do?
Technical Answer: It records how you resolved a conflict and can reuse that resolution when the same conflict appears again.
Code/Syntax Justification:
git config rerere.enabled true
🔍 Behind the Question: Conflict resolution reuse. ❌ The Red Flags: Manually resolving the same conflict over and over.
Q102: What is the purpose of git notes?
Technical Answer:
notes let you attach metadata to commits without changing the commit itself.
Code/Syntax Justification:
git notes add -m "reviewed by security"
🔍 Behind the Question: Non-invasive metadata. ❌ The Red Flags: Putting review context only in commit messages when commit data should stay immutable.
Q103: How do commit signatures improve trust?
Technical Answer: Signed commits help verify that a commit was authored or approved by a trusted key.
Code/Syntax Justification:
git commit -S -m "fix auth bug"
🔍 Behind the Question: Integrity and provenance. ❌ The Red Flags: Assuming Git history is authenticated by default.
Q104: What is git describe good for?
Technical Answer: It turns a commit into a human-friendly version-like identifier based on the nearest reachable tag.
Code/Syntax Justification:
git describe --tags --always
🔍 Behind the Question: Version identification. ❌ The Red Flags: Using raw hashes everywhere when a descriptive version is enough.
Q105: How do you rewrite multiple commits safely?
Technical Answer: Use interactive rebase locally, or create new commits that replace the old ones if the branch is already shared.
Code/Syntax Justification:
git rebase -i HEAD~4
🔍 Behind the Question: Controlled history editing. ❌ The Red Flags: Rewriting public history without warning.
Q106: What is the difference between merge conflicts and semantic conflicts?
Technical Answer: Merge conflicts are textual overlaps Git cannot auto-resolve. Semantic conflicts are logic problems where the code merges cleanly but the resulting behavior is wrong.
Code/Syntax Justification:
git diff --check
🔍 Behind the Question: Correctness beyond syntax. ❌ The Red Flags: Assuming a clean merge means the change is correct.
Q107: Why do teams often prefer linear history?
Technical Answer: Linear history simplifies bisect, review, and reasoning about order of changes.
Code/Syntax Justification:
git pull --rebase
🔍 Behind the Question: History readability. ❌ The Red Flags: Forcing linear history in a way that hides important structure.
Q108: What is git archive used for?
Technical Answer: It exports tracked files from a commit or tree without Git metadata, useful for packaging source snapshots.
Code/Syntax Justification:
git archive --format=zip HEAD > source.zip
🔍 Behind the Question: Source snapshot distribution. ❌ The Red Flags: Confusing an archive with a full clone.
Q109: How do hooks help enforce commit message standards?
Technical Answer: Commit-msg hooks can validate patterns like Conventional Commits before the commit is accepted.
Code/Syntax Justification:
#!/bin/sh
grep -Eq '^(feat|fix|docs):' "$1"
🔍 Behind the Question: Workflow policy enforcement. ❌ The Red Flags: Assuming humans will always follow message rules unaided.
Q110: What is the purpose of git sparse-checkout?
Technical Answer: It materializes only part of a large repository in your working tree, improving focus and performance.
Code/Syntax Justification:
git sparse-checkout set src/app
🔍 Behind the Question: Monorepo ergonomics. ❌ The Red Flags: Cloning huge repositories fully when you only need a few directories.
Q111: How does git pull --rebase differ from git pull?
Technical Answer:
Default pull merges remote changes into your branch, while --rebase replays your local commits on top of the fetched tip.
Code/Syntax Justification:
git pull --rebase origin main
🔍 Behind the Question: Synchronization strategy. ❌ The Red Flags: Using rebase pull without understanding commit rewriting.
Q112: What is git fsck used for?
Technical Answer:
fsck checks repository object integrity and can surface missing or corrupt objects.
Code/Syntax Justification:
git fsck --full
🔍 Behind the Question: Repository health validation. ❌ The Red Flags: Ignoring corruption until the repo becomes unusable.
Q113: What is a refspec?
Technical Answer: Refspecs define how local refs map to remote refs during fetch or push.
Code/Syntax Justification:
git fetch origin refs/heads/*:refs/remotes/origin/*
🔍 Behind the Question: Remote reference mapping. ❌ The Red Flags: Treating fetch and push mappings as magic.
Q114: Why is git log --follow useful for renamed files?
Technical Answer: It traces history across renames so you can see the full evolution of a file.
Code/Syntax Justification:
git log --follow -- path/to/file
🔍 Behind the Question: History continuity. ❌ The Red Flags: Assuming rename breaks all past history.
Q115: How do you inspect unmerged paths during a conflict?
Technical Answer: Use status and diff tools to see which files are unmerged and what each side contributed.
Code/Syntax Justification:
git status
git diff --ours
git diff --theirs
🔍 Behind the Question: Conflict investigation. ❌ The Red Flags: Resolving without comparing both sides.
Q116: What is git blame -L useful for?
Technical Answer: It narrows blame to a line range, making provenance checks easier on large files.
Code/Syntax Justification:
git blame -L 20,40 app.py
🔍 Behind the Question: Focused provenance lookup. ❌ The Red Flags: Running full-file blame when only a small range matters.
Q117: What is the role of .gitignore versus assume-unchanged?
Technical Answer:
.gitignore prevents new untracked files from being added; assume-unchanged is a local hint for tracked files and should not be used as real ignore policy.
Code/Syntax Justification:
git update-index --assume-unchanged config.local
🔍 Behind the Question: Ignore semantics. ❌ The Red Flags: Using local index flags as team policy.
Q118: How do you identify the current branch state in scripts?
Technical Answer: Use ref resolution commands instead of parsing human output. Scripted Git should rely on machine-readable refs and exit codes.
Code/Syntax Justification:
git symbolic-ref --short HEAD
🔍 Behind the Question: Automation safety.
❌ The Red Flags: Parsing git status text in brittle scripts.
Q119: What is git merge --abort for?
Technical Answer: It stops an in-progress merge and returns the working tree to the pre-merge state when possible.
Code/Syntax Justification:
git merge --abort
🔍 Behind the Question: Recovery during merge failure. ❌ The Red Flags: Manually deleting files to escape a bad merge state.
Q120: What is git replace?
Technical Answer:
replace lets you virtually substitute one object for another locally, useful for repository surgery or experiments.
Code/Syntax Justification:
git replace <old> <new>
🔍 Behind the Question: Object-level history manipulation. ❌ The Red Flags: Using replacement refs as shared history.
Q121: Why is commit granularity important?
Technical Answer: Small focused commits make review, revert, and bisect much easier. They also document intent better than giant mixed-purpose commits.
Code/Syntax Justification:
git commit -m "refactor auth middleware"
🔍 Behind the Question: Change management discipline. ❌ The Red Flags: Bundling unrelated changes together.
Q122: How do you validate a repository before handing it off?
Technical Answer: Check status, confirm the intended branch and remote, review recent commits, and verify no WIP or secrets remain.
Code/Syntax Justification:
git status
git log --oneline -5
🔍 Behind the Question: Repository hygiene. ❌ The Red Flags: Sharing a dirty branch without review.
Q123: What is the strongest senior-level Git answer in interviews?
Technical Answer: Git is a content-addressed DAG with refs pointing to immutable objects, and operational choices are about history shape, collaboration safety, and recovery.
Code/Syntax Justification:
git reflog
git bisect
git worktree add
🔍 Behind the Question: Deep operational fluency. ❌ The Red Flags: Knowing commands without understanding their effects on history.
🚀 Section 4: Expert Extensions
Q124: Why is git status still important for advanced users?
Technical Answer: It gives a quick, trustworthy snapshot of staged, unstaged, and untracked changes before you do anything destructive or publish history. Senior users rely on it because it reduces accidental commits and helps verify the working tree state.
Code/Syntax Justification:
git status --short
🔍 Behind the Question: Repository safety and working-tree awareness. ❌ The Red Flags: Skipping status checks before committing or rebasing.
Q125: What problem does git stash solve, and what are its tradeoffs?
Technical Answer: Stash temporarily shelves local work so you can switch branches or pull changes without committing half-finished edits. The tradeoff is that stashed changes can be forgotten, so it is best used for short-lived context switches, not long-term storage.
Code/Syntax Justification:
git stash push -m "wip auth fix"
🔍 Behind the Question: Work interruption handling. ❌ The Red Flags: Treating stash as a substitute for commits.
Q126: How does git worktree help with parallel work?
Technical Answer: Worktrees let you check out multiple branches from the same repository into separate directories without cloning again. This is useful when you need to review a branch, fix a hotfix, or compare histories in parallel.
Code/Syntax Justification:
git worktree add ../hotfix hotfix-branch
🔍 Behind the Question: Efficient branch-level multitasking. ❌ The Red Flags: Creating extra clones for every small task.
Q127: Why is git fetch safer than git pull in some workflows?
Technical Answer:
fetch updates remote-tracking refs without changing your current branch, which lets you inspect incoming changes before integrating them. That makes it safer when you need a controlled review step before merge or rebase.
Code/Syntax Justification:
git fetch origin
🔍 Behind the Question: Controlled synchronization. ❌ The Red Flags: Pulling blindly into a dirty or sensitive branch.
Q128: What is a detached HEAD, and when is it useful?
Technical Answer: A detached HEAD means you are checked out at a commit instead of a branch tip. It is useful for inspection, bisect, or testing a historical commit, but you must create a branch if you want to keep new work.
Code/Syntax Justification:
git checkout <commit-sha>
🔍 Behind the Question: Commit-level navigation. ❌ The Red Flags: Making changes in detached HEAD and forgetting to branch.
Q129: What does git clean do, and why is it dangerous?
Technical Answer:
clean removes untracked files and directories, which is useful for resetting generated artifacts. It is dangerous because it deletes local work that was never staged or committed.
Code/Syntax Justification:
git clean -fd
🔍 Behind the Question: Workspace cleanup. ❌ The Red Flags: Running it without first checking what will be removed.
Q130: How do you use git blame constructively in a team?
Technical Answer:
blame helps trace when and why a line changed so you can understand intent, regression origins, or context for a fix. It should be used for investigation, not as a way to assign fault to teammates.
Code/Syntax Justification:
git blame -L 20,40 app.py
🔍 Behind the Question: Change provenance and debugging. ❌ The Red Flags: Using blame as a social weapon.
Q131: What is the purpose of signed commits?
Technical Answer: Signed commits provide cryptographic verification that a specific key authored the commit. They improve supply-chain trust and help teams verify provenance in sensitive workflows.
Code/Syntax Justification:
git commit -S -m "release patch"
🔍 Behind the Question: Provenance and trust. ❌ The Red Flags: Confusing signature checks with code review.
Q132: What is the difference between origin/main and main?
Technical Answer:
main is your local branch, while origin/main is your local view of the remote-tracking branch. They may differ until you fetch or pull, so they should not be treated as the same pointer.
Code/Syntax Justification:
git log main..origin/main
🔍 Behind the Question: Local vs remote branch state. ❌ The Red Flags: Assuming remote branches update automatically.
Q133: Why can git push --force-with-lease be safer than --force?
Technical Answer:
--force-with-lease refuses to overwrite remote changes you have not seen, which protects teammates from accidental history loss. It is still a history rewrite, but with a safety check.
Code/Syntax Justification:
git push --force-with-lease
🔍 Behind the Question: Safer history rewriting. ❌ The Red Flags: Using raw force push on shared branches.
Q134: What is the role of git tag in release management?
Technical Answer: Tags mark specific commits, usually releases, so you can refer to stable points in history. Annotated tags are preferable for releases because they carry metadata and are better suited for release workflows.
Code/Syntax Justification:
git tag -a v1.2.0 -m "release v1.2.0"
🔍 Behind the Question: Release traceability. ❌ The Red Flags: Using mutable branch names as release markers.
Q135: What does git restore solve compared with older reset-based habits?
Technical Answer:
restore makes intent clearer by separating file restoration from branch movement. It is safer for common workflows like discarding unstaged changes or unstage/revert file-level edits without resetting history.
Code/Syntax Justification:
git restore --staged file.py
🔍 Behind the Question: Command clarity and safer file operations.
❌ The Red Flags: Using reset for everything without understanding scope.
Q136: How do you inspect the exact contents of a commit?
Technical Answer: Use show or low-level object inspection to review the patch, metadata, and affected files. This is useful for code review, release audits, and debugging suspicious history.
Code/Syntax Justification:
git show --stat <commit-sha>
🔍 Behind the Question: Commit introspection. ❌ The Red Flags: Reviewing only the commit title.
Q137: What is the benefit of git log --graph?
Technical Answer: It visualizes branch structure, merges, and divergence so you can reason about history shape quickly. It is especially useful in complex repositories with multiple feature and release branches.
Code/Syntax Justification:
git log --graph --oneline --decorate --all
🔍 Behind the Question: History visualization. ❌ The Red Flags: Reading commit history without understanding topology.
Q138: When would you choose rebase over merge in a team setting?
Technical Answer: Use rebase for your own private branch when you want a clean linear history before review or merge. Use merge when you need to preserve branch context or you are integrating shared work that others already pulled.
Code/Syntax Justification:
git rebase origin/main
🔍 Behind the Question: Collaboration-safe history shaping. ❌ The Red Flags: Rebasing published shared branches.
Q139: What is a submodule, and why do teams avoid them sometimes?
Technical Answer: A submodule pins one repository inside another at a specific commit. Teams avoid them when they want simpler cloning, updating, and onboarding because submodules add extra operational complexity.
Code/Syntax Justification:
git submodule add <repo-url>
🔍 Behind the Question: Dependency pinning across repositories. ❌ The Red Flags: Using submodules without clear ownership rules.
Q140: How does git cherry differ from cherry-pick?
Technical Answer:
cherry compares two branches and shows which commits are present on one side but not the other. cherry-pick copies commits. The distinction matters when you want analysis versus history rewriting.
Code/Syntax Justification:
git cherry main feature
🔍 Behind the Question: Branch comparison vs commit application. ❌ The Red Flags: Using the wrong tool for diffing branches.
Q141: Why do merge conflicts often happen on long-lived branches?
Technical Answer: Long-lived branches diverge more, so overlapping edits accumulate and the merge base becomes older. Frequent integration reduces conflict size and makes resolution easier because changes stay closer to current mainline state.
Code/Syntax Justification:
git merge origin/main
🔍 Behind the Question: Branch divergence management. ❌ The Red Flags: Letting feature branches drift for weeks without syncing.
Q142: What is the operational value of git notes?
Technical Answer: Git notes attach metadata to commits without changing commit hashes. They are useful for audit information, review annotations, or internal process metadata that should not rewrite history.
Code/Syntax Justification:
git notes add -m "needs security review" <commit-sha>
🔍 Behind the Question: Non-invasive metadata. ❌ The Red Flags: Overloading commit messages with everything.
Q143: What is the strongest concise senior summary of Git?
Technical Answer: Git is a content-addressed object graph with refs, and expert usage is about preserving intent, managing collaboration risk, and recovering safely when history moves.
Code/Syntax Justification:
git reflog
git worktree add
git push --force-with-lease
🔍 Behind the Question: Mastery of history, workflow, and recovery. ❌ The Red Flags: Treating Git as just a commit button.
Senior / Principal — Real World Git Scenarios
Q144: Accidental commit of secrets to main branch
Level: Senior
Scenario: A developer pushes an .env with database credentials to main. The CI pipeline already ran. The secret is in Git history.
Impact: Credential exposure. Incident response. Key rotation required.
Root Cause: No pre-commit secret scanning. .env not gitignored. No branch protection requiring PR review.
Simple explanation: You mailed a postcard with your house key code to the whole team. Even if you recall it, others read it.
Technical Answer:
- Rotate all exposed credentials immediately (assume compromise).
- Remove from history:
git filter-repo --path .env --invert-paths(coordinated force push). - Add
.envto.gitignore. - Add pre-commit hook (
detect-secrets,truffleHog) to block secrets. - Enable branch protection: require PR review, require status checks, no direct push to
main.
Code/Syntax Justification:
git filter-repo --path .env --invert-paths --force
git push origin main --force # coordinate with team first
Workflow Diagram:
Secret pushed to main
|
v
Rotate credentials immediately
|
v
Rewrite history (filter-repo/BFG)
|
v
Force push (team coordination)
|
v
Pre-commit hook + branch protection
🔍 Behind the Question: Secret removal from history, key rotation, pre-commit hooks, branch protection.
❌ The Red Flags: .env not gitignored; direct push to main; no secret scanning; not rotating after leak.
Q145: Merge conflict hell — long-lived feature branch
Level: Principal
Scenario: A feature branch lives for 3 weeks. main advances with 50 commits. Merging the feature branch produces 40 conflict files. The merge takes days and introduces bugs.
Impact: Delayed releases. Merge bugs. "Merge Friday" deployment fear.
Root Cause: Long-lived branches diverge too far from main. No regular rebase/merge of main into the feature branch. Small, frequent integrations skipped.
Simple explanation: You build a house addition for 3 weeks while the main house gets remodeled. When you try to connect them, nothing lines up.
Technical Answer:
- Short-lived branches: merge to
mainwithin 1-2 days. - Rebase or merge
maininto feature branch daily to stay current. - Use trunk-based development with feature flags for large features.
- Small PRs (<400 lines) reviewed and merged quickly.
- CI runs on every push to catch conflicts early.
Code/Syntax Justification:
# Daily rebase to stay current
git checkout feature-branch
git fetch origin
git rebase origin/main
git push --force-with-lease
Workflow Diagram:
Long-lived branch (3 weeks): Short-lived branch (1 day):
main diverges 50 commits main diverges 2 commits
40 conflicts on merge 0-2 conflicts on merge
days to resolve minutes to resolve
🔍 Behind the Question: Branch lifetime, rebase vs merge, trunk-based development, small PRs, CI on every push.
❌ The Red Flags: Feature branches living weeks; rebasing only at the end; large PRs; no CI on feature branches.