Version Control with Git
Core Git — the staging area, commits, branches, merge conflicts, remotes, stashing, and inspecting history.
7 questions
JuniorTheoryVery commonWhich git commands do you use daily?
Which git commands do you use daily?
Daily: git status/diff to inspect; add+commit -m to record; push/pull to sync; log --oneline --graph for history; switch -c for branches; merge/rebase to integrate; stash/stash pop to shelve; cherry-pick, blame, bisect for targeted ops.
Common mistakes
- ✗Using
git add .blindly — accidentally stages.env,*.o, or large binaries; always checkgit statusfirst and maintain a good.gitignore - ✗Confusing
git pullwithgit fetch—pull= fetch + merge (or rebase); usegit fetch+ inspect before merging to avoid surprise conflicts - ✗Losing stash entries —
git stashcreates a stack;git stash listshows all; stashes are not backed up on push; use a WIP commit instead for important work
Follow-up questions
- →What is the difference between
git reset --soft,--mixed, and--hard? - →How do you recover a dropped stash or a detached HEAD commit?
JuniorTheoryCommonWhat are the stages of committing changes in git?
What are the stages of committing changes in git?
Git has three areas: working tree (filesystem), index/staging (snapshot for next commit), repository (.git/objects). Workflow: edit files; git add copies a snapshot into the index; git commit creates a commit object from the index pointing to the parent.
Common mistakes
- ✗Forgetting
git addand wondering why the commit is empty —git commitonly commits what's in the index; usegit commit -ato auto-add tracked files (but it skips untracked) - ✗Confusing
git diffandgit diff HEAD—git diffcompares working tree to index;git diff HEADcompares working tree to the last commit (includes unstaged changes) - ✗Committing large binary files — Git stores the full content of every version; use Git LFS for large assets
Follow-up questions
- →How does Git store file content internally (blob, tree, commit, tag objects)?
- →What is the difference between
git commit --amendand creating a new commit?
JuniorTheoryCommonDifference between git fetch and git pull.
Difference between git fetch and git pull.
git fetch downloads new commits and refs from remote (updates origin/main) but does not touch the working tree or current branch. git pull = git fetch + git merge (or git rebase if pull.rebase=true).
Common mistakes
- ✗Running
git pullwithout checking what changed — can introduce unexpected merge conflicts in the middle of active work; usegit fetch+ review first - ✗Forgetting that
git fetchonly updates remote-tracking branches —origin/mainis updated, butmainstill points to the old commit until you merge/rebase - ✗Using
git pull --rebaseon shared branches — rebasing rewrites history; only rebase local-only commits, never commits that others have built on
Follow-up questions
- →How do you configure git to always rebase on pull with
git config pull.rebase true? - →What does
git remote update --prunedo and when is it useful?
JuniorTheoryCommonHow do you resolve a merge conflict?
How do you resolve a merge conflict?
Git marks conflicts with <<<<<<<, =======, >>>>>>>. Steps: (1) git status to see conflicts; (2) edit each file, pick a side or combine, remove markers; (3) git add <file>; (4) git commit (merge) or git rebase --continue (rebase).
Common mistakes
- ✗Accepting all 'ours' or 'theirs' without reading the conflict — silently drops the other developer's work; always read both sides
- ✗Committing conflict markers —
<<<<<<< HEADleft in code causes compile errors; usegit diff --checkin CI to detect them - ✗Resolving a conflict in a rebase and then running
git merge --continue— during rebase usegit rebase --continue; during merge usegit merge --continue; mixing them corrupts the operation
Follow-up questions
- →How does
git rerere(Reuse Recorded Resolution) help with repeated conflicts? - →What is a three-way merge and how does it differ from a two-way diff?
JuniorDebuggingOccasionalWhat does "detached HEAD" mean in git and how do you recover from it?
What does "detached HEAD" mean in git and how do you recover from it?
Detached HEAD means HEAD points at a commit, not a branch — common after git checkout <sha>. Commits there are reachable only via reflog and can be GC'd. Save with git switch -c new-branch; return via git switch main. Harmless unless you commit and forget.
Common mistakes
- ✗Committing in detached HEAD and switching away — commits lost (recovery via reflog)
- ✗Mistaking detached HEAD for a 'broken' state and resetting
- ✗Using
git checkoutfor both branch switching and inspection —git switch/git restore(C++17 of git 2.23+) clarifies intent
Follow-up questions
- →What is
git switch --detachand when do you use it intentionally? - →How do you find a 'lost' commit after leaving detached HEAD?
JuniorTheoryOccasionalWhat is SCM and why use version control?
What is SCM and why use version control?
SCM covers version control, build, release, and change tracking. VCS records each change with author and message, enabling rollback, parallel branches, and audit. Git is the dominant distributed VCS — every clone is a full repo.
Common mistakes
- ✗Committing directly to
mainwithout review — breaks CI for the whole team; use short-lived feature branches and pull requests - ✗Writing meaningless commit messages ('fix', 'update', 'stuff') — makes
git logandgit bisectuseless; use imperative mood describing what and why - ✗Committing generated files or build artefacts — they should be in
.gitignore; they bloat the repo and cause merge conflicts
Follow-up questions
- →What is the difference between a distributed VCS (Git) and a centralised VCS (SVN)?
- →How does
git bisectuse binary search to find the commit that introduced a bug?
JuniorTheoryOccasionalWhat does git stash do and when should you use it instead of a WIP commit?
What does git stash do and when should you use it instead of a WIP commit?
git stash saves working-tree and index changes onto a stack and reverts to HEAD; restore via stash pop/apply. Use for short interruptions (pulling, brief switch). For longer work prefer a WIP commit — stashes aren't in git log and get forgotten.
Common mistakes
- ✗Stashing untracked files without
--include-untrackedand losing them - ✗Forgetting which stash holds which work — they're indexed by number, not name
- ✗Using stash as long-term storage for in-progress features
Follow-up questions
- →What is
git stash branchand when is it useful? - →How does
git stash --keep-indexdiffer from a plain stash?