345

I was trying to create a branch from master with the following command,

git branch SSLOC-201_Implement___str__()_of_ProductSearchQuery

when Git suddenly stopped responding. I suspect the unescaped () are to blame, somehow. Now, whenever I try to run any Git command, I get the same error:

git:176: command not found: _of_ProductSearchQuery

with the number after git increasing every time I type a command.

Can anyone explain what happened? And how do I get back to normal? I'd like to delete that branch, but how can I do that?

jub0bs
  • 60,866
  • 25
  • 183
  • 186
ruipacheco
  • 15,025
  • 19
  • 82
  • 138
  • 8
    I would guess this is related to your zsh environment as I was able to run create the branch in my bash shell with no ill side-effects (lubuntu 13.10), but I'm seeing the error when I switch to my totally vanilla zsh – Jonathan.Brink Sep 02 '15 at 14:23
  • 31
    In the future, quote stuff that looks suspect. `git branch "SSLOC-201_Implement___str__()_of_ProductSearchQuery"` works just fine. – Qix - MONICA WAS MISTREATED Sep 02 '15 at 14:49
  • 11
    @Qix Better to avoid problematic characters altogether. – jub0bs Sep 02 '15 at 16:04
  • 3
    @Jubobs Definitely, though I've seen certain companies enforce weird branch names like this. – Qix - MONICA WAS MISTREATED Sep 02 '15 at 16:04
  • Be mindful of special characters in your commands: http://tldp.org/LDP/abs/html/special-chars.html – Dwight Spencer Sep 02 '15 at 19:28
  • 1
    @DwightSpencer Your link is specific to Bash, but this question is zsh-specific. The problem doesn't actually occur in Bash. – jub0bs Sep 09 '15 at 09:17
  • @Jubobs Its not about being bash or zsh. The UNIX/OpenGroup standard states there are special characters one uses in a shell command and the '()' characters are just two of the few listed in the link provided. The issue itself persists across all shells (plan9 rc, tcsh, ash/bash/dash, and zsh/korn) with some of the more commonly installed ones actually given an intelligent error message instead of breaking the script. But for the sake of documentation: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html – Dwight Spencer Sep 09 '15 at 16:13
  • @DwightSpencer We're on the same page, but what I meant is that the particular problem that the OP is experiencing in zsh doesn't occur in bash. – jub0bs Sep 09 '15 at 16:15
  • @Jubobs True, I can confirm that and has been since at lease version 4. As for this being only zsh well that's since zsh is more "power user" thus more BOFH users and less PFY admins so there's no need to teach the user of their wrong doings. While bash, tcsh, and rc it would state syntax error. In older version though it would behave the same as the error above. All in all its still better to document the POSIX standard than x tool solution. – Dwight Spencer Sep 09 '15 at 16:25
  • 1
    @DwightSpencer You write: *[...] there's no need to teach the user of their wrong doings*. However, the OP wrote: *Can anyone explain what happened?*. My answer provides an explanation. I also recommend avoiding special shell characters, at the bottom of my answer. Is something missing? – jub0bs Sep 09 '15 at 16:39
  • 1
    @Jubobs A patient goes to a doctor and says, "it hurts when I write the letter 'g' ." The doctor responds, "You can be expressive enough with the other letters" I do not like this doctor. – s4y Sep 20 '15 at 19:13
  • @Sidnicious Sure, feel free to use parentheses in branch names. However, in my opinion, the need for quoting such names at the command line is a good enough disincentive. – jub0bs Sep 26 '15 at 12:28

1 Answers1

624

Problem

Can anyone explain what happened? [...] I'd love to be able to delete that branch, but Git won't work for me.

By running

git branch SSLOC-201_Implement___str__()_of_ProductSearchQuery

in zsh, you did not create any branch. Instead, you accidentally defined three shell functions, called git, branch, and SSLOC-201_Implement___str__, which ignore their parameters (if any) and whose body is _of_ProductSearchQuery. You can check for yourself that this is indeed what happened, by invoking the builtin zsh command called functions, which lists all existing shell functions:

$ functions                                                     
SSLOC-201_Implement___str__ () {
    _of_ProductSearchQuery
}
branch () {
    _of_ProductSearchQuery
}
git () {
    _of_ProductSearchQuery
}

Unfortunately, although the other two shell functions are not problematic, the shell function called "git" now shadows the bona fide git command!

$ which git
git () {
    _of_ProductSearchQuery
}
# but the real "git" is a binary file that lives in /usr/local/bin/git (or some similar path)

Therefore, you will subsequently get the error

command not found: _of_ProductSearchQuery

whenever you attempt to run a Git command, e.g. git log, git status, etc. (assuming, of course, that no command called _of_ProductSearchQuery exists).

Side note

[...] I get the same error:

git:176: command not found: _of_ProductSearchQuery

(with the number after git increasing every time I type a command)

That number simply corresponds to the value of HISTCMD, an environment variable that holds

[t]he current history event number in an interactive shell, in other words the event number for the command that caused $HISTCMD to be read.

See the zsh manual for more details.

Solution

And how do I get back to normal?

Simply delete the problematic shell function (and the other two you created by accident, while you're at it):

unset -f git
unset -f branch SSLOC-201_Implement___str__

Then everything should be fine.

What if unset is shadowed also?!

Good question! I refer you to Wumpus W. Wumbley's excellent comment below.


Branch-naming tips

Avoid any special shell characters

Yes, as pointed out in the comments, parentheses are valid characters in Git branch names; you just need to quote the name appropriately, e.g.

$ git branch 'foo()bar'
$ git branch
  foo()bar
* master
$ git checkout 'foo()bar'
Switched to branch 'foo()bar'

However, the need for quoting such names every single time when used as command-line arguments should convince you to eschew parentheses in reference names. More generally, you should (as much as possible) avoid characters that have a special meaning in shells, to prevent surprises like this one.

Use simple branch names

You should keep your branch names short and sweet anyway. Long descriptions like

SSLOC-201_Implement___str__()_of_ProductSearchQuery

belong in commit messages, not in branch names.

Community
  • 1
  • 1
jub0bs
  • 60,866
  • 25
  • 183
  • 186