0

I have two git branches with completely different histories.

What I need to do:

I need to get branch B's history and combine it and place it ontop of branch A's history. I want to "merge" branch B into branch A but they have completely different histories.

What can I do?

Taylor Austin
  • 5,407
  • 15
  • 58
  • 103

2 Answers2

3

I think you should use git rebase (because you will have conflicts hard to solve when applying the first commit) but instead, use the graft feature and the do a git filter-branch.

http://gitolite.com/archived/grafting.html

Philippe
  • 28,207
  • 6
  • 54
  • 78
1

It's pretty common that two Git branches have different histories of commits (at least from the moment they branched from parent). You want to get all commits from branch B and "place them ontop of branch A's history", so looks like the most suitable tool will be a git rebase. I can't see any information about merges performed for both branches, but I assume that you wan't to preserve all of them. In such case I'd suggest to try with

git checkout B
git rebase A --preserve-merges

You might get some conflicts during rebase process, so following commands can also be helpful:

git add <some_file_with_conflict>
git rebase --continue

More information about rebasing branches you can find in this Git rebase documentation and this Git Book

Kamil
  • 2,712
  • 32
  • 39
  • So the two branches have completely different histories like we said, but branch B does not have any of branch A's history. I did a `git rebase A --preserve-merges` and it looked liked it erased all my code in branch B. What should I do from here? – Taylor Austin Apr 13 '18 at 20:48
  • I have this message in the console: `Your branch and 'origin/react-v2-module' have diverged, and have 146 and 11 different commits each, respectively.` – Taylor Austin Apr 13 '18 at 20:49
  • Looks like someone has pushed some commits to origin/react-v2-module branch. You need to update both branches before rebase to have the most recent version of them. Otherwise you will have to merge your local branch with remote one. You can check https://stackoverflow.com/questions/2452226/master-branch-and-origin-master-have-diverged-how-to-undiverge-branches for more details about your current problem. – Kamil Apr 13 '18 at 20:52
  • When you say updated what do you mean by that. And yes they are two separate branches. One of the branches did not start with the master's git history and started it's own. – Taylor Austin Apr 13 '18 at 20:54
  • By update I mean git fetch and git rebase or git pull, depending on what approach you prefer. – Kamil Apr 13 '18 at 20:57
  • I did a git fetch on both and did the git rebase A --preserve-merges and still have branch B losing all of its content. I want to essentially replace branch A with branch B. – Taylor Austin Apr 13 '18 at 21:03