344

Basically I'm trying to alias:

git files 9fa3

...to execute the command:

git diff --name-status 9fa3^ 9fa3

but git doesn't appear to pass positional parameters to the alias command. I have tried:

[alias]
    files = "!git diff --name-status $1^ $1"
    files = "!git diff --name-status {1}^ {1}"

...and a few others but those didn't work.

The degenerate case would be:

$ git echo_reverse_these_params a b c d e
e d c b a

...how can I make this work?

5
  • 23
    Note that in git 1.8.2.1 it's possible to do that without shell function (your original approach with $1 should work).
    – Eimantas
    Aug 15, 2013 at 12:49
  • 12
    @Eimantas Would you care to elaborate in an an answer? It doesn't work for me, and I can't find any documentation about it.
    – pavon
    Aug 18, 2014 at 18:46
  • 1
    @Eimantas there's nothing about this in the release notes though.
    – Knu
    Oct 1, 2016 at 3:52
  • 2
    i can confirm i can run shell commands with arguments without any shenanigans in Git 2.11.
    – anarcat
    Feb 14, 2018 at 17:20
  • @Eimantas Can you maybe create an answer with a detailed explanation?
    – leonheess
    Feb 7, 2022 at 20:44

7 Answers 7

466

A shell function could help on this:

[alias]
    files = "!f() { git diff --name-status \"$1^\" \"$1\"; }; f"

An alias without ! is treated as a Git command; e.g. commit-all = commit -a.

With the !, it's run as its own command in the shell, letting you use stronger magic like this.

UPD
Because commands are executed at the root of repository you may use ${GIT_PREFIX} variable when referring to the file names in commands

14
  • 10
    Thanks, this looks exactly right: [alias] files = "!f() { echo $3 $2 $1; }; f" ; $ git files a b c => c b a
    – ramses0
    Jul 23, 2010 at 23:20
  • 1
    @KohányiRóbert: That's actually not a shell script question; that's a particular of git config. An alias without ! is treated as a Git command; e.g. commit-all = commit -a. With the !, it's run as its own command in the shell, letting you use stronger magic like this.
    – Cascabel
    Oct 19, 2011 at 15:04
  • 51
    Be careful, ! will run at the root of the repository, so using relative paths when calling your alias will not give the results you might expect.
    – Drealmer
    Aug 8, 2013 at 16:28
  • 4
    @RobertDailey It doesn't break it, it just doesn't implement it. See stackoverflow.com/questions/342969/… for how to add it.
    – Cascabel
    Jun 23, 2014 at 16:08
  • 3
    Note: This doesn't quote arguments (which is dangerous in general). Also, a function is unnecessary. See my answer for more explanation.
    – Tom Hale
    Sep 16, 2016 at 3:56
145

The alias you are looking for is:

files = "!git diff --name-status \"$1\"^ \"$1\" #"

With argument validation:

files = "!cd -- \"${GIT_PREFIX:-.}\" && [ x$# != x1 ] && echo commit-ish required >&2 || git diff --name-status \"$1\"^ \"$1\" #"

The final # is important - it prevents all the user-supplied arguments from being processed by the shell (it comments them out).

Note: git puts all user-supplied arguments at the end of the command line. To see this in action, try: GIT_TRACE=2 git files a b c d

The escaped (due to nesting) quotes are important for filenames containing spaces or "; rm -rf --no-preserve-root /;)

11
  • 3
    For the simplest cases this is the right answer, there's really no need to complicate by wrapping it in a function or sh -c.
    – Ed Randall
    Mar 8, 2017 at 8:08
  • 5
    Yes, ! already implies sh -c (shown when prepending GIT_TRACE=2), so there's no need to run a another sub-shell. What issues do you see in more complicated cases?
    – Tom Hale
    Mar 9, 2017 at 1:18
  • Does this work if you want to set default arguments? e.g. I want to do this to fetch a Github PR: fp = "! 1=${1:-$(git headBranch)}; 2=${2:-up}; git fetch -fu $2 pull/$1/head:$1; git checkout $1; git branch -u $2 #". This works great without the first two statements, but falls down if you use them. (I have headBranch = symbolic-ref --short HEAD as well).
    – gib
    Aug 12, 2017 at 19:17
  • 2
    Worked it out, it works if you set new params, so this is fine: fp = "! a=${1:-$(git headBranch)}; b=${2:-up}; git fetch -fu $b pull/$a/head:$a; git checkout $a; git branch -u $b #".
    – gib
    Aug 12, 2017 at 19:31
  • 1
    @EugenKonkov Quotes are needed since the expansion of variables could contain spaces, and we want to keep them as a single shell token.
    – Tom Hale
    Nov 26, 2018 at 7:05
108

You can also reference sh directly (instead of creating a function):

[alias]
        files = !sh -c 'git diff --name-status $1^ $1' -

(Note the dash at the end of the line -- you'll need that.)

11
  • 8
    If you're sharing the command, you probably want to use sh, since that is in itself a shell, and it's available on the vast majority of systems. Using the default shell only works if the command works as written for all shells.
    – nomothetis
    Dec 6, 2012 at 16:20
  • 18
    I prefer -- to - as it's more familiar and less likely to accidentally mean stdin at some point. ("An argument of - is equivalent to --" in bash(1) is ungoogleable)
    – bsb
    Aug 26, 2013 at 0:28
  • 4
    See also Official Git Wiki - Advanced aliases with arguments.
    – user456814
    Jun 24, 2014 at 1:31
  • 8
    What is the exact meaning of the ending '-' and where is it documented ?
    – Zitrax
    Apr 21, 2016 at 13:24
  • 8
    Note: This doesn't quote arguments (which is dangerous in general). Creating a sub-shell (with sh -c) is also unnecessary. See my answer for an alternative.
    – Tom Hale
    Sep 16, 2016 at 4:07
34

Use GIT_TRACE=1 described on the git man page to make the alias processing transparent:

$ git config alias.files
!git diff --name-status $1^ $1
$ GIT_TRACE=1 git files 1d49ec0
trace: exec: 'git-files' '1d49ec0'
trace: run_command: 'git-files' '1d49ec0'
trace: run_command: 'git diff --name-status $1^ $1' '1d49ec0'
trace: exec: '/bin/sh' '-c' 'git diff --name-status $1^ $1 "$@"' 'git diff --name-status $1^ $1' '1d49ec0'
trace: built-in: git 'diff' '--name-status' '1d49ec0^' '1d49ec0' '1d49ec0'
trace: run_command: 'less -R'
trace: exec: '/bin/sh' '-c' 'less -R' 'less -R'
MM      TODO

Your original commands work with git version 1.8.3.4 (Eimantas noted this changed in 1.8.2.1).

The sh -c '..' -- and f() {..}; f options both cleanly handle the "$@" parameters in different ways (see with GIT_TRACE). Appending "#" to an alias would also allow positional parameters without leaving the trailing ones.

1
  • 1
    thanks for the explanations: those commands work for me on the original problem, following your advice: files = "!git diff --name-status $1^ $1 #" files = "!git diff --name-status $1^" Apr 21, 2015 at 12:19
21

As stated by Drealmer above:

« Be careful, ! will run at the root of the repository, so using relative paths when calling your alias will not give the results you might expect. – Drealmer Aug 8 '13 at 16:28 »

GIT_PREFIX being set by git to the subdirectory you're in, you can circumvent this by first changing the directory :

git config --global alias.ls '!cd "${GIT_PREFIX:-.}"; ls -al'

5
  • I'm having trouble with this as well (commands being run at the root of the repository) but this solution doesn't seem to do anything. (If it matters, I'm using OS X.)
    – waldyrious
    Apr 4, 2016 at 17:43
  • Oops... git alias is an alias I made. Apr 6, 2016 at 13:45
  • (since git 1.8.2) git config --set alias.alias = '! git config --global alias.$1 "$2"' Apr 6, 2016 at 13:46
  • This is what ended up working for me: "prefix your git aliases (that run shell commands and need the right pwd) with cd ${GIT_PREFIX:-.} &&." (source: stackoverflow.com/a/21929373/266309)
    – waldyrious
    Apr 6, 2016 at 14:52
  • Do quote this. !cd "${GIT_PREFIX:-.}" && ls -al
    – mirabilos
    Mar 2, 2018 at 23:53
10

I wanted to do this with an alias that does this:

git checkout $1;
git merge --ff-only $2;
git branch -d $2;

In the end, I created a shell script named git-m that has this content:

#!/bin/bash -x
set -e

#by naming this git-m and putting it in your PATH, git will be able to run it when you type "git m ..."

if [ "$#" -ne 2 ]
then
  echo "Wrong number of arguments. Should be 2, was $#";
  exit 1;
fi

git checkout $1;
git merge --ff-only $2;
git branch -d $2;

This has the benefit that it's much more legible because it's on multiple lines. Plus I like being able to call bash with -x and set -e. You can probably do this whole thing as an alias, but it would be super ugly and difficult to maintain.

Because the file is named git-m you can run it like this: git m foo bar

3
  • 2
    I like this a lot more too, but I haven't been able to figure out how to use the autocomplete I want with this approach. On aliases you can do this: '!f() { : git branch ; ... }; f' and it will autocomplete the alias as a branch which is super handy.
    – Hassek
    May 11, 2017 at 22:57
  • Yeah, I think I prefer having the non-trivial things done as individual script files on the path. The down side though is yes, you loose automatic completion of things like references. You can though fix this up by manually configuring your own auto-completion. Again though, I like that you can just drop a script into a folder on the path and it will start working, but for the auto-completion, you need to 'load' it, so usually it's in my .bashrc file that I source. But I don't think I change how I auto-complete arguments to a script as much as the script itself, and it'd only be during dev.
    – thecoshman
    Nov 5, 2018 at 16:11
  • GOSH, YES. I was killing myself over trying to use a shell variable on the ! inline git alias....... Aug 27 at 23:46
7

Just bumped into something similar; hope it's oK to post my notes. One thing that confuses me about git aliases with arguments, probably comes from the git help config (I have git version 1.7.9.5):

If the alias expansion is prefixed with an exclamation point, it will be treated as a shell command. For example, defining "alias.new = !gitk --all --not ORIG_HEAD", the invocation "git new" is equivalent to running the shell command "gitk --all --not ORIG_HEAD". Note that shell commands will be executed from the top-level directory of a repository, which may not necessarily be the current directory. [...]

The way I see it - if an alias "will be treated as a shell command" when prefixed with an exclamation point - why would I need to use a function, or sh -c with arguments; why not just write my command as-is?

I still don't know the answer - but I think actually there is a slight difference in the outcome. Here's a little test - throw this in your .git/config or your ~/.gitconfig:

[alias]
  # ...
  ech = "! echo rem: "
  shech = "! sh -c 'echo rem:' "
  fech = "! f() { echo rem: ; }; f " # must have ; after echo!
  echargs = "! echo 0[[\"$0\"]] 1-\"$1\"/ A-"$@"/ "
  fechargs = "! f() { echo 0[[\"$0\"]] 1-\"$1\"/ A-"$@"/ ; }; f "

Here is what I get running these aliases:

$ git ech word1 word2
rem: word1 word2

$ git shech word1 word2
rem:

$ git fech word1 word2
rem:

$ git echargs word1 word2
0[[ echo 0[["$0"]] 1-"$1"/ A-$@/ ]] 1-word1/ A-word1 word2/ word1 word2

$ git fechargs word1 word2
0[[ f() { echo 0[["$0"]] 1-"$1"/ A-$@/ ; }; f ]] 1-word1/ A-word1 word2/

... or: when you're using a "plain" command after the ! "as-is" in a git alias - then git automatically appends the arguments list to that command! A way to avoid it, is indeed, to call your script as either a function - or as the argument to sh -c.

Another interesting thing here (for me), is that in a shell script, one typically expects the automatic variable $0 to be the filename of the script. But for a git alias function, the $0 argument is, basically, the content of the entire string specifying that command (as entered in the config file).

Which is why, I guess, if you happen to misquote - in the below case, that would be escaping the outer double quotes:

[alias]
  # ...
  fail = ! \"echo 'A' 'B'\"

... - then git would fail with (for me, at least) somewhat cryptic message:

$ git fail
 "echo 'A' 'B'": 1: echo 'A' 'B': not found
fatal: While expanding alias 'fail': ' "echo 'A' 'B'"': No such file or directory

I think, since git "saw" a whole string as only one argument to ! - it tried to run it as an executable file; and correspondingly it failed finding "echo 'A' 'B'" as a file.

In any case, in the context of the git help config quote above, I'd speculate that it's more accurate to state something like: " ... the invocation "git new" is equivalent to running the shell command "gitk --all --not ORIG_HEAD $@", where $@ are the arguments passed to the git command alias from the command line at runtime. ... ". I think that would also explain, why the "direct" approach in OP doesn't work with positional parameters.

2
  • nice test. A quick way to check all possibilities!
    – albfan
    Apr 27, 2013 at 12:11
  • fail is trying to run a command called "echo 'A' 'B" (ie. 10 chars long). Same error from sh -c "'echo a b'" and same cause, too many layers of quotes
    – bsb
    Aug 25, 2013 at 22:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.