1

I'm looking to create an alias that takes a diff between the merge base of a branch and origin/HEAD. Ideally, I'd be able to write something like:

[alias]
  hd = diff origin/HEAD...

however, that ends up being expanded out to this:

git diff origin/HEAD... branchname

The problem there is the space between ... and branchname causes a different result than I'm looking for. I can invoke this as:

[alias]
  hd = "!git diff origin/HEAD...$@"

and get what I'm looking for, however, that breaks things like --name-only for:

git hd --name-only

unless I explicitly write HEAD and put the arguments later:

git hd HEAD --name-only

Is there a way to write this alias that works and still allows for typical ordering of flags?

It looks like something hd = diff --merge-base origin/HEAD would probably work, but I'm stuck on 2.25, currently.

dfreese
  • 378
  • 1
  • 10
  • 2
    Git aliases are too weak to do proper parameter parsing. Write a shell script, call if `git-doh`, put it in `$PATH` and you can call it `git doh`. – phd Apr 18 '22 at 01:39
  • 2
    Or as a sort of hybrid variant of what @phd suggested, make your alias define a shell function and then invoke it: `alias foo = '!f() { ...; } f`. Inside the shell function `f` you have `$1`, `$2`, `$#`, `$@`, etc., as desired and can do things like `arg1=$1; shift;` to split out the first argument into a named variable. You can invoke `git rev-parse` to do your parsing. But once things get complicated, you're probably best off with a real script anyway. – torek Apr 18 '22 at 10:04
  • Related to [this one](https://stackoverflow.com/questions/65224416/how-to-alias-git-checkout-b-with-a-string-format-for-the-branch-name), I guess. Duplicate? – Romain Valeri Apr 18 '22 at 12:15

0 Answers0