0

I have a situation where my master branch is way, way behind another branch. I'd like to simply more or less obliterate the old master and have the other branch be, essentially, a new master.

I'm the only one working on this project, and the old master has very little in common with the new/current branch.

Major Productions
  • 5,914
  • 13
  • 70
  • 149
  • Possible duplicate of [How to replace master branch in Git, entirely, from another branch?](https://stackoverflow.com/questions/2862590/how-to-replace-master-branch-in-git-entirely-from-another-branch) – phd Jul 30 '19 at 19:25
  • https://stackoverflow.com/search?q=%5Bgit%5D+replace+master+current+branch – phd Jul 30 '19 at 19:25

3 Answers3

4

If you don't care about the old history at all:

git checkout master
git reset --hard the-other-branch-i-want-as-master

That will delete all changes you have laying around your working tree so use with care.... if what you would like instead is a new revision where you set your working tree as a new revision on your old master:

git checkout the-other-branch --detach
git reset --soft master
git commit -m "Single change to move old master to a new position where I want it"
git branch -f master
git checkout master
eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • 1
    Upvoted for the second solution which is arguably the most useful and elegant way to achieve the goal while keeping a clear history of what's been made. – Romain Valeri Jul 30 '19 at 18:49
  • If I push to origin master, which has the outdated code, will it just accept the revision bomb (for lack of a better term), or will I need to do more to ensure the remote doesn't complain? – Major Productions Jul 30 '19 at 18:50
  • 1
    If you reset --hard (or branch -f, as others have suggested), you are rewriting history (revisions on the old branch might disappear)... that will require you to use `git push -f`. – eftshift0 Jul 30 '19 at 18:53
  • 1
    By default remote will not accept changes in history. So you will need push force (-f) or rebase new commits on top of original master. But it is not match question and situation where only you are working in repo. – oklas Jul 30 '19 at 18:55
  • 1
    If you _have_ to keep the old history, then I think you should consider using the `reset --soft` recipe I provided. You will get one revision on top of your old history with all changes required to get your old branch to look like the new branch (with a single unrelated revision). – eftshift0 Jul 30 '19 at 18:58
  • The soft reset was the best of both worlds (getting the rebase I wanted without completely obliterating the commit history (while saving it wasn't a priority, it's nice to keep that intact, too))... thanks! :) – Major Productions Jul 30 '19 at 20:00
2

If so, the simplest way probably is to just move the reference :

git branch -f master <nameOfTheUpToDateBranch>

(doc)

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
1

This is may be achieved with two very simple methods. Either:

  git checkout master
  git reset --hard my-branch

or in any other branch which is not the master branch:

  git branch -f master my-branch
Markus1189
  • 2,829
  • 1
  • 23
  • 32
oklas
  • 7,935
  • 2
  • 26
  • 42