At Zensurance we use a tool quite often called rebasing which is one of the processes found in Git’s version control system. In this article we’ll look at what rebasing is as well as some benefits. If you’re a seasoned developer the information in this article may be nothing new but for developers early in their career this concept may be something you find interesting!
This article also assumes you know the basics of Git and general version control processes. I’ll include some links at the end of the article for further reading as well.
What is Rebasing?
Rebasing in Git is one of two ways a developer can merge changes from a feature branch onto the master branch (or any other branch for that matter) - the other way is to do a simple merge. We’ll focus on rebasing here but for more information on git merge, see the links section below.
To explain this in further detail, let’s work through an example. In this example, you created a feature branch off of the master branch. You’ve coded out some sick features, passed all your tests, added commits, and now it’s ready to go live. However, while you were working there was communication among the team that a bug showed up on the master branch but has been resolved. It’s possible that everything will still be okay, however the recommendation is that the team “rebases” their feature branches with the master branch.
So what’s happening when we rebase then? When you rebase your feature branch onto the master branch, you’re taking the current state of the branch you’re rebasing onto (the master branch in this case) then replaying your commits from the feature branch on top. The end result is a nice clean commit history. Using the example described above, after rebasing you can then run your tests again to make sure your changes are still in good shape giving you confidence that when it finally goes live it will be without bugs!
Git Commands for Rebasing
You can use the following commands to complete a rebase in git:
git checkout [name of feature branch]
git rebase master
At this point your feature branch is now rebased with master and you can complete the merge of your feature branch into master:
git checkout master
git merge [name of feature branch]
There is also the --rebase
flag you can add onto git pull
commands. To execute a rebase, you can run git pull origin master -- rebase
but make sure you’re on your feature branch.
A Heads Up!
When you rebase, the commits will be added on a per commit basis one by one. If conflicts arise, git will provide a message indicating there are conflicts which will need to be handled. Once the conflicts are resolved you can run git rebase --continue
to continue with the rebase.
Rebasing in git is pretty powerful and definitely helps with our workflows at Zensurance. I hope this introduction was helpful and perhaps spurs some discussion about adding it to your own workflows!
Happy coding!
Resources, Additional Reading: