Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git branch delete deletes my branch where i currently staing on when name was written with diferent case

Tags:

git

I just created new branch with name daco.

git checkout -b daco

Then i try delete it. I know it will be error and it was.

git branch -D daco

Error: Cannot delete branch 'daco'

But when i try delete this way:

git branch -D Daco

It is deleted without problem.

Do you know why?

enter image description here

like image 223
Jaroslav Beňo Avatar asked Feb 08 '21 05:02

Jaroslav Beňo


2 Answers

It’s a artifact of a case insensitive file system, like that on Windows. Git internally stores branches as files and directories, inside the .git/refs folder.

So, when you create a branch named foo, a file named foo is created inside the refs/heads folder. Since you did it from a checkout command, git also records that foo is now the checked out branch. So, when you try to delete branch foo, git sees that foo is the checked out branch and gives you an error.

Now, when you try to delete branch named Foo, git first checks if a file named Foo exists in the refs/heads folder, and here is where it gets weird:

Since your file system is case insensitive, Foo, as a file name, is equivalent foo. Therefore, the file system reports that path refs/heads/Foo exists, when it’s actually refs/heads/foo. But then, when git checks if Foo is the checked out branch, it sees that Foo is not the checked out branch, because foo is, since git is internally case sensitive, for it "Foo" is not equal to "foo". So, git wrongly determines that: foo is a existent branch, distinct from Foo, and that Foo is not the checked out branch. Therefore, git allows you to delete Foo.

like image 147
Levi Haskell Avatar answered Oct 26 '22 23:10

Levi Haskell


Apparently, git branches act case-insensitive because your underlying OS filesystem is case-insensitive (git branches are represented as files inside .git/). Git lets you delete 'Daco' because it sees that as a separate branch name, but the OS treats it as the same file, so Git ends up deleting the original 'daco'.

The answer from "torek" on this question has some good details. The file names that represent Git branches "are case-insensitive, e.g. case-preserving, but fold case during name-matching." Basically, Git is case-sensitive and your OS is probably case-insensitive, which results in being able to delete a nonexistent branch with the capitalized name of an existing branch. I tested it on my Mac and I get the same results as you.

like image 45
Blue Avatar answered Oct 26 '22 23:10

Blue