Alexis King
Alexis King

Reputation: 43872

How can I insert a commit before rebasing?

I have a history that looks a little like this right now:

                  origin/master
                        |
A --- B --- C --- D --- E
                   \
                     -- F
                        |
                     master

I want to rebase my changes (commit F) after E, but there are some fairly major issues in commit E I would like to correct first. Basically, this is what I want to be able to get to:

                  origin/master
                        |
A --- B --- C --- D --- E --- G
                   \
                     -- F
                        |
                     master

I then want to rebase F on at the end, for a final, simple result:

                              origin/master
                                    |
A --- B --- C --- D --- E --- G --- F

How can I "put aside" commit F while I work on the main trunk, then rebase it on later? I would assume it would involve git branch, but I don't know enough git to know the sequence of commands to issue. What is the best way to do this?

Upvotes: 3

Views: 251

Answers (2)

xaizek
xaizek

Reputation: 5252

Create a branch pointing to your F commit:

git branch tmp

Remove F commit from master branch:

git reset --hard HEAD~1

Make your fixes and commit them:

git commit

Add F commit on top of current master branch (this can result in conflicts, so resolve them):

git cherry-pick tmp

Remove temporary branch:

git branch -d tmp

So you don't really need git rebase here, it's for more complex things or at least not for working with one commit.

Upvotes: 2

Yair Zaslavsky
Yair Zaslavsky

Reputation: 4137

I assume you have a separate branch for F, if not - I missed that and you should create it.
I would recommend you not to work on master with commit G, but also have a separate branch for it as well
Please create a separate branch for it.
1. Then, try to rebase the branch that contains f against that branch (perhaps using git rebase --onto,
although I think there is another way to do that)
Another way to do this, is to perform git format-patch origin/master at the branch that contains F, and then apply this patch on the branch that contains G.
You might of course run into conflicts.
Based on my experience - option 1 is better , for some reason git apply and git am are more "fragile" (sorry for the bad term) than performing rebasing against another branch.

Upvotes: 0

Related Questions