How to merge two branches without a common ancestor?

Disclaimer: I’ve only used “graft points” myself once in a toy repository. But it is an obscure feature which you may not have heard of, and which _may_ be helpful in your situation.

You could use “graft points” to fake the ancestry information. See, e.g., What are .git/info/grafts for? or proceed immediately to the git wiki entry on graft points.

In essence, you would create a file .git/info/grafts that tricks git into thinking that commit M1 is an ancestor of commit M2:

$ cat .git/info/grafts
<your M2 commit hash> <your M1 commit hash>

Subsequently, it would look like M2 was an empty commit that just merged I2 and M1 into a common tree.

The major downside: the graft point is not committed; therefore, it is not checked out, but needs to be added to each local working copy of the repository manually.



Update: use git replace --graft instead.

Graft points, as described above, have been superseded. Run

git replace --graft <your M2 commit hash> <your M1 commit hash>

to create the graft. This is stored in .git/refs/replace/. Although git does not fetch, or push, these refs by default, they can be synchronized between repositories using:

git push origin 'refs/replace/*'
git fetch origin 'refs/replace/*:refs/replace/*'

(StackOverflow: How to push ‘refs/replace’ without pushing any other refs in git?)

Leave a Comment