Rebasing without `git rebase`Freiburg, Germany
git workflow involves creating a lot of short-lived branches (a.k.a.
feature branches), switching between them and, sometimes, I need to rebase one
of these branches. git rebase is a super
useful git command and I recommend everyone to get more familiar with it (take a
look at git rebase in depth for instance).
My feature branches usually contain a single commit (of interest) and when there
are more commits, my team at $WORK uses the squash and
button anyway. In other words, I do not care much about the history of a
short-lived git branch. Using this approach, executing
git rebase is
straightforward and there is usually little to no conflict to handle.
That being said, it can be tricky to perform a rebase sometimes (for example,
when the main branch has changed a lot after someone ran a code formatter on
the entire codebase).
Such a situation could also occur when one merges the main branch into the
feature branch, resulting in a merge commit like
Merge branch 'main' into
Attempting a rebase in these situations will convince a lot of people that git
rebase is awful but we can achieve pretty much the same result using a different
git merge with the
--squash option. Here is how I proceed to
rebase a branch named
make sure the main branch is up to date locally first:
$ git checkout main $ git pull origin main
“rename” the branch to rebase to
$ git checkout feature-branch $ git branch --move feature-branch-2
recreate the branch
feature-branchbased on the main branch:
$ git checkout main $ git checkout -b feature-branch
git mergethe temporary branch with squash:
$ git merge --squash feature-branch-2
Now, commit and force push
feature-branchto update the Pull/Merge Request 🎉 The temporary branch can be safely deleted at this point.
That’s it! Note that some of the commands above could be combined to be more efficient but I do not use this procedure a lot so I do not mind.
Bonus: a few years ago I learned about
git commit --fixup so I often pass
git rebase (and sometimes
--autostash too). Take a look at
these options if you do not know about them already :)