1

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

3 Answers 3

1

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 
Sign up to request clarification or add additional context in comments.

1 Comment

While all the suggestions are valid solutions, this explains what was wrong with my attempt.
1

The standard trick is to define a function which you immediately call.

[alias] comb = ! f () { echo "Combining branches $1 with $2" && git checkout "$2" && git merge "$1" && git push && git checkout "$1" && :; } f 

This simplifies the quoting.

4 Comments

I agree again, this is a work around. But what is the problem with the quotes in my attempt? (Or the echo alias that I commented below?
The single quotes already escape the double quotes; there's no need to escape them again with a backslash. Plus, the single quotes should have surrounded the entire list of commands for sh to execute them, rather than having sh only execute echo. Git aliases don't take arguments, only the shell, so you need a single command that can receive arguments (either sh or a function) in which to use $1` and $2.
git comb foo bar expands to f () { ... }; f foo bar (or in your original case, sh -c 'echo ...' && git ... && git ... && : foo bar).
Thanks, along with @phd answer, I get it now.
0

Your quotes are messed up:

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

You could also consider changing your alias to an executable called git-comb and store it somewhere in your path:

$ cat /path/to/executables/git-comb #!/bin/sh if [ "$#" -ne 2 ] then >&2 printf 'Usage: %s <ref> <ref>\n" "${0##*/}" exit 2 fi echo "Combining branches $1 with $2" git checkout "$2" git merge "$1" git push git checkout "$1" 

This way you can call it with:

$ git comb branch_1 branch_2 

1 Comment

I agree, I can create a sh script to get away. But even the following doesn't work comb = ! sh -c 'echo "Combining $1 with $2"'. Or let's say, I create an alias echo = ! sh -c 'echo "$*"', I expect git echo Hello World to work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.