Syntax for Git aliases with multiple commands

后端 未结 4 885
滥情空心
滥情空心 2020-12-08 13:42

I want to create a Git alias to perform multiple commands, but I cant find documentation on how this is done.

  1. What is the syntax for Git aliases with multiple
相关标签:
4条回答
  • 2020-12-08 13:51

    Say the commands are echo a and echo b (not a and b), to add multiple commands for an alias q:

    From the command line:
    git config alias.q '!echo a; echo b'

    Directly in the configuration file:

    [alias]
        q = "!echo a; echo b"
    

    For more complex things, define a shell function and call it:
    '!f() { echo a ; echo b ; }; f'

    For passing parameters to the commands see:
    Git alias with positional parameters
    Git Alias - Multiple Commands and Parameters

    Based in Jonathan Wakely's comment

    0 讨论(0)
  • 2020-12-08 13:52

    I struggled to find a nice example for myself too. After some research, here's what I came up with:

    git config --global alias.purge '!f() { if [ $(git ls-remote --heads origin $1 | wc -l) -eq 1 ]; then git branch -d $1 && git push origin --delete $1; else echo "remote branch not found"; fi } ; f'
    

    Let's break that down from left to right, shall we?

    git config --global alias. we know by now. It's the code we need in order to create an alias. Often referred to as [alias] on this site

    purge is the shorthand name I decided to give to my alias

    'f() { we start to write our function

    if [ the start of our if-statement, closing it later on with ]

    git ls-remote --heads origin $1 checks if there is a remote branch with the name we supplied. Return value would be nothing if there's no branch, otherwise, it'll return the reference tag. By enclosing it in $( ) we make sure it's known to Bash as a command

    | wc -1 added as a suffix, converts the return value to 0 if there's no remote branch, 1 if there is

    -eq 1 turns the whole enclosed command into a boolean, read as (returnValue == 1)

    NOTE: because the commands are written on one line, instead of \n behind each, we need to type ; after each command

    then is used to announce what our function should do if the boolean returns true

    git branch -d $1 will delete the local branch of the parameter we entered

    && this logical operator will ensure both commands are carried out

    git push origin –delete $1 will delete the remote branch of the parameter we entered

    else does what the second half of an if-else should do

    echo "remote branch not found" is a String I want to return in case there's no remote branch

    fi announces the end of our if-else statement

    } ; f' closes our function

    0 讨论(0)
  • 2020-12-08 13:59

    Addendum Answer: Often I need more complex commands that decide what to do through positional parameters and branch on the parameters or loop through parameters or input files.

    Such commands are too complex for single liners and they are hard to read and edit on one line. But I found a real simple method to do very complex commands from files:

    Assume you have a file called alias/cmd in your repository:

    !function f {
        if [ -z "$1" ]
        then echo "Please give me some argument!" 1>&2
            exit -1
        fi
        echo "Hello $1"
    }; f
    

    then you can simply say

    git config alias.cmd "`cat alias/cmd`"
    

    to define the alias cmd from the file and

    git config --get alias.cmd > alias/cmd
    

    to write the defined alias to the file.

    0 讨论(0)
  • 2020-12-08 14:09
    $ git config alias.q '!echo a; echo b'
    
    $ git q
    

    Output:

    a
    b
    

    I think this is (rudimentarily) documented in man git-config under alias.*

    Note that git commands should include git, unlike in normal aliases. It is caused by fact that it is treated as a shell command, not as a git command (see manpage quoted in the question). For example to chain

    git init
    

    and

    git commit --allow-empty -m "empty initial commit"
    

    it is necessary to create

    "!git init; git commit --allow-empty -m \"empty initial commit\""
    

    alias.

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