Home > OS >  Can't change my master branch to main locally
Can't change my master branch to main locally

Time:11-30

I really need some help regarding branch renaming.

Unfortunately I can't push on github with the main branch, I have to use the master branch because I can't locally change my branch name.

I need to commit once to be able to use this command : git branch -M main.

But if I create a new file and put a git init, the display branch will be master and running git branch -M main will lead me to this error : error: refname refs/heads/master not found fatal: Branch rename failed

I would like to know if there is any other ways to rename the branch locally to main without renaming manually after each file creation.

Thank you so much.

CodePudding user response:

To change the name from master to main locally before even makeing a commit.follow these steps:-

  1. Navigate to the directory where your project sits.

  2. It will show hidden file since by default, .git would be hidden.

  3. Inside .git, there is a file, HEAD, open it in a text editor.

  4. You'd see, ref: refs/heads/master.

  5. change master to main in ref: refs/heads/master.

It will rename the master branch as main.

CodePudding user response:

TL;DR

Make a commit, or use git checkout --orphan main (or git switch --orphan main), or use git init -b main if you have that.

Long-ish

In a new, totally-empty Git repository, Git is in a somewhat strange state:

  • A branch name like master or main must contain the hash ID of some valid, existing commit.
  • No commits exist yet.

Therefore, no branch names can exist yet.

Nonetheless, git status will tell you that you are on branch master, for instance. This is the strange state: you're on a branch that does not exist.

In recent versions of Git, git branch -m and git branch -M—both of which rename a branch—were smartened up so that they can rename this nonexistent branch. If you have one of these recent versions of Git (2.30 or later), git branch -m main will work in this state.

Older versions of Git, however, only allow you to rename a branch that actually exists. So in this case, to use git branch -m main, you must, as you said:

commit once

Note that you need only make one commit, so that the commit exists, so that the branch name exists. It's the act of creating a commit while in this state that creates the branch name. The branch name is there, it's just that it doesn't exist. Then you run git commit, and now the branch name is there and does exist. The new commit you just made is a root commit: a commit with no parent. And that's all there is to it.

You don't have to use git branch -m to rename this unborn branch. If you do want to use git branch -m to rename it, it must be an existing branch, but you can rename it before it exists. You just need to use some other command, in Git versions before 2.30.

In Git version 2.28 or later, git init takes --initial-branch (or -b for short) which allows you to specify the name of the branch that doesn't exist, but that you're on. So git init -b main does the trick.

If your Git predates 2.28, you can run git init as usual, then use the checkout or switch command with the --orphan flag. This creates a branch that does not exist yet. This flag has worked since Git 1.7.2: its purpose is to re-create that peculiar state in which you're on a branch that does not exist. The next commit you make then creates that branch, by creating a root commit and storing the new commit's hash ID in the branch name as usual. The storing of the hash ID creates the branch name and the peculiar situation, of being on a branch that does not exist, is now resolved.

Note that when you're on a branch that does not exist and you use git checkout --orphan, you change the name of the branch that does not exist. The old name continues not to exist, so the fact that you were going to create that branch with a future commit is now forgotten: Git has no idea that master never sprang into existence, and won't go create it. It will instead create this other name that does not yet exist.

  • Related