Git rebase with dependent branches

Oct 8, 2022 · 2 min read

git rebase is always an important part of my workflow. As a maintainer of repositories at work, browsing commit history of a major part of my work. Thus, keeping the history “clean” is important. By “clean”, it means

  • code can be compiled and unit tests passes, and
  • code changes of a commit is complete and can be understood as a whole (not necessarily a complete feature but completeness on code level)

During active development in a feature branch, I prefer to commit small and often. This creates a lot of commits which it useful for me to understand the trail and to change the ordering of the commits. However, these small commits are probably only useful during active development but not so much when debugging the same code a few months later. Thus, my practice is always to rebase (in other words, re-writing the history of) the feature branch before creating a pull request.

Another reason for using rebase is to avoid any merge commits in a feature branch. I understand this controversial and it also depends on team’s policy. Personally, I do not see any use cases of having merge commits in a feature branch and I think it is only acceptable in main (or master). Thus, I will have a practice to rebase a feature branch before merging it into main.

There are times when development of a feature feature-b depends on another feature feature-b which is also in development. In this case, I would branch out from feature-a to create feature-b.

original feature-b

This works fine until I want to patch one of the commits originated from feature-a.

feature-b with fixup

In such case, I will probably have to checkout feature-a, create a fixup commit, rebase feature-a to squash the fixup commit, checkout feature-b again, and rebase it against feature-a. The following shows the trail of commands involved. This is pretty involved.

git checkout feature-a
vim code.cs
git add code.cs
git commit --fixup feature-a~
git rebase --autosquash -i master
git checkout feature-b
git rebase -i feature-a

Thanks to the option of update-refs of rebase in the latest version (2.38) of git, such patch can be done directly on feature-b. The trail of commands look like the following.

vim code.cs
git add code.cs
git commit --fixup feature-a~
git rebase --autosquash --update-refs -i master

Since feature-b has a reference of feature-a, option --update-refs will be able to help rebasing feature-a as well. This is very useful command when dealing with multiple development branches and it could save me a lot of time!

rebase command and message

rebased feature-b

rebased feature-a

Hope this helps you as well.

Git
Alex Ho
Authors
Software Developer
Experienced software engineer with interests in web and cloud technologies