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.

2

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

6 Comments

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.
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?
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.
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.
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).
|
2

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

git branch -f master <nameOfTheUpToDateBranch>

(doc)

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.