Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging split history in git

Tags:

git

rebase

I have been toying with rebase a bit, trying to split and combine history as described here: https://git-scm.com/book/en/v2/Git-Tools-Replace

Here is my test repository with split history: https://github.com/defufna/split-history

Note that "initial commit" and "Added description" commits both point to the same tree. What I'm trying to do is merge these two histories (while maintaining merges). I used this:

git rebase --rebase-merges --onto history 94da9b0f

This works, but I'm getting a conflict at 084dae5. This is a merge that resolves modify/delete conflict, and it requires me to manually resolve this conflict.

I am doing this in preparation for much larger repo merge, where I will have to do something similar but with 50k commits, so I would like to avoid manual conflict resolution. I am aware that git replace could solve my problem, but I'm curious if it is possible to do it without replacement. I would also like to keep sha values of commits in history branch.

Edit:

So I've managed to do what I want, but I'm not sure how good this idea is. I've added -i to rebase, and when I got todo file, I've changed every

merge -C 9751be2 Merge # Merge

line into:

exec git checkout `git commit-tree '9751be2^{tree}' -p HEAD -p refs/rewritten/Merge -m Merge`

I'm basically telling git whenever it encounters a merge to reuse tree from original merge.

It works as expected (for now), but it's a lot slower. Is there a better way to do this?

like image 514
Ivan Avatar asked Feb 11 '20 14:02

Ivan


People also ask

How do I merge two git repository and keep history?

To combine two separate Git repositories into one, add the repository to merge in as a remote to the repository to merge into. Then, combine their histories by merging while using the --allow-unrelated-histories command line option.

Does git merge keep history?

In the Conceptual Overview section, we saw how a feature branch can incorporate upstream changes from main using either git merge or git rebase . Merging is a safe option that preserves the entire history of your repository, while rebasing creates a linear history by moving your feature branch onto the tip of main .

Does git merge rewrite history?

Rebase is one of two Git utilities that specializes in integrating changes from one branch onto another. The other change integration utility is git merge . Merge is always a forward moving change record. Alternatively, rebase has powerful history rewriting features.

Which command is used to display merge history in git?

I'll start by showing how the default “chronological” order of git log can mislead you when the history contains merge commits. Then I'll show how you can visualize Git merge history using git log --graph , and how to see the “true” history of a single branch using --first-parent .


1 Answers

Note that "initial commit"[94da] and "Added description"[c00e] commits both point to the same tree

Is there a better way to do this?

Much.

git replace --graft 94da c00e^
git filter-branch master

and you're done.

git rebase exists to re-apply changes onto different base content, producing snapshots with new content. You already have all the snapshots you want, you only need to rewire the ancestry. git replace --graft does it locally, allowing you to experiment casually. git filter-branch exists to bake in rewired ancestry while applying any super-easy content changes -- but you don't even have any of those. You just want to rewire the master history.

like image 87
jthill Avatar answered Sep 29 '22 16:09

jthill