Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch branch names in git

Tags:

git

branch

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.

like image 553
Daniel Benamy Avatar asked Aug 22 '08 05:08

Daniel Benamy


People also ask

Can you change branch names in git?

Git Branch Rename Command The steps to change a git branch name are: Rename the Git branch locally with the git branch -m new-branch-name command. Push the new branch to your GitHub or GitLab repo. Delete the branch with the old name from your remote repo.

How do I switch branches in GitHub?

You can use the git switch - command to undo any changes you make and return to your previous branch. If you instead want to keep your changes and continue from here, you can use git switch -c <new-branch-name> to create a new branch from this point.

What command is used to switch branches?

The git checkout command allows you to switch branches by updating the files in your working tree to match the version stored in the branch that you wish to switch to. You can think of it as a way of switching between different workspaces.


1 Answers

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 
like image 179
Greg Hewgill Avatar answered Sep 28 '22 01:09

Greg Hewgill