I accidently ran the git branch -M main
command when I was not on the master branch. NOTE: The command was intented to rename the master
branch to main
. See more. While lots of articles explain how rename master to main, none explain how to recover from running this if you are on the wrong branch when you run it.
What happened
The following steps summarize the steps that happened:
git checkout feature/do-it
git branch -M main
git checkout main
Already on 'main'
# How do I fix this?
Details on how it happened
This happened because I blindly followed a command on github.com that shows how to rename your master branch to main when creating a new repository. My github/me/my-repo
webpage shows these commands to setup the new repo.
echo "# my-repo" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin [email protected]:me/my-repo.git
git push -u origin main
But when I ran git branch -M main
I failed to realize that I was not on the master branch. I was on the feature/do-it
branch which was now gone! And now I have both master and main branches!
Searching for answers:
https://www.google.com/search?q=what does git branch -M main do? - did not give me any useful results.
Git book - https://git-scm.com/book/en/v2/Git-Branching-Branch-Management didn't show what -M option does. It does show
Changing the master branch name
$ git branch --move master main
Which is likely what -M does. I'm just not sure.
So my questions are:
What does the command git branch -M main
do, when your on a non-master branch
How do I recover? Get the master
branch renamed to main
and get my feature/do-it
branch back.
CodePudding user response:
What does the command
git branch -M main
do, when your on a non-master branch
Things to know:
There is no such thing as a "non-master branch". There are just branches, which themselves are just names. No name is "special" in any way.
The command
git branch -M newname
is very simple: it changes the name of the current branch tonewname
. It doesn't know or care about anything else; it just does what you said to do.
So, it follows as the night the day, that if you were on branch feature/do-it
at the time you said git branch -M main
, then the branch feature/do-it
was renamed to main
— because that is what you said to do.
There is also (as you have discovered) a fuller form, git branch -M oldname newname
. You can give that command while you are on any branch, and the result will be the same regardless, because you are specifying both names. But you didn't do that; you used a form where you supply only one name, which is taken as the new name, and the branch you are currently on is the one whose name is changed.
Then, too, there is your somewhat unhappy choice of -M
. This is a "force" command: it will happily permit you to change a branch's name to a name currently held by some other branch. That's a slightly nutty thing to do if you don't really mean it; better to say -m
instead, because Git will then stop you if you try to do that.
The good news, ultimately, is that, as I already said, branches are nothing but names. So it is trivial (and harmless) to rearrange or otherwise manipulate those names however you like. Just say what you really mean when you give the command, that's all.
(Also I should mention that everything you've done is undoable, because that's what Git is all about. You can find any previous state of affairs by saying git reflog --all
. There, you will see the record of the renames, so you will understand exactly what you did — and exactly how to undo it.)
One final comment: I notice that in your search for answers you neglected to look at the actual docs. All of this is completely spelled out there. The docs for git branch
are at https://git-scm.com/docs/git-branch. There you will see, for example, that -m
is --move
, and -M
is --move --force
.
I recommend (1) that you get very familiar with where the docs are, and (2) that you look at them before you go thrashing around with Git. Git is not a user-friendly notation; on the contrary, it makes it very easy for you to shoot yourself in the foot. Renaming branches is a benign exercise, and not a way that you can readily shoot yourself in the foot, but there are lots of commands that can.