se7entyse7en
se7entyse7en

Reputation: 4294

Adding git alias with pipes complains about wrong configuration

I am trying to add a git alias that makes me abort a rebase that have just finished. I tried to add this in .gitconfig:

rebabort = !sh -c 'git reflog | grep -v rebase | head -1 | sed -e "s/^\([[:alnum:]][[:alnum:]]*\)\(.*\)/\1/g" | git reset --hard'

but it complains about a wrong configuration. Anyway I tried to add the alias in the following way:

git config alias.rebabort '!git reflog | grep -v rebase | head -1 | sed -e "s/^\([[:alnum:]][[:alnum:]]*\)\(.*\)/\1/g" | git reset --hard'

and it worked. What am I missing?

UPDATE

I also tried to add this in .gitconfig

rebabort = "!f() { \
                git reflog | grep -v rebase | head -1 | sed -e 's/^\([[:alnum:]][[:alnum:]]*\)\(.*\)/\1/g\' | git reset --hard;
    }; f"

and this

rebabort = !"git reflog | grep -v rebase | head -1 | sed -e 's/^\([[:alnum:]][[:alnum:]]*\)\(.*\)/\1/g' | git reset --hard"

but it still complains about wrong configuration.

Upvotes: 2

Views: 359

Answers (1)

starlocke
starlocke

Reputation: 3661

I replicated your "line that worked" and then looked in my dummy project's .git/config and found the following:

[alias]
        rebabort = !git reflog | grep -v rebase | head -1 | sed -e s/^\\([[:alnum:]][[:alnum:]]*\\)\\(.*\\)/\\1/g | git reset --hard

Just copy that to your ~/.gitconfig

It looks like a simple matter of not needing the sh -c stuff. Your .gitconfig is not a .bashrc; I say this because it looked like you were trying to write an alias in .bashrc format.

Also, for your reference, you could have done the following for the same effect (your line that worked, PLUS the --global) option, since --global typically adds to your ~/.gitconfig:

git config --global alias.rebabort '!git reflog | grep -v rebase | head -1 | sed -e "s/^\([[:alnum:]][[:alnum:]]*\)\(.*\)/\1/g" | git reset --hard'

The help files for git config are very informative for learning new options and tricks, too. https://git-scm.com/docs/git-config

Upvotes: 2

Related Questions