2025-03-16 Web Development, Programming, Productivity
Understanding Git Rebase
By O. Wolfson
What is Git Rebase?
Git rebase is a powerful tool that helps you update your branch by applying its changes on top of another branch, typically the latest version of main
or develop
. Unlike git merge
, which creates a new merge commit, rebasing keeps the commit history cleaner and more linear.
Why Use Git Rebase?
- Keeps history clean – Avoids unnecessary merge commits.
- Improves readability – Creates a straight-line commit history.
- Helps prevent conflicts early – Incorporates upstream changes incrementally.
- Useful for squashing commits – Allows you to combine multiple commits before merging.
When to Use Git Rebase
1. Updating Your Feature Branch
If you're working on a feature branch and the main
branch has new updates, rebase your branch before merging:
shgit checkout feature-branch git fetch origin git rebase origin/main
This applies your commits on top of the latest main
branch.
2. Squashing Commits Before Merging
If you’ve made multiple commits that should be combined into one, use interactive rebase:
shgit rebase -i HEAD~3 # Squashes the last 3 commits
Then, choose squash
(or s
) for commits you want to merge.
3. Avoiding Merge Commits
Instead of merging a feature branch, you can rebase to keep history linear:
shgit checkout main git pull origin main git checkout feature-branch git rebase main
When Not to Use Git Rebase
- On shared branches – Rebasing changes commit history, which can cause conflicts if others have based work on the original branch.
- If unsure – If history rewriting isn't necessary, merging might be simpler.
- For long-running feature branches – If rebasing causes too many conflicts, consider merging instead.
How to Handle Conflicts During Rebase
If you encounter conflicts, Git will pause the rebase and prompt you to resolve them:
- Manually fix the conflicts in your files.
- Mark the conflicts as resolved:
sh
git add . git rebase --continue
- If something goes wrong, abort the rebase:
sh
git rebase --abort
Step-by-Step Rebase Exercise
Follow this exercise to practice Git rebase in a controlled environment.
Setup
- Create a new directory and initialize a Git repository:
sh
mkdir git-rebase-exercise && cd git-rebase-exercise git init
- Create and switch to a new branch:
sh
git checkout -b feature-branch
- Create a new file and commit it:
sh
echo "Initial content" > file.txt git add file.txt git commit -m "Initial commit on feature branch"
Simulating Main Branch Updates
- Switch back to
main
and add a change:shgit checkout main echo "Main branch update" > file.txt git add file.txt git commit -m "Update from main branch"
Rebasing Feature Branch
- Switch back to
feature-branch
and start the rebase:shgit checkout feature-branch git rebase main
- If there are conflicts, resolve them manually in
file.txt
and continue:shgit add file.txt git rebase --continue
- Verify that history is clean by checking the log:
sh
git log --oneline --graph
Conclusion
Git rebase is a great tool for keeping your commit history clean and avoiding unnecessary merge commits. However, it should be used carefully to avoid disrupting shared branches. By understanding when and how to use it, you can work more efficiently and keep your Git history readable.