Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zsh does not recognize HEAD^

Tags:

zsh

cygwin

prezto

I am using zsh and prezto with cygwin. When I type this git command 'git reset HEAD5', zsh does not found HEAD^. But when I switch to use bash, it works.

$ git reset HEAD^                                        
zsh: no matches found: HEAD^

Has anyone seen the same issue?

like image 991
michael Avatar asked Oct 10 '14 04:10

michael


People also ask

Which is better bash or zsh?

Zsh is built on top of bash thus it has additional features. Zsh is the default shell for macOS and Kali Linux. Zsh provides the user with more flexibility by providing various features such as plug-in support, better customization, theme support, spelling correction, etc.

What is Zshrc?

zshrc file is used to configure the zsh shell, you'll need to manually create one in your home directory for zsh to access. There's also a system-level zshrc file, but that is less commonly modified by users.

What is zsh on Mac?

The Z shell (also known as zsh ) is a Unix shell that is built on top of bash (the default shell for macOS) with additional features. It's recommended to use zsh over bash . It's also highly recommended to install a framework with zsh as it makes dealing with configuration, plugins and themes a lot nicer.


3 Answers

The ^ character is treated as special in filename expansions in zsh, but only if the EXTENDED_GLOB option is set:

zsh% setopt noEXTENDED_GLOB
zsh% echo HEAD^
HEAD^
zsh% setopt EXTENDED_GLOB
zsh% echo HEAD^
zsh: no matches found: HEAD^
zsh% 

Bash doesn't have this feature. (To be precise, bash does have an extended glob feature, enabled by shopt -s extglob, but bash's extended glob syntax doesn't treat the ^ character as special.)

With this feature enabled, ^ is a special character similar to * but with a different meaning. Like *, you can inhibit its special meaning by escaping it, either by enclosing it in single or double quotes or by preceding it with a backslash. Quoting is the simplest solution.

Rather than

git reset HEAD^

try this:

git reset 'HEAD^'

The meaning of the ^ wildcard is not relevant, since all you need to do is avoid using it, but I'll mention it anyway. According to the zsh manual, ^X matches anything except the pattern X. For the case of HEAD^, nothing follows the ^ -- which means that HEAD^ matches HEAD followed by anything other than nothing. That's a roundabout way of saying that HEAD^ matches file names starting with HEAD and followed by some non-empty string. Given files HEAD, HEAD1, and HEAD2, the pattern HEAD^ matches HEAD1 and HEAD2.

like image 112
Keith Thompson Avatar answered Sep 20 '22 21:09

Keith Thompson


A quick workaround to avoid the ^ character is to use git reset head~1 instead of git reset head^.

See this post for the difference between the two.

like image 40
Andrew Avatar answered Sep 20 '22 21:09

Andrew


issue 449 of oh-my-zsh describes this exact behaviour and provides the solution.

The culprit is the option extended_glob on zsh. Presto must be setting it. So when you type HEAD^ zsh tries to create a glob negation expression and fails with an error.

In other words, setopt extended_glob allows us to use ^ to negate globs.

To fix it, you can write this line on your .zshrc:

unsetopt nomatch 

With the above line, we are saying to zsh we want that when pattern matching fails, simply use the command "as is".

like image 20
ninrod Avatar answered Sep 21 '22 21:09

ninrod