Shrugo
Shrugo

Reputation: 157

Issue while renaming a branch from uppercase to lowercase on local and remote git repos

As I am new to git I didn't know that it is a good practice to have the branch names lowercase so I ended up having it uppercase. So now I am trying to rename the branch and tried what has been suggested here but no luck.

Commands that I also tried

git branch -m newfeature
git branch -m NEWFEATURE newfeature

The error I get when I try the above commands is

fatal: A branch named 'newfeature' already exists

Upvotes: 4

Views: 2307

Answers (2)

kostix
kostix

Reputation: 55473

Two points:

  • If your Git repository is located on a case-insensitive filesystem (say, NTFS with default settings on Windows or HFS[+] on MacOS), any attempt to rename a branch to a case-different version of itself will fail.

    The reason is that ("fresh") branches in Git are stored as plain files with the names of such files being the name of the branch they represent. On a case-insensitive filesystem, names "foo", "FOO" and "FoO" are considered to refer to the same file, so it's impossible to create a file named "whatever" if a file named "WHATEVER" already exists in the same directory.

  • To force owerwriting a branch when using the git branch -m command, you may use its upper-cased version: git branch -M would replace the target branch even if it exists.

To recap, the simplest way to rename a branch is to trip it through some neutral name which is currently not taken:

git branch -m temp newfeature
git branch -m newfeature NEWFEATURE

Also note that if you only care about the name of the branch you have pushed, you may well rename it remotely: the command

git push origin WHATEVER:refs/heads/whatever :refs/heads/WHATEVER

would push the local branch "WHATEVER" to create a branch named "whatever" in the remote repo known as "origin" and then delete the branch named "WHATEVER" there.

Provided "WHATEVER" locally contains the same history as was already pushed to "origin" under that same name, the command would not even transfer any data.

Upvotes: 8

mnestorov
mnestorov

Reputation: 4484

The reason git is telling you that newfeature already exists, is because with the first command, you have already renamed you branch to newfeature. The git branch -m newfeature command will move your current branch to the newfeature branch name.

So both commands are correct, you just did it on the first go.

NOTE: if you are on Windows, which has a case-sensitive filesystem, you need to pass the -M option. It stands for --move --force to force the rename.

You can view all your branches with

git branch -a

and see what their names are.

Upvotes: 0

Related Questions