Interactive Git rebase for updating branches
Let's see how to do a git rebase in an example project. It is crucial knowing how to work with git because it is an essential tool in the front-end development for working in a team.
That topic is essential since it may have happened to you that you have to merge a new feature to the main branch. When this happens, the workflows only allow the pull request merge once the branch is updated with the main one.
To do this, we have two options: rebase and merge. Merging is not a good option as it creates another commit, not related to the new feature, to update. It will be tricky for the teammates to spot the changes regarding the specific feature. The git rebase is the solution to work well in those scenarios.
Rebase is better than merge for updating branches with the main one
By performing the rebase, our branch will be clean and present the commits that include that new feature, fix, etc.
Previous steps
If you want to follow this tutorial, you should do these steps before:
Install Git Lens extension for Visual Studio Code.
Run this command for setting up Visual Studio Code as default editor for git:
git config --global core.editor "code --wait"
Example 1
Let's start by visualizing these two branches:
We want to make the feature update with the development branch. We must be on the feature branch and then run the following command:
git rebase -i development
After running the previous command, you will see the Git Lens interface for rebase:
You can choose between some options to apply to each commit. In this example, we have one commit, and we can see the different options:
pick: if you want to keep this commit on the rebase.
reword: if you want to rename the commit.
edit: if you want to change the code in the commit.
drop: if you want to discard the commit.
We are going to choose the pick option, and then we must click on the "START REBASE" button. If we don't have conflicts with the development branch, the rebase will be successful.
Example 2
We are going to work with an example where we have conflicts with the development branch, and we have more than one commit where at least one of them has conflicts:
We run the git rebase command:
git rebase -i development
We will see the Git Lens interactive interface:
We choose the pick option for both because we want to keep both commits and click on the "START REBASE" button.
We have been notified we have conflicts in the second commit:
We must resolve all the conflicts. Once all conflicts have been resolved, we must add to the git staging area. Then, we run the following command to continue with the rebase process:
git rebase --continue
If we don't have conflicts anymore, we will end up in the rebase process. If we have more conflicts in other commits, we should follow the same process as the previous one until we finish with all.
That is all for today. I hope it was helpful.
It is essential knowing how to work well in those real scenarios to make the project development much more agile and efficient.
I hope you enjoyed the article.
Join my weekly newsletter, where I share articles to help you become a better front-end developer. You'll receive them directly to your inbox.
If you have any questions, feel free to contact me on LinkedIn, Edi Rodriguez, or leave a comment in the post.
See you in the next post.
Have a great day!