Git and version control
A commit is a snapshot with a parent, and a branch is only a label that points to a commit. With this picture in your head, merge, rebase and undo stop being scary. Keep small commits with clear messages, and never rewrite history that others already have.
Author: bezzad
Contents
The problem: a history nobody can read
A bug reached production in our online shop: the shipping price is wrong for some countries. It worked last week. The team opens the history of the last week:
a91f3c2 wip
7d2e810 fix
c04b5a1 more fixes
e66d0f9 merge
1b8c7a4 everything for the new checkout (3,214 lines changed)
- Nobody can tell which commit changed the shipping price.
- The big commit mixes the new checkout, a library update and the shipping change. We cannot undo only the bad part.
- On top of that, a teammate says their commit from yesterday is missing. Someone ran a force push on the shared branch.
Git did not fail here. The way the team used it did. Git is easy to use well once you see how it works inside.
How Git thinks
Three ideas explain almost everything:
- A commit is a snapshot of all files at one moment, plus a message, an author, and a link to its parent commit.
- Each commit has an id (a hash), like 1b8c7a4. The id is made from the content and the parent. If anything changes, the id changes.
- A branch is only a label that points to one commit. When you make a new commit on a branch, the label moves forward to it. HEAD is a pointer that says “you are here”.
Because of this:
- Creating a branch is cheap. Git does not copy files.
- A commit is almost never really lost. If no label points to it, it is still there for a while, and the reflog command can find it.
- “Rewriting history” means making new commits with new ids, and moving a label to them. The old commits still exist, but people who built on them now have a different history.
The daily workflow
Most teams use short-lived branches and pull requests:
git switch main
git pull # get the latest main
git switch -c fix/shipping-price # new branch for one task
# ... change code, run tests ...
git add -p # choose the changes for this commit
git commit -m "Use the country zone when calculating shipping"
git push -u origin fix/shipping-price
# open a pull request, get a review, merge
- One branch, one task. A branch that lives for one or two days is easy to review and easy to merge.
- Pull main often. The longer a branch lives, the more it drifts from main, and the bigger the conflicts become.
- The add command with -p shows each change and asks if it belongs in this commit. It helps you keep commits small and clean.
Good commits
A good commit does one thing and explains why.
Bad
- “fix”
- “wip”
- “changes”
- One commit with a new feature, a library update and a rename
Good
- “Use the country zone when calculating shipping”
- “Update the payment library to fix the timeout bug”
- “Rename Basket to Cart” (only the rename, nothing else)
- The first line is a short summary. Many teams keep it around 50 characters and write it as a command: “Add”, “Fix”, “Remove”.
- Then a blank line, then the reason. The code shows what changed. The message must say why: the bug, the ticket, the decision.
- Each commit should build and pass the tests. Then you can go back to any commit, and tools like bisect work well.
Use the country zone when calculating shipping
The price table is per zone, but we passed the country code.
Countries without their own row got the default price.
Fixes SHOP-412.
Merge and rebase
Your branch started from main. Meanwhile, others added commits to main. There are two ways to bring them together.
- Merge creates a new commit with two parents. The history shows exactly what happened. Nothing is rewritten, so it is always safe. The history has more lines and more merge commits.
- Rebase takes your commits and replays them, one by one, on top of the new main. The history becomes a straight line. But your commits are new commits with new ids.
After you rebase your own branch, a normal push is rejected, because the history changed. Use force-with-lease, not a plain force:
git fetch origin
git rebase origin/main
git push --force-with-lease
Force-with-lease pushes only if the remote branch is still where you last saw it. If someone else pushed in the meantime, it stops instead of deleting their work.
Undo safely
Choose the undo tool by one question: has anyone else got this commit?
| Situation | Command | Changes history? |
|---|---|---|
| Throw away changes in a file you have not committed | git restore with the file name | No |
| Fix the last commit, not pushed yet | git commit –amend | Yes, only local |
| Move your branch back, not pushed yet | git reset | Yes, only local |
| Undo a commit that is already on a shared branch | git revert with the commit id | No, it adds a new commit |
| Find a commit you “lost” | git reflog | No |
For our shipping bug on main, the right tool is revert. It adds a new commit that does the opposite of the bad one. Everyone can pull it normally.
Find the bad commit with bisect
When you know a good old version and a bad new version, bisect finds the commit that broke it. It is a binary search over the history:
git bisect start
git bisect bad # the current version has the bug
git bisect good v2.3.0 # this old version was fine
# Git checks out a commit in the middle. Test it, then say:
git bisect good # or: git bisect bad
# ... repeat until Git names the first bad commit ...
git bisect reset
With 1,000 commits between good and bad, bisect needs only about 10 steps. But it only helps if every commit is small and builds. A 3,000-line commit only tells you “the bug is somewhere in here”.
Secrets and big files
- Never commit secrets: passwords, API keys, tokens. Put local settings files in .gitignore, and keep secrets in a secret store or environment variables.
- If a secret was pushed, change it right away. Deleting it in a new commit is not enough, because it is still in the history. Even rewriting the history is not enough, because other people and services may already have a copy.
- Do not commit build outputs and large binary files. They make the repository big and slow for everyone. Build outputs can be made again from the code.
Common mistakes
| Mistake | Result | The right way |
|---|---|---|
| Messages like “fix” and “wip” | Nobody can find when and why something changed. | A short summary line, and the reason in the body. |
| One huge commit for many changes | It cannot be reviewed or reverted in parts. | One idea per commit. |
| A branch that lives for weeks | Big, painful conflicts at the end. | Small branches. Pull main often. |
| Rebase or force push on a shared branch | Teammates get conflicts. Commits can disappear. | Rebase only your own branch. Use force-with-lease. |
| Reset on a commit that is already pushed | Other people’s history no longer matches. | Use revert on shared branches. |
| A secret committed and then “deleted” | The secret is still in the history. | Change the secret. Add the file to .gitignore. |
| Panic after “losing” work | Hours spent writing the code again. | Look in git reflog first. |
Summary in six lines
- A commit is a snapshot with a parent and an id. A branch is only a label on a commit.
- Work on short-lived branches, pull main often, and merge through a pull request.
- Make small commits that do one thing, with a message that says why.
- Merge is always safe. Rebase rewrites commits, so use it only on your own branch, then push with force-with-lease.
- On shared branches undo with revert, not reset. A “lost” commit can usually be found with reflog.
- Never commit secrets. If one was pushed, change it right away.