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?
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With