Git
Fundamentals
What is Git and how is it different from GitHub?
- Git: A distributed version control system that tracks changes in source code.
- GitHub/GitLab/Bitbucket: Hosting platforms for Git repositories with additional features (PRs, issues, CI/CD).
Key concepts:
- Repository: Project folder tracked by Git
- Commit: Snapshot of changes
- Branch: Independent line of development
- Remote: Server-hosted copy of the repository
What problems does this solve?
- Clarifies the absolute foundational distinction between the local version control engine and the external cloud hosting infrastructure.
What are the three states/areas in Git?
- Working Directory: Your actual files
- Staging Area (Index): Changes marked for next commit
- Repository: Committed history
What problems does this solve?
- Explains the exact geographical mechanics of the working directory, the staging index, and the permanent repository history.
What is the difference between git fetch and git pull?
git fetch: Downloads changes from remote but doesn't merge them. Safe to run anytime.git pull: Downloads AND merges changes (git fetch+git merge).
Best practice: Use fetch first to review changes, then merge/rebase manually.
What problems does this solve?
- Highlights the critical difference between safely downloading remote updates versus aggressively merging them directly into the current branch.
What is the difference between git merge and git rebase?
Both integrate changes from one branch into another.
Merge: Creates a merge commit, preserves branch history.
Rebase: Moves commits to the tip of the base branch, linear history.
Rule: Never rebase public/shared branches.
What problems does this solve?
- Evaluates the architectural tradeoff between preserving exact historical merge commits versus maintaining a perfectly linear, sequential history.
What is a merge conflict and how do you resolve it?
Occurs when Git can't automatically merge changes (same lines modified differently).
Tools: VS Code, IntelliJ, git mergetool
What problems does this solve?
- Demonstrates the ability to manually untangle overlapping code modifications when automatic deterministic merging fails.
What is HEAD in Git?
A pointer to the current commit/branch you're on.
What problems does this solve?
- Explains the symbolic pointer that fundamentally dictates exactly what branch or specific commit the working directory is currently observing.
What is .gitignore?
A file specifying patterns for files/directories Git should ignore.
Note: Files already tracked aren't affected. Use git rm --cached <file> to untrack.
What problems does this solve?
- Prevents sensitive environment variables, massive dependencies, and compiled build outputs from accidentally polluting the repository.
Branching Strategies
What is Git Flow?
A branching model with structured branch types:
Branches:
main: Production-ready codedevelop: Integration branchfeature/*: New featuresrelease/*: Prepare releaseshotfix/*: Emergency fixes
Criticism: Too complex for many projects, slows deployment.
What problems does this solve?
- Evaluates familiarity with strict, legacy release-branching methodologies commonly used in large, slow-moving enterprise environments.
What is Trunk-Based Development?
All developers commit to a single branch (main/trunk) with short-lived feature branches.
Practices:
- Small, frequent commits
- Feature flags for incomplete work
- Branch lifespan < 2 days
- Fast CI/CD pipeline
Preferred for: Continuous deployment, smaller teams.
What problems does this solve?
- Highlights the modern, rapid-iteration continuous integration methodology of aggressively merging small changes directly into main.
What is a Pull Request (PR) / Merge Request (MR)?
A request to merge changes from one branch into another with code review.
PR workflow:
- Create feature branch
- Push commits
- Open PR on GitHub/GitLab
- Code review + discussions
- Address feedback
- Approve and merge
- Delete feature branch
Best practices:
- Small, focused PRs
- Descriptive titles and descriptions
- Link to issues/tickets
- Include tests
What problems does this solve?
- Explains the fundamental asynchronous peer-review gateway that protects the primary codebase from unverified or destructive code.
Essential Commands
How do you undo changes in Git?
Discard unstaged changes:
Unstage files:
Undo commits:
What problems does this solve?
- Demonstrates mastery over retrieving lost work, un-staging accidents, or nuking local uncommitted mistakes entirely.
What is the difference between git reset and git revert?
| Command | Action | History | Use when |
|---|---|---|---|
reset | Moves HEAD backward | Rewrites history | Local/private branches |
revert | Creates undo commit | Preserves history | Public/shared branches |
What problems does this solve?
- Crucially distinguishes between aggressively erasing local commits versus safely appending anti-commits to a public history branch.
What is git stash?
Temporarily saves uncommitted changes without committing.
Use case: Switch branches without committing incomplete work.
What problems does this solve?
- Provides a clean mechanism to explicitly vault uncommitted, work-in-progress code temporarily to urgently switch branches.
What is git cherry-pick?
Apply a specific commit from another branch to your current branch.
Use cases:
- Backport a bug fix to an older release
- Grab a specific feature without merging entire branch
What problems does this solve?
- Surgically extracts one specific, highly isolated commit from a completely different branch and duplicates it onto the current timeline.
What is git bisect?
Binary search to find the commit that introduced a bug.
What problems does this solve?
- Utilizes a powerful binary search algorithm to deterministically hunt down the exact historical commit that introduced a subtle bug.
What is git reflog?
Records all HEAD movements (commits, checkouts, resets). Your safety net.
Use case: Recover from accidental git reset --hard or deleted branch.
What problems does this solve?
- Acts as the ultimate safety net, allowing recovery of "deleted" commits or branches that have been orphaned from the chronological log.
How do you rewrite commit history?
Amend last commit:
Interactive rebase: Edit multiple commits
⚠️ Never rewrite history on public branches.
What problems does this solve?
- Demonstrates raw power-user capability to retroactively squash, edit, or reorder local commits before pushing cleanly to a public remote.
What is git log and useful options?
What problems does this solve?
- Explains how to visually traverse, filter, and audit massive project histories using explicit graph topography.
Advanced Topics
What is a Git hook?
Scripts that run automatically on Git events.
Client-side hooks (.git/hooks/):
pre-commit: Before commit (linting, tests)commit-msg: Validate commit messagepre-push: Before push (run tests)
Server-side hooks:
pre-receive: Before accepting pushpost-receive: After accepting push (deploy)
Tools: Husky, lint-staged (easier hook management)
What problems does this solve?
- Empowers teams to strictly enforce code formatting, linting, or test execution automatically just before a commit or push occurs.
What is a Git submodule?
Include another Git repository as a subdirectory.
Use cases: Shared libraries, monorepo components.
Downsides: Complex, easy to get out of sync. Consider npm packages instead.
What problems does this solve?
- Allows massive monorepos to securely embed entire separate Git repositories completely inside a specific sub-directory.
What is git worktree?
Manage multiple working directories from one repository.
Use case: Work on multiple branches simultaneously without stashing.
What problems does this solve?
- Enables checking out completely different branches simultaneously in separate physical directories without ever touching the stash.
What is the difference between ~ and ^?
Both navigate commit ancestry:
~(tilde): Linear ancestry (first parent)^(caret): Specific parent (for merge commits)
What problems does this solve?
- Clarifies the obscure but critical syntactical difference between horizontal branch traversal and vertical commit traversal.
What is a bare repository?
A repository without a working directory (only .git contents).
Use case: Central/remote repositories (e.g., what GitHub hosts).
What problems does this solve?
- Explains the architecture of central remote servers, which contain zero working files and exist purely to accept and store pushed commits.
What is git blame?
Shows who last modified each line of a file.
What problems does this solve?
- Provides forensic accountability by explicitly revealing the exact author and historical commit hash for every single line of a file.
How do you clean up a repository?
What problems does this solve?
- Demonstrates how to aggressively purge untracked files, orphaned branches, and dangling garbage to drastically reduce repository bloat.
Collaboration
What is git remote?
Manage connections to remote repositories.
What problems does this solve?
- Explains the explicit DNS pointers that connect a local offline repository to various upstream cloud endpoints.
What is a fork workflow?
Workflow for contributing to repositories you don't own.
What problems does this solve?
- Exposes the decentralized open-source collaboration model of copying a repository, pushing changes locally, and submitting up-stream PRs.
What are signed commits?
Cryptographically verify commit authorship using GPG.
Benefit: Proves commits are from who they claim to be.
What problems does this solve?
- Uses cryptographic GPG signatures to categorically prove that a specific commit was authored by a specific, verified human.
What is git tag?
Mark specific commits (usually releases).
What problems does this solve?
- Creates strictly immutable, human-readable semantic versioning pointers anchored to incredibly specific release commits.
How do you write good commit messages?
Format:
Example:
Conventional Commits types:
feat: New featurefix: Bug fixdocs: Documentationstyle: Formatting (no code change)refactor: Code restructuringtest: Adding testschore: Maintenance
What problems does this solve?
- Ensures permanent repository history remains easily traversable, highly descriptive, and fundamentally understandable for future engineers.