Oscar the owl and Vershd logo

Git Rebase Instead of Merge

2nd March 2020

This is part of a Git Tips series. For the complete set for beginners and intermediate Git users, see our white paper.

The Problem

Sometimes after merging a branch, you realise that you’d like to create a nice linear history, as you would if you had rebased that branch with git rebase, instead of merging it.

Depending on your team’s outlook, this is either a sensible ‘clean repository’ step, or a significant step to obsessive compulsive disorder. The diagram shows the situation, whereby you want to move the commits of my-branch onto master in one seamless line.

It’s important to recognise that this will create new commit hashes, so you should be wary of doing this on a branch that has been published. If you do, then other people’s history will change, and that may cause havoc for them.

The Solution

This checks out the commit and allows you to work on it, and its ancestors:
git checkout d2bb4ba
This effectively moves your commits on my-branch, up to d2bb4ba, onto the tip of a new base, 8029ced:
git rebase 8029ced
This reapplies all the subsequent commits that were on master, from e7cdf7b onwards:
git cherry-pick e7cdf7b..master
Important: At this point, carefully check the history that you’ve created!
Warning: This overwrites the master branch entirely:
git branch -f master
And now see the results of your effort:
git checkout master

Pushing to the Horizon

Do you want to use Git far more easily, with no commands or parameters to remember, and with a clear overview of your repository, plus the ability to preview changes? Then make your life easier, get a free download of Vershd, the effortless Git GUI.
Designed from the ground up to prevent errors, accidental deletions, and having to write all of this morning's code. Again.

Features include:
  • Rewind is simpler and easier than using Revert or Reset. It winds the clock back on commits, letting you choose whether to keep pending files or not.
  • Advanced Context Menus. Right clicking on a commit lets you merge, cherry pick, or create a branch there and then. Files have many ways to compare them to either historic or working files on your desktop.
  • See everyone's work in the clearest way possible. Branches shows branches and their commits, where you can easily pull, push, cherry pick and more. Pending shows what you can commit or stash. Files shows commits' details. Stashes helps you apply, view and delete your stashes.
Oscar the owl and Vershd logo
 © 2020 Blightysoft
Made in England
St. George's flag of EnglandThe Union Jack, the flag of the United KingdomThe European Union flagThe United Nations flag
chevron-right linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram