Rebase or Merge for git?
1 min readMar 1, 2024
Here is what I follow:
In general: “rebase your feature branch, then merge to main branch”. However there are two exceptions:
- If your feature branch has multiple developers working on it, you should opt for merge instead of rebase, as rebase rewrites commit history and is unsafe for group collaboration feature branches. Most of the time though, you will be working on some features or fixes on a feature branch alone, in that case rebasing your feature branch before merging to main branch works the best, because you are the only developer for the feature branch thus rebase fits.
- If you submit PR(pull request) to upstream projects, you will always be asked for a rebase instead of merge, as the main branch gatekeeper will want to keep his branch history linear.
Three extra tips:
- Always do
git pull
beforegit push
git pull
is by default using merge, because merge is just “safer”.- use
git push --force-with-lease
instead ofgit push --force
after rebase or amend.