Search code examples
gitgithubzshiterm2zsh-completion

Zsh in iTerm2 completion causing git push error


I am using Zsh with iTerm2 and trying to follow these instructions to remove a commit that I accidentally pushed to a repo (don't worry, it doesn't have any followers other than me).

Running the command

git push -f origin HEAD^:develop

causes an error in Zsh

error: src refspec HEADER does not match any.
error: failed to push some refs to '[email protected]:xxx/yyy.git'

There is a file named HEADER in this directory, so I am thinking Zsh or iTerm is trying to be smart by doing some completion with ^.

I did the same push command in Terminal.app (which still uses /bin/sh) and it worked.

What is this Zsh/iTerm behavior and how can I control it?


Solution

  • I suspect that zsh is attempting to expand HEAD^:develop, which is a valid glob pattern and is accurately matching the HEADER file.

    One easy solution is to wrap your params in quotes, which will prevent zsh from being "helpful" and expanding those globbing patterns for you:

    git push -f origin "HEAD^":develop
    

    You can also escape problematic characters (as suggested in the docs for git-rm):

    git push -f origin HEAD\^:develop