Git alias with positional parameters

前端 未结 7 1022
北荒
北荒 2020-11-22 12:35

Basically I\'m trying to alias:

git files 9fa3

...to execute the command:

git diff --name-status 9fa3^ 9fa3
7条回答
  •  一向
    一向 (楼主)
    2020-11-22 13:40

    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 /;)

提交回复
热议问题