Rebasing is a powerful feature in version control systems like Git that allows developers to integrate changes from one branch into another. However, when multiple developers are working on the same codebase, conflicts can arise. This article presents three practical examples of rebasing with conflicts, illustrating common scenarios and how to resolve them effectively.
In a development team, a feature branch is created to add a new login feature. Meanwhile, another developer modifies the same section of code in the main branch.
The development process proceeds as follows:
feature/login
and makes changes to the login.js
file.main
branch, also updates the login.js
file by fixing a bug.feature/login
onto the latest main
branch.During the rebase, a conflict occurs because both branches modified the same lines in login.js
.
git checkout feature/login
and git rebase main
login.js
.Developer A opens login.js
, which contains conflict markers:
// HEAD
const user = authenticateUser();
// Changes from main
const user = validateUser();
Developer A decides to combine both functionalities into:
const user = authenticateUser() || validateUser();
After editing, Developer A marks the conflict as resolved: git add login.js
git rebase --continue
git mergetool
for a visual conflict resolution experience.In a scenario where multiple developers are updating configuration files simultaneously, conflicts can arise during rebasing.
feature/analytics
branch, where they modify config.json
to add analytics settings.feature/performance
branch and makes changes to the same config.json
file to enhance performance settings.feature/analytics
onto the main
branch, conflicts occur in config.json
.git checkout feature/analytics
and git rebase main
config.json
.Developer C opens the file and sees:
// HEAD
{
"performance": {
"enabled": true
}
}
// Changes from main
{
"analytics": {
"enabled": true
}
}
Developer C merges both changes:
{
"performance": {
"enabled": true
},
"analytics": {
"enabled": true
}
}
After saving, Developer C marks the conflict as resolved with git add config.json
.
git rebase --continue
When updating documentation, conflicts may arise if two developers modify the same document simultaneously.
README.md
file in the feature/docs
branch to add new installation instructions.README.md
file in the feature/updates
branch to revise existing usage examples.feature/docs
onto the main
branch and encounters a conflict.git checkout feature/docs
and git rebase main
README.md
.Developer E opens the file and sees:
# Installation
- Install via NPM
# Usage
- Example usage here...
// Changes from main
# Installation
- Install globally using NPM
Developer E edits to integrate both changes:
# Installation
- Install globally using NPM
- Install via NPM
# Usage
- Example usage here...
After resolving, Developer E stages the changes with git add README.md
.
git rebase --continue
By understanding these practical examples of rebasing with conflicts, developers can better navigate version control challenges and maintain a smoother workflow.