CI/CD & Infrastructure as Code
Git Internals and Advanced Operations
4 min read
Git questions go beyond basic commands in DevOps/SRE interviews. Let's master the internals and advanced operations.
Git Object Model
Understanding Git's internals impresses interviewers:
Git stores 4 object types:
1. blob - File contents
2. tree - Directory structure
3. commit - Snapshot with metadata
4. tag - Named reference to commit
# View object type
git cat-file -t <sha>
# View object content
git cat-file -p <sha>
# List all objects
find .git/objects -type f
How Commits Work
┌──────────┐
│ commit │
│ (sha: a1)│
└────┬─────┘
│ points to
┌────▼─────┐
│ tree │
│ (sha: b2)│
└────┬─────┘
┌──────────┴──────────┐
┌────▼─────┐ ┌────▼─────┐
│ blob │ │ tree │
│ (file) │ │ (dir) │
└──────────┘ └──────────┘
Advanced Git Operations
Rebase vs Merge
| Aspect | Merge | Rebase |
|---|---|---|
| History | Preserves all commits | Linear history |
| Conflicts | Once during merge | Per commit |
| Safety | Safe for shared branches | Rewrites history |
| When to use | Shared branches | Local/feature branches |
# Interactive rebase (reorder, squash, edit commits)
git rebase -i HEAD~5
# Rebase onto main
git checkout feature
git rebase main
# NEVER rebase shared branches!
# "If in doubt, don't rebase"
Interview question: "When should you use rebase vs merge?"
Answer: Use rebase for local feature branches to maintain clean history. Use merge for shared/public branches to preserve history and avoid rewriting commits others depend on.
Cherry-Pick
# Apply specific commit(s) to current branch
git cherry-pick <commit-sha>
# Cherry-pick without committing
git cherry-pick --no-commit <sha>
# Cherry-pick range
git cherry-pick A^..B
Git Bisect
Find the commit that introduced a bug:
# Start bisect
git bisect start
# Mark current (broken) as bad
git bisect bad
# Mark known good commit
git bisect good <sha>
# Git checks out middle commit, test and mark:
git bisect good # or
git bisect bad
# Continue until Git finds the first bad commit
# When done:
git bisect reset
Reflog - Your Safety Net
# View reflog (history of HEAD movements)
git reflog
# Recover "deleted" commits
git checkout -b recovery <sha-from-reflog>
# Undo a bad rebase
git reset --hard HEAD@{5}
Git Workflow Strategies
Trunk-Based Development
main ─────●─────●─────●─────●─────●
\ /
●─────●───
(short-lived feature branch)
- Direct commits to main
- Feature flags for incomplete features
- Requires good CI/CD and testing
- Used by Google, Facebook
GitFlow
main ─────●───────────────●─────────
\ /
develop ───●───●───●───●───●───●───●───
\ \ /
feature ●───● ●───●
release ●───●───●
- Separate develop and main branches
- Feature, release, hotfix branches
- Better for scheduled releases
- More complex, more overhead
GitHub Flow
main ─────●─────●─────●─────●─────●
\ / \ /
feature ●───●─── ●───●
(PR) (PR)
- Simple: main + feature branches
- Pull requests for review
- Deploy from main
- Good balance for most teams
Interview Questions
Q: "How would you recover a commit after running git reset --hard?"
# Check reflog for the lost commit
git reflog
# Find the commit SHA before the reset
# Create a branch from it
git checkout -b recovery <sha>
# Or reset back to it
git reset --hard <sha>
Q: "What's the difference between git reset and git revert?"
| Operation | Effect | History | Use Case |
|---|---|---|---|
reset |
Moves HEAD, can discard commits | Rewrites | Local cleanup |
revert |
Creates new commit undoing changes | Preserves | Shared branches |
# Reset: moves HEAD backward (rewrites history)
git reset --hard HEAD~3
# Revert: creates new commit (safe for shared)
git revert <sha>
Q: "You accidentally pushed secrets to a public repo. How do you fix it?"
# 1. Remove from history using filter-branch or BFG
git filter-branch --force --index-filter \
"git rm --cached --ignore-unmatch secrets.env" HEAD
# Or use BFG Repo Cleaner (faster)
bfg --delete-files secrets.env
# 2. Force push (coordinate with team!)
git push --force-with-lease
# 3. Rotate all exposed credentials immediately!
# 4. GitHub may cache - contact support
Next, we'll dive into CI/CD pipeline design—a core DevOps/SRE competency. :::