Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rebase backwards, preferably without using diff/apply?

Tags:

git

rebase

I have the following commit history:

(master)              (topic)   (quickfix)
       a -> b -> c ->       d ->         e

I want to modify it so that it looks like so:

     (master, quickfix)              (topic)   
a ->                  e -> b -> c ->       d

In other words, master was at a commit a. I started working on a topic branch, but while on that branch I saw an issue and made a fix to unrelated code. I did this in a quickfix branch, but the branch was from topic instead of master. I'd like to merge quickfix back into master, but without any of the changes that were made in topic.

I know I can use diff/apply to ultimately get where I want to go, but this has happened to me a few times and I find the diff/apply workflow a little cumbersome. Is there a feature in git that makes this easier?

like image 228
Dan Passaro Avatar asked Sep 16 '25 21:09

Dan Passaro


1 Answers

There isn't one command to do this, but a relatively simple sequence can do it, run while working on the master branch:

# make it so that it's as if quickfix were branched off of master instead
# of topic
git rebase --onto master topic quickfix

# proceed as normal
git merge quickfix
git rebase master topic

The reason I didn't understand this at first was that I wasn't aware of the --onto option for git rebase. I found out about it while reading git help rebase, which had a similar enough example.

like image 85
Dan Passaro Avatar answered Sep 19 '25 12:09

Dan Passaro