Barzi2001
Barzi2001

Reputation: 1766

Deleting branches which name contains weird characters

I use Git Bash, and by mistake I created a branch named -D. After having search with Google, I tried either

git branch -d -- -D 

obtaining

error: branch '-D' not found. 

and I also tried to remove it from gitk but without success.

On the other hand, I have noticed that the branch name changes if I type git branch from the console or if I display it on gitk. For instance, the branch is -D in the console, but it is named with weird characters on gitk.

Branch name obtained with git log

Branch name displayed in gitk

At this point I copied/paste the weird name from gitk to the console, and I issued

git branch -d â?"D

without success.

However, I got the warning message: "Warning: Your console font probably doesn't support Unicode. If you experience strange characters in the output, consider switching to a TrueType font such as Lucida Console!"

As last attempt, I tried to remove the branch from gitk, but I get a popup window saying "error: branch yet another weird and longer name not found".

This time, I cannot copy and paste yet another weird and longer name from the pop up box to the console, since I cannot copy from a pop up window.

Upvotes: 4

Views: 2803

Answers (3)

VonC
VonC

Reputation: 1323115

With Git 2.29 (Q4 2020), you should now be able to delete that branch from gitk directly, considering it has learned to deal with Unicode characters in branch names.

http://wiki.seas.harvard.edu/geos-chem/images/6/6e/GitKDeleteBranch.png

See commit e244588 (10 Sep 2020) by Denton Liu (Denton-L).
See commit a99bc27 (15 Dec 2019), and commit f177c49 (02 Nov 2019) by Роман Донченко (SpecLad).
See commit 2faa6cd (09 Apr 2020) by Johannes Sixt (j6t).
See commit c1a6345 (15 Oct 2019) by Eric Huber (echuber2).
See commit b8b6095 (12 Dec 2019) by Beat Bolli (bbolli).
See commit e2b9cb1 (03 Oct 2020), and commit e272a77 (23 Jan 2020) by Junio C Hamano (gitster).
See commit 6cd8049 (03 Oct 2020) by Paul Mackerras (paulusmack).
See commit 113ce12 (11 Feb 2020) by Stefan Dotterweich (stefandtw).
See commit d4247e0 (07 Dec 2019) by Kazuhiro Kato (kkato233).
(Merged by Junio C Hamano -- gitster -- in commit 0cf28f6, 05 Oct 2020)

gitk: fix branch name encoding error

Signed-off-by: Kazuhiro Kato
Signed-off-by: Paul Mackerras

After "git checkout -b '漢字'"(man) to create a branch with UTF-8 character in it, "gitk" shows the branch name incorrectly, as it forgets to turn the bytes read from the "git show-ref"(man) command into Unicode characters.

Upvotes: 2

sleske
sleske

Reputation: 83577

Judging from the gitk screenshot, the hyphen in the branch name is not a regular ASCII hyphen (ASCII code 45), but an Em dash, encoded as UTF-8, and then interpreted (by gitk) with character encoding Windows-1252 (aka "Codepage 1252").

The Em dash has Unicode code point 2014, which translates to the UTF-8 byte sequence E2 80 94. If you interpret these three bytes as Windows-1252, you obtain the three characters — (the Unicode code points "LATIN SMALL LETTER A WITH CIRCUMFLEX", "EURO SIGN", "RIGHT DOUBLE QUOTATION MARK") - which is what gitk shows (apparently gitk defaults to character encoding Windows-1252 on your system).


To delete this branch, you need to grab an Em dash somewhere. One way is to start Windows Wordpad (or Microsoft Word), then type Alt 0151 (you must use the numeric keypad!).

That enters an Em dash. Copy that character, then paste it into your git shell into this command, before the "D":

git branch -d 'D'

Enter, and you're done :-).

Upvotes: 4

CodeWizard
CodeWizard

Reputation: 141946

Warp the branch name with quotes

git branch -D "-D"

Other options

If you have it locally you can simply remove the file from your .git/ref/heads/branch_name folder and delete it.

Upvotes: 7

Related Questions