Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

git-branch

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
like image 478
Shrugo Avatar asked Oct 16 '25 15:10

Shrugo


1 Answers

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.

like image 196
kostix Avatar answered Oct 18 '25 07:10

kostix