(the post is automatically translated by AI)
Introduction
With the plugin development mostly done, the next task is to merge two parallel branches of work. Git provides two approaches for this: merge and rebase.
In this article, I’ll explain how rebase works and illustrate it with a practical example from our development workflow.
How It Works
git rebase replays commits from one branch on top of another, using the target branch as the new base. Suppose you have two branches A and B with diverging histories — you need to integrate their differences.
- If you want to use branch A as the base, first switch to branch A:
❯ git checkout A
- Confirm HEAD is on branch A:
❯ git status
On branch A
nothing to commit, working tree clean
- Rebase B onto A (run this from branch B):
❯ # (on branch B)
❯ git rebase A
If there are conflicts, resolve them one by one following git’s output messages.
After resolving each conflict, stage the file:
❯ git add <modified file name>
- Repeat steps 4–5, then continue the rebase until all conflicts are resolved:
❯ git rebase --continue
Example
Suppose my partner and I are each working on an independent feature, me on branch A and them on branch B. Their feature A is done and I want to include it in my branch B. I can use rebase to bring their work into my current branch.
A simplified example: the develop branch has d.cpp. In separate branches, I add a.cpp to branch A and b.cpp to branch B.
- Original
d.cpp(branchdevelop):
#include <iostream>
int main()
{
std::cout << "D" << std::endl;
}
d.cppon branch A:
#include <iostream>
int main()
{
std::cout << "A" << std::endl;
}
d.cppon branch B:
#include <iostream>
int main()
{
std::cout << "B" << std::endl;
}
Rebasing B onto A causes a conflict in d.cpp:
Auto-merging d.cpp
CONFLICT (content): Merge conflict in d.cpp
error: could not apply 1b2cb82... Update B
Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply 1b2cb82... Update B
- Open
d.cppand resolve the conflict:
#include <iostream>
int main()
{
<<<<<<< HEAD
std::cout << "A" << std::endl;
=======
std::cout << "B" << std::endl;
>>>>>>> 1b2cb82 (Update B)
}
- Keep the version you want, then stage the file.
- Run
git rebase --continueand repeat step 5 until all conflicts are resolved. - Done.