Overwrite the master branch with a orphan branch in git
Asked Answered
S

3

7

I've created a new orphan branch with git checkout --orphan orphan-branch and made a lot of commits on that branch.

Now I want to replace the master branch with the newly created orphan-branch, all the files and the history of the master branch should be replaced by the files and the history of the orphan branch. What is the best way to do this?

Slantwise answered 3/6, 2016 at 9:1 Comment(0)
T
9

To replace the history and old file of the master branch you need to do a forced update on it.

git push origin +your_orphan_branch_name:master

Attention: This deletes the whole history on the remote repository for your branch master. But that what you intended to do...

Tabitha answered 3/6, 2016 at 9:11 Comment(1)
Don't forget to change your local repo's master branch as wellDoelling
L
3

git branch -m master old-master // rename master to old-master locally

git branch -m orphan_branch master // rename orphan_branch to master locally

git push -f origin master // force-update master on remote

Littrell answered 3/6, 2016 at 9:7 Comment(1)
please add a explanation for this.Hames
E
0

Here is how to git merge/replace an orphan branch into master:

git checkout --orphan new-framework
# completely rewrite your application in new framework
git merge --strategy=ours --allow-unrelated-histories master
git commit-tree -p HEAD^2 -p HEAD^1 -m "Merge branch 'new-framework'" "HEAD^{tree}"
git reset --hard $OUTPUT_FROM_PREVIOUS_COMMAND
git checkout master
git merge --ff-only new-framework

Unlike other answers, this keeps history and does not force push.

Credits: simon04

Endospore answered 1/10 at 10:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.