Steerpike
Steerpike

Reputation: 1863

Delete origin/Master branch and promote origin/Develop to master

Go easy on me - I'm a QA who's learning Git along the way!

This should be very straightforward. My repo is in Bitbucket.

I have a branch origin/master that for various reasons, now contains old code. I have one at origin/develop containing latest code. Both have local branches tracking each. I don't wish to merge them.

I'm happy to lose origin/master entirely (I cloned it the 'old' as a backup) and want develop to become master.

remote

What would be the recommended Git command to do this via the terminal?

Upvotes: 1

Views: 916

Answers (2)

Daniel Trugman
Daniel Trugman

Reputation: 8511

Marco gave a very good answer, but I think that should not be the essence of your question.

Seeing that you are just starting to use Git for version control, I think you should ask yourself: "How am I supposed to handle such scenarios when working with Git?"

And the answer is NOT "delete the master branch and replace it with develop".

The main reason to use Git is to be able to save your history. If you get to a point where you want to delete your master branch, you might as well wanna create a new repository altogether!

Instead, why not simply merge develop into master? This way, you can save your history, and the final result is exactly the same.

If you are now writing the first lines of your new project and you wanna hide the ugly history and create a new slate for your project, just get to a point where you are comfortable with your MVP and recreate the repository from scratch step by step. You'll also learn a lot from the process. It might even help you understand how you were supposed to do it in the first place.

(Pro tip: If you were an advanced Git user, I would've probably told you to just use interactive rebase to rewrite the history of master)

Upvotes: 0

Marco Luzzara
Marco Luzzara

Reputation: 6056

Renaming a remote branch is a dangerous operation as far as I know because there is no way to directly rename the remote branch (unless you do it on the remote repo itself). This means that all the developers that are working on the develop branch must fetch the new master and set its starting-point on the tip of their develop. Then they can delete their local develop branch. For this solution, I assume that no one is working on master.

git branch -d master
git branch -d -r origin/master
git branch -d -r origin/develop
git branch -M develop master
git push -f -u origin master
git fetch origin develop
git push -d origin develop

I am basically deleting master, so that I can rename develop as master. Then I forcely (-f) push the new master, I fetch the develop (which now points to the same commit as master) in order to delete it remotely right after. For completeness: if someone had been working on master, it would have been safer to use git push --force-with-lease -u origin master. See here for more info.

Upvotes: 1

Related Questions