Switch branch names in git

GitBranch

Git Problem Overview


There may be more than one way to ask this question, so here's a desciption of the problem. I was working on master and committed some stuff and then decided I wanted to put that work on hold. I backed up a few commits and then branched from before I started my crap work. Practically this works fine, I just now have a different branch as my main development branch. I'm wondering how I could change things around so I'm working on master again but it doesn't have my junk work and said work is on a different branch.

Some ways this could be asked/solved: How do I rename my master branch to something else and then rename something else to master? How do I back up master and then cause all commits I've backed up past to be on a different branch?

Thanks for all the (quick) answers! They're all good.

Git Solutions


Solution 1 - Git

In addition to the other comments, you may find the -m (move) switch to git-branch helpful. You could rename your old master to something else, then rename your new branch to master:

git branch -m master crap_work
git branch -m previous_master master

Solution 2 - Git

I think you should consider a different development strategy to prevent issues like this. One that seems to work best for me is to never do development directly on my master branch. Regardless of the changes I'm making, I always create a new branch for new code:

git checkout -b topic/topic_name master

From there, I can push out the changes to public repositories:

git push pu topic/topic_name

or eventually just merge it back in with my master branch:

git checkout master && git merge topic/topic_name

If you truly need to go back to an older point in time and set that as your master, you can rename the current branch to something else and then check out an older version to be your master:

git branch -m master junk
git co -b master old_sha1_value

Solution 3 - Git

Start on master, create a branch called in-progress, then reset master to an earlier commit.

$ git branch in-progress
$ git reset --hard HEAD^

Solution 4 - Git

This is relatively easy:

git checkout -b fake_master master # fake_master now points to the same commit as master
git branch -D master               # get rid of incorrect master
git checkout -b master real_master # master now points to your actual master
git checkout master                # optional -- switch on to your master branch

Solution 5 - Git

This will set your master to any point in one step:

git checkout -B master new_point

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaniel BenamyView Question on Stackoverflow
Solution 1 - GitGreg HewgillView Answer on Stackoverflow
Solution 2 - GitBrian RiehmanView Answer on Stackoverflow
Solution 3 - GitT PercivalView Answer on Stackoverflow
Solution 4 - GitolliejView Answer on Stackoverflow
Solution 5 - GitPenghe GengView Answer on Stackoverflow