Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

The Short Answer

  • git merge: joins two branches and keeps every commit as it is. Safe on any branch.
  • git rebase: moves your commits so they start from the tip of another branch. The commits get new hashes, so use it only on commits nobody else has.

The starting point for both is a feature branch that split from main, while main moved on:

          A---B---C  feature
         /
    D---E---F---G    main

What Merge Does

git switch main
git merge feature

Git creates a merge commit with two parents, G and C. Nothing that existed is changed:

          A---B---C
         /         \
    D---E---F---G---M  main

The history shows that work happened in parallel, and the merge commit marks where it came together. Undoing the whole feature later is one command: revert the merge commit, as shown in git revert.

Fast-Forward Merges

If main has not moved since the branch started, there is nothing to join. Git moves main forward to C and makes no merge commit. Two options change that:

  • --no-ff: always makes a merge commit, so the feature stays visible as a group.
  • --ff-only: merges only when a fast-forward is possible and refuses otherwise. This is a good choice on a server that deploys with git pull.

What Rebase Does

git switch feature
git rebase main
  1. Git sets aside the commits of feature that are not on main: A, B, and C.
  2. It moves feature to the tip of main, G.
  3. It applies the set-aside changes one by one, creating new commits.
    D---E---F---G---A'--B'--C'  feature

A' contains the same change as A, but it is a new commit. A merge into main afterwards is a fast-forward, and the history is one straight line. If a step conflicts, fix the files, git add them, and run git rebase --continue; git rebase --abort returns to the start.

When to Use Which

SituationUse
Finished feature into mainMerge
Update your own branchRebase
Branch others push toMerge
Tidy commits before reviewInteractive rebase
Pull on a deploy serverFast-forward only

Many teams combine the two: rebase your own branch onto main while you work, then merge it when it is done. Tidying commits is covered in how to squash commits in Git.

The Rule for Shared Branches

Never rebase commits that others have already pulled. Their clones still hold the old commits, and when they pull, the old and new versions of the same work meet, which leads to duplicate commits and conflicts. If you rebased your own branch after pushing it, push with git push --force-with-lease, which refuses when someone else has pushed in the meantime. Never use a plain --force, and never force-push main.

If a rebase went wrong, the branch before it is in the reflog: git reflog lists it, and git reset takes you back.

git pull --rebase

git pull is a fetch followed by an update of your branch. When you and a colleague both committed to the same branch, the histories have diverged: current Git versions then stop and ask you to choose merging or rebasing, unless you have set a default. Merging adds a small merge commit each time. With rebase, your local commits are replayed on top of what you fetched instead:

git pull --rebase
git config --global pull.rebase true

The second line makes it the default for all your repositories. It only replays your local commits that are not on the remote yet, so nobody else's commits are rewritten. For what fetch and pull do, see git pull vs git fetch.

Was this answer helpful?
Loading...