SE

Git and version control

Must-know concept2 hBeginner

Commits, branches, merges, rebases, pull requests, and safe recovery.

Version control is the safety net for every serious codebase. Git lets you save small states, compare what changed, collaborate without overwriting people, and recover when a change goes sideways.

The big idea

Git is not just "upload code to GitHub." It is a local database of snapshots. Each commit points to the full project state, a parent commit, an author, and a message.

Working treeedited files
Indexstaged changes
Commitsaved snapshot
Branchmoving name
Remoteshared copy

The most important mental model: a branch is just a name pointing at a commit. When you commit, the branch name moves forward.

Daily loop

git status
git diff
git add app/api/orders.ts
git commit -m "Validate order payloads"
git pull --rebase
git push

Run git status before and after every risky command. It tells you which files are tracked, staged, modified, or untracked.

Merge, rebase, revert, reset

merge
Combine two branches with a merge commit. Good when history should show the join.
rebase
Replay your commits on top of a newer base. Good for keeping a feature branch clean before review.
revert
Create a new commit that undoes an old commit. Safe on shared branches.
reset
Move a branch pointer. Powerful, but dangerous if the commits were already pushed.

The safe default on shared branches is revert, not reset. Rewriting published history surprises everyone else.

Pull requests are communication

A good PR is not just a diff. It gives the reviewer context:

  • What changed and why.
  • What files or flows are most important.
  • How you tested it.
  • What trade-offs or follow-ups remain.

Small PRs get better review. If a change touches routing, database, UI, and deployment in one diff, split it unless the behavior truly cannot be separated.

Recovery moves

# See recent branch movements, even after mistakes.
git reflog
 
# Move a file out of staging without losing edits.
git restore --staged path/to/file.ts
 
# Undo local edits in one file after checking they are disposable.
git restore path/to/file.ts
 
# Make a safe undo commit for something already pushed.
git revert <commit-sha>

In practice

Practice Git on a throwaway repository: create branches, make conflicting edits, resolve a merge conflict, revert a commit, and recover a deleted branch from git reflog. You want these moves in muscle memory before a production incident.

Key takeaways

  • Git is a local snapshot database; GitHub is only one remote host for it.
  • Commit small, coherent changes with messages that explain intent.
  • Use rebase to clean a private branch; use revert to undo shared history safely.
  • A pull request is a communication artifact, not only a code diff.
  • When something goes wrong, inspect first: `status`, `diff`, `log`, then `reflog`.

Checkpoint questions

Use these to test whether the lesson is clear enough to explain without rereading.

  1. 1Can you explain what a commit stores and how it differs from a branch?
  2. 2When would you merge, rebase, cherry-pick, or revert?
  3. 3How would you recover from committing to the wrong branch without losing work?
  4. 4What should a pull request description tell a reviewer before they read the diff?

References

External resources for going deeper after the lesson above.