Transplant commits with git rebase –onto

You are working along and then realize that all the work you did is based off the wrong branch. Instead of working off the branch wrong-branch, you should have been working off the branch right-branch. What to do?

First, as a safety create a fix branch off your current branch:

git checkout -b fix-topic-branch topic-branch

Now, take a look at the commits to find the ref your work is on top of:

git log --oneline --decorate

8cb0c1a (fix-topic-branch) Added artcode urls
1ff8dac Added art form
0670ee7 added django-resourceful requirement
633dda6 Added ArtView class
e64f05f Added models
4186ba8 Added pristine django app artcode
d49ad49 (wrong-branch) Merge branch 'add-test-finder' into baseline
[...]

In the case above the commits we want are on top of d49ad49, which is `wrong-branch`. With this info you can tell git rebase --onto to take all commits “on top of” d49ad49 in fix-topic-branch and plant them on top of right-branch.

git rebase --onto right-branch d49ad49 fix-topic-branch

Once you confirm fix-topic-branch looks as you expect, you can rename it back to topic-branch:

git branch -D topic-branch
git branch -m topic-branch

Advertisements

One thought on “Transplant commits with git rebase –onto”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s