Git: A Comprehensive Guide for Developers
From core concepts to advanced workflows — branching, rebasing, cherry-picking, merge conflicts, and clean history with hooks and PRs.

Git is the most widely used version control system, yet many developers only learn a handful of commands. This guide goes from the core model to advanced workflows so you can use Git with confidence rather than fear.
1. The core model
- **Commit** — a snapshot of your files with a parent pointer.
- **Branch** — a lightweight movable pointer to a commit.
- **HEAD** — where you currently are.
- **Index/Staging** — what will go into the next commit.
bash
git init
git add -p # stage hunks interactively
git commit -m "message"
2. Branching and merging
Branches are cheap. Use one per feature:
bash
git switch -c feature/login
# ... work ...
git switch main
git merge feature/login
A fast-forward merge just moves the pointer; a merge commit records that two histories joined.
3. Rebasing
Rebase replays your commits onto another base, producing a linear history.
bash
git fetch origin
git rebase origin/main
Rule of thumb: rebase local commits; never rebase commits others have pulled.
4. Cherry-picking
Apply a single commit from another branch:
bash
git cherry-pick a1b2c3d
Useful for hotfixes you want in multiple branches without a full merge.
5. Merge conflicts
When Git can't auto-combine, it marks the file. Resolve, stage, and continue (see the dedicated merge-conflicts guide). The key is to understand both sides before choosing.
6. Keeping history clean
- One logical change per commit.
- Write messages that explain *why*, not just *what*.
- Use `git commit --amend` and interactive rebase to tidy before sharing:
bash
git rebase -i HEAD~5
7. Hooks for automation
Hooks run on events. A pre-commit hook can lint and format so bad code never enters history:
bash
# .git/hooks/pre-commit
npm run lint && npm run format
Or use Husky + lint-staged for a managed setup.
8. Pull requests and collaboration
- Push a branch and open a PR.
- Keep PRs small and focused for fast review.
- Use branch protection (required reviews, passing CI).
Quick reference
| Task | Command |
|---|---|
| New branch | git switch -c name |
| See status | git status |
| Stage part | git add -p |
| Linearize | git rebase |
| Copy commit | git cherry-pick |
| Undo commit | git revert |
Mastering Git isn't about memorizing commands — it's about understanding that commits form a graph you can reshape. Once that clicks, every workflow becomes a tool, not a mystery.