Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to re-apply commits on top of unrelated branch?

Tags:

git

I have a simple repository with linear history of commits, like:

[A] -> [B] -> [C] -> [D] -> [E] ...

I basically need to remove commits A and B so I thought I'd create a new repository and would like to achieve something like:

[X] -> [C] -> [D] -> [E] ...

So I created a new repository, manually created commit X that takes stores the relevant information from A and B and now need a command that will bring commits C, D, E etc. from the original repository and will put it on top of my new commit X.

How to do that?

Edit: Two problems I have with the suggested cherry-pick method are:

  1. Transferred commits lost their dates. Is there any way to preserve commit dates?
  2. When I fetched master from the original repository (and that remote master doesn't have any commit in common with the new repository), I have trouble deleting those fetched commits. When I do git branch -D myoriginalrepo/master, it says that no such branch exists while I can clearly see those commit in my GUI tool.
like image 905
Borek Bernard Avatar asked Feb 01 '12 02:02

Borek Bernard


1 Answers

Not sure why you need the commit date to stay the same but here goes:

git rebase B E --onto X --committer-date-is-author-date

If B..E aren't in the same repository as X (as they can be if you create your fresh start in-place), you'll need to fetch them first:

git fetch <path_to_old_repos>

Of course, B, E and X here mean their commit-ids if you haven't actually tagged/branched them.

You can also do something similar (although commit date won't be preserved) by rebasing from A in your original repository and squashing B onto it:

git rebase -i `A`

# change "pick b" to "squash b"

You'll get a chance to change commit message at which point you could make it X's.

like image 147
antak Avatar answered Sep 20 '22 15:09

antak