Handling Conflicts During Git Rebase
In the previous blog, we learned the basics of Git rebase. Now let’s explore what happens when conflicts occur during a rebase, and how to resolve or abort them.
Scenario Setup
We start with a project (project7) and the first commit:
echo "Hello" > hello.txt
git add hello.txt
git commit -m "Initial commit"
At this point, main has one commit with hello.txt.
Creating a Feature Branch
From the initial commit, create a feature branch:
git switch -C feature
Add some files and commit:
echo "file 1" > 1.txt
echo "file 2" > 2.txt
echo "file 3" > 3.txt
git add .
git commit -m "Add files 1, 2, 3"
echo "New line added in feature branch" >> hello.txt
git commit -am "Update hello.txt in feature branch"
Now feature has diverged from main.
Updating the Main Branch
On main, modify the same hello.txt:
git switch main
echo "Okay" >> hello.txt
git commit -am "Update hello.txt in main branch"
Now both branches modified hello.txt differently — a classic setup for a conflict.
Rebasing the Feature Branch
Switch back to feature and rebase onto main:
git switch feature
git rebase main
Conflict occurs:
CONFLICT (content): Merge conflict in hello.txt
Aborting the Rebase
If you don’t want to fix the conflict right now, abort:
git rebase --abort
This resets your branch to its state before the rebase began. Very useful when you want to postpone resolving conflicts.
Resolving the Conflict
Let’s try rebasing again:
git rebase main
Check status:
git status
# both modified: hello.txt
Open hello.txt:
Hello
<<<<<<< HEAD
Okay
=======
New line added in feature branch
>>>>>>> feature
Decide how to resolve it. For example, keep a clean file:
Hello
Okay
New line added in feature branch
Stage it:
git add hello.txt
Continuing the Rebase
Now continue the rebase:
git rebase --continue
Git will ask for a commit message:
Update hello.txt in feature branch
Save and exit (:wq).
Rebase is complete, and history has been rewritten. Now both main and feature share a linear history.
Summary
- During rebase, conflicts occur if both branches edit the same file.
- Use
git rebase --abortto stop and reset to the pre-rebase state. - Resolve conflicts manually in files, then
git addthem. - Run
git rebase --continueto finalize. - After rebase, history is rewritten to look linear.
Keep Learning 🚀
👉 Subscribe to Learning Ocean – Subscribers get coupon codes for my courses, early access to blogs and courses, and even exclusive YouTube videos.
👉 My YouTube Channel – More videos, more fun, and lots of learning!
👉 📺 Watch this topic in video form
Stay curious, keep coding, and let’s make learning fun together! 🎉