What is the difference between Git merge and Git rebase
· Category: Git
Short answer
git merge creates a merge commit and preserves branch history, while git rebase replays commits on top of another branch for a linear history. Use merge for shared branches and rebase for local cleanup. When merge conflicts arise, see how to resolve Git merge conflicts. To move individual commits between branches, see how to cherry-pick a commit in Git.
Steps
- Merge approach:
git checkout main, thengit merge feature-branch - Rebase approach:
git checkout feature-branch, thengit rebase main - Resolve any conflicts during the process
- Continue rebase if needed:
git rebase --continue - Push changes; use
git push --force-with-leaseafter rebasing if already pushed
Tips
- Never rebase public branches that others are working on
- For fixing commits before merging, see how to amend a Git commit
- To connect your local branch properly, make sure you add a remote repository