Version Control with Git
Track changes, branch safely, resolve conflicts and keep a clean history with Git.
Beginner · about 8 hours · difficulty 2/5
Learn first: Linux and the Command Line
Git records snapshots of your project so you can go back in time, work on features in isolation and collaborate without overwriting each other. Every professional backend team uses it.
The mental model
A commit is a snapshot plus a pointer to its parent. A branch is just a movable label pointing at a commit. HEAD is where you are now. The staging area (index) lets you choose exactly what goes into the next commit. Once this clicks, commands stop feeling magical.
Everyday workflow
git status,git add -p,git commit,git log --oneline --graphgit switch -c feature/xto branch,git mergeorgit rebaseto integrategit pull,git push, and working with a remote calledorigingit stashto park unfinished work,git restoreto discard changes
Resolving conflicts
Conflicts happen when two branches change the same lines. Git marks the region; you edit the file to the correct final state, stage it and continue. Small, frequent commits and short-lived branches keep conflicts rare.
Pitfalls
- Committing secrets such as
.envfiles. Add them to.gitignorefrom day one; removing a secret from history is painful and the secret should be rotated anyway. - Rewriting history (
rebase,push --force) on branches others use. - Giant commits with messages like "fix". Write messages that explain why.
You are ready to move on when you can branch, commit, rebase onto main, resolve a conflict and recover a "lost" commit with git reflog.
Key concepts
- Commits, staging and history
- Branching and merging
- Rebasing
- Resolving merge conflicts
- .gitignore
- Remotes: push, pull, fetch
- reflog and undoing mistakes
- Conventional commit messages
Mini project
Create a repo, make two branches that edit the same function differently, merge them to produce a conflict, resolve it, then practice an interactive rebase to squash commits.
Check yourself
What is the difference between git merge and git rebase, and when would you avoid rebasing?
Free resources
- Pro Git (free book) (git-scm.com)
- Learn Git Branching (learngitbranching.js.org)