Git alias with positional parameters

前端 未结 7 849
北荒
北荒 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:41

    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

    0 讨论(0)
提交回复
热议问题