gitgit-alias

echo arbitrary arguments in git


I am chaining a bunch of git commands in alias as below. How do I get the 'echo' part to work:

[alias]
        comb = ! sh -c 'echo \"Combining branches $1 with $2\"' && git checkout $2 && git merge $1 && git push && git checkout $1 && :

Some context: Git Alias - Multiple Commands and Parameters


Solution

  • Do not use single quotes around echo — Unix shells do not expand parameters inside single quotes. The fix is

    $ git config alias.comb '! sh -c "echo \"Combining branches $1 with $2\""'
    $ git config alias.comb 
    ! sh -c "echo \"Combining branches $1 with $2\""
    

    Example:

    $ git comb 1 2 3                
    Combining branches 1 with 2
    

    Or

    $ git config alias.comb '! sh -c "echo \"Combining branches $*\""'
    $ git config alias.comb         
    ! sh -c "echo \"Combining branches $*\""
    $ git comb 1 2 3                
    Combining branches 1 2 3