2

I know there's the option --allow-unrelated-histories which is available since Git 2.9 to merge unrelated histories. However, for some legacy corporate reasons, I'm stuck with an older version of Git 1.8.

Can anyone help showing how to do this without --allow-unrelated-histories?

His
  • 5,891
  • 15
  • 61
  • 82
  • Does merging two unrelated histories fail at all with `1.8` ? – LeGEC Oct 15 '20 at 12:31
  • No. But I keep getting the error `not something we can merge`, which isn't present using Git 2.9. I follow the steps at https://stackoverflow.com/a/10548919/68759 – His Oct 15 '20 at 13:04
  • Yep, the issue is I need to automate the sync of two repos periodically with a Jenkins job. It is the Jenkins build agent that is having an old Git :( – His Oct 15 '20 at 13:08
  • No I don't use `git-filter-repo`. The commands I follow are `cd path/to/project-b;` `git remote add project-a /path/to/project-a;` `git fetch project-a --tags;` `git merge --allow-unrelated-histories project-a/master;` `git remote remove project-a` – His Oct 15 '20 at 13:12
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/223087/discussion-between-his-and-legec). – His Oct 15 '20 at 13:14
  • 1
    I misunderstood how you followed said instructions. I deleted my (useless) comments. – LeGEC Oct 15 '20 at 13:17
  • Previous to Git 2.9, `--allow-unrelated-histories` was *always enabled* and there was no way to *dis*-able it. The error you are getting is due to something else entirely. – torek Oct 15 '20 at 13:58

1 Answers1

2

If the two branches have points in their history that should naturally be stitched together (e.g : commits with the same content), you can use git replace (I don't know if it already existed in 1.8, though ?) or grafts to stitch the histories together :

# say <commit-a> on branch 'other' should be treated as
# <commit-b> on branch 'mine' :
git replace <commit-a> <commit-b>

# if 'git replace' does not exist :
# find the *descendant* of commit-a (say : child-a) and run :
echo "<child-a> <commit-b>" >> .git/info/grafts

# <child-a> is now treated as if <commit-b> was one of his parents

You should then be able to run git merge.

Remove the replace (git replace -d <commit-a>) or the graft (remove line from .git/info/grafts, or the whole file if you don't have any other grafts).


Another option is to dump the complete content of branch other as a patch (for example : diff it against a fake commit which has an empty content), and use git apply :

git diff <commit with empty content> other > other.patch
git apply other.patch
LeGEC
  • 46,477
  • 5
  • 57
  • 104