Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make the current Git branch a master branch

Tags:

git

I have a repository in Git. I made a branch, then did some changes both to the master and to the branch.

Then, tens of commits later, I realized the branch is in much better state than the master, so I want the branch to "become" the master and disregard the changes on master.

I cannot merge it, because I don't want to keep the changes on master. What should I do?

Extra: In this case, the 'old' master has already been push-ed to another repository such as GitHub. How does this change things?

like image 988
Karel Bílek Avatar asked May 04 '10 05:05

Karel Bílek


People also ask

How do I make my branch a master?

Rename “{current-branch}” to “master” and push to repository (this will create a new “master” branch still “{current-branch}” will exist). In repository settings, change “Default Branch” to point at “master”.

How do I change my main branch to master?

Not only do you have to change the name of the branch in your remote, but you'll have to change it locally as well: git branch -m master . Then, you'll need to set origin/master as the corresponding remote branch: git push -u origin master .


2 Answers

The problem with the other two answers is that the new master doesn't have the old master as an ancestor, so when you push it, everyone else will get messed up. This is what you want to do:

git checkout better_branch git merge --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 

If you want your history to be a little clearer, I'd recommend adding some information to the merge commit message to make it clear what you've done. Change the second line to:

git merge --strategy=ours --no-commit master git commit          # add information to the template merge message 
like image 103
Cascabel Avatar answered Sep 25 '22 07:09

Cascabel


Make sure everything is pushed up to your remote repository (GitHub):

git checkout main 

Overwrite "main" with "better_branch":

git reset --hard better_branch 

Force the push to your remote repository:

git push -f origin main 
like image 45
Brandon Howard Avatar answered Sep 24 '22 07:09

Brandon Howard