My account

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.

Not reviewedWritten with AI helpReading time: 14 minOnline shop exampleGit commands

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)
  1. Nobody can tell which commit changed the shipping price.
  2. The big commit mixes the new checkout, a library update and the shipping change. We cannot undo only the bad part.
  3. 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:

  1. A commit is a snapshot of all files at one moment, plus a message, an author, and a link to its parent commit.
  2. 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.
  3. 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”.
C1C2C3C4C5C6e4a1c079b3f2d151c8e9a0d7a6b3c2e94f8mainfix/shipping-priceHEADEach arrow points to the parent commit.
Commits point back to their parents. A branch is just a label on one commit. Making a branch is cheap: Git writes one small pointer.

Because of this:

  1. Creating a branch is cheap. Git does not copy files.
  2. 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.
  3. “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
  1. One branch, one task. A branch that lives for one or two days is easy to review and easy to merge.
  2. Pull main often. The longer a branch lives, the more it drifts from main, and the bigger the conflicts become.
  3. 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)
  1. The first line is a short summary. Many teams keep it around 50 characters and write it as a command: “Add”, “Fix”, “Remove”.
  2. Then a blank line, then the reason. The code shows what changed. The message must say why: the bug, the ticket, the decision.
  3. 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: a new commit with two parentsABCDXYMThe merge commitYour commits, unchangedRebase: your commits copied on top of mainABCDX'Y'New copies with new idsOne straight line
Merge keeps the real history and adds a merge commit. Rebase copies your commits on top of main, with new ids.
  1. 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.
  2. 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.
The golden rule of rebase. Never rebase or force push a branch that other people use, like main or develop. Their machines still have the old commits. Their next pull brings strange conflicts and duplicate commits, and a force push can delete the work they pushed in the meantime. On your own branch, before others build on it, rebase is fine.

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.

Clean up before the pull request. On your own branch, you can tidy messy commits with an interactive rebase (git rebase -i): join “fix” commits into the commit they fix, and reword unclear messages. Many teams also use “squash merge” on the server, which turns the whole pull request into one commit on main.

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
Reset: move the label backABCmainCommit C is no longer on the branch.People who have C now see a different history.Revert: add a commit that undoesABCDmainCommit D undoes C.Nobody's history breaks.
Reset moves the label back and the commit disappears from the branch. Revert adds a new commit that undoes the change, so nobody's history breaks.

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

  1. Never commit secrets: passwords, API keys, tokens. Put local settings files in .gitignore, and keep secrets in a secret store or environment variables.
  2. 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.
  3. 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

  1. A commit is a snapshot with a parent and an id. A branch is only a label on a commit.
  2. Work on short-lived branches, pull main often, and merge through a pull request.
  3. Make small commits that do one thing, with a message that says why.
  4. Merge is always safe. Rebase rewrites commits, so use it only on your own branch, then push with force-with-lease.
  5. On shared branches undo with revert, not reset. A “lost” commit can usually be found with reflog.
  6. Never commit secrets. If one was pushed, change it right away.

Done reading this lesson?Mark it as finished so it shows in your lessons list and report card.