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

Kap4Lin
  • 109
  • 5

3 Answers3

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.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • 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? – Kap4Lin Jan 27 '22 at 18:57
  • 1
    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`. – chepner Jan 27 '22 at 18:59
  • 1
    `git comb foo bar` expands to `f () { ... }; f foo bar` (or in your original case, `sh -c 'echo ...' && git ... && git ... && : foo bar`). – chepner Jan 27 '22 at 19:02
  • Thanks, along with @phd answer, I get it now. – Kap4Lin Jan 27 '22 at 19:50
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
phd
  • 82,685
  • 13
  • 120
  • 165
  • While all the suggestions are valid solutions, this **explains** what was wrong with my attempt. – Kap4Lin Jan 27 '22 at 19:47
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
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
  • 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. – Kap4Lin Jan 27 '22 at 18:47