2

In my project, I started with quite a few commits in the master branch. After a couple of releases, I realised that the approach I was taking might not be what we want at all.

The new approach required a complete code rewrite so I decided to start in an orphan branch (a-new-beginning-branch). I liked it and eventually, it became my de-facto master branch. Now I want to replace master with a-new-beginning.

I've found how to make the current Git branch a master branch but when following the steps shown there, I get the error message fatal: refusing to merge unrelated histories. I know I could use the –allow-unrelated-histories option but I don't really understand what it does and don't want to break things.

I've also found how to overwrite the master branch with a orphan branch but I don't want to delete the history in my master branch.

Ideally I would like to end up with a lineal history, but if that's not possible, I'll be also happy with some sort of a merge.

git workflow

Any ideas will be much appreciated!

Bsquare ℬℬ
  • 4,423
  • 11
  • 24
  • 44

3 Answers3

2

I don't understand why you want to keep the current history of the current master branch, with your a-new-beginning one.

If you objective is just to have a branch named master with the history of your a-new-beginning branch, you can perform it this way:

git branch -m master master-backup
git push origin :master master-backup

git branch -m a-new-beginning master
git push origin :a-new-beginning master

This way, you will update the branches, locally and remotely.

Bsquare ℬℬ
  • 4,423
  • 11
  • 24
  • 44
0

Did you try rebasing your master in your new Branch ?

1- $ git checkout a-new-beginning-branch

2- $ git rebase master

Your new branch now contains both the history of a-new-beginning-branch & master branch

Melchia
  • 22,578
  • 22
  • 103
  • 117
0

As you've said you can use --allow-unrelated-histories. This option allow to merge data when no commit are common. In this case how to make the current git branch a master branch discussion looks as our better solution.

We can do then :

git checkout better_branch
git merge --allow-unrelated-histories --strategy=ours master    # keep the content of this branch, but record a merge
git checkout master
git merge better_branch             # fast-forward master up to the merge

Fist merge get a related history to new branch with old master. Second merge act new branch data as master data.

You can check merge is ok with a git diff master..better_branch^ . Diff should be empty.

azerttyu
  • 11
  • 2