What is Git and why do we use it?
Git is a distributed version control system that stores snapshots of your entire project at every commit. Every developer has the full history on their own machine. Key benefits: go back to any point in time, work on multiple features (branches), collaborate without overwriting each other’s work.
What are the three object types Git stores internally?
Blob (raw file content, no filename), Tree (directory snapshot — maps filenames to blob/tree hashes), Commit (pointer to root tree + parent commit + author + message). All stored in .git/objects/ addressed by SHA-1 hash.
What is a Git branch really?
A branch is literally just a tiny text file inside .git/refs/heads/ containing a commit hash. Creating a branch is instant and costs almost nothing.
What is HEAD in Git?
HEAD is the file .git/HEAD. It normally holds ‘ref: refs/heads/main’, meaning ‘I am currently on branch main’. When you commit, the current branch pointer moves forward automatically.
What is ‘detached HEAD’ state?
You checked out a commit directly (not a branch), e.g. git checkout 9f8e7d6. Any commits made here become orphaned if you switch away. Fix: git switch -c rescued-work to create a branch from it.
What are the three zones in Git’s file lifecycle?
Working Directory (your edits on disk) → Staging Area / Index (.git/index — what will go into next commit) → Repository (.git/objects — committed snapshots).
What does git add -p do?
Interactive staging. Git shows each changed chunk (hunk) and asks: stage it? (y/n/s to split). Lets you commit only part of a file for clean, focused commits.
How do you unstage a file without losing your changes?
git restore –staged Student.cs (modern syntax) OR git reset HEAD Student.cs (older syntax)
How do you discard working directory changes to a file?
git restore Student.cs (modern) OR git checkout – Student.cs (older). WARNING: changes are gone permanently.
What makes a good commit message?
It should complete the sentence ‘If applied, this commit will…’. Be specific. Bad: ‘fix’. Good: ‘Add null-check in StudentController POST handler’. Each commit = one logical change.
What does git commit -am do?
Stages ALL already-tracked (modified) files and commits in one step. It does NOT include brand new untracked files.
What is a fast-forward merge?
When the branch being merged into (e.g. main) has no new commits since the feature branch was created. Git simply moves the main pointer forward — no merge commit is created, history stays linear.
What is a 3-way merge?
When both branches have new commits since they diverged. Git finds the common ancestor, compares both tips, and creates a new merge commit with two parents.
How do you force a merge commit even when fast-forward is possible?
git merge –no-ff feature/login — the –no-ff flag prevents fast-forward and always creates a merge commit.
What is git rebase and when should you use it?
Rebase replays your commits on top of another branch, giving a clean linear history. Use it for local, not-yet-pushed work to tidy history before sharing. NEVER rebase commits others have already fetched.
What is the golden rule of rebase?
Never rebase commits that have already been pushed and others may have fetched. Rebase only your local, unpushed work.
How do you push after rebasing a branch that was already pushed?
git push –force-with-lease origin feature/branch — safer than –force because it checks nobody else pushed since your last fetch.
What does interactive rebase (git rebase -i HEAD~4) let you do?
Edit, reword, squash, fixup, or drop any of the last 4 commits. Use ‘squash’ to merge a commit into the previous one (combines messages), ‘fixup’ to merge but discard the message.
How do you resolve a merge conflict step by step?
What do conflict markers mean?
«««< HEAD = your current branch version. ======= = separator.»_space;»»> feature/x = incoming branch version. You decide what the final code should be.
What is git fetch vs git pull?
git fetch downloads remote changes into remote-tracking branches (origin/main) without touching your working directory. git pull = fetch + merge/rebase in one step. Prefer fetch + review + merge for safety.
What does git stash do?
Temporarily shelves your uncommitted changes so you can switch branches cleanly. git stash pop restores them.
What is git blame?
Shows every line of a file annotated with who wrote it, which commit, and when. git blame -L 10,20 file.cs shows only lines 10–20.
What commands show commit history?
git log (full), git log –oneline (compact), git log –oneline –graph –all –decorate (branch graph — memorise this!), git show <hash> (one commit detail).</hash>