0

I picked up a git alias online that is supposed to be the command git rebase -i HEAD~$1 where $1 is a number passed to the alias. Here is the git alias I've got setup in my .zshrc file:

alias grn="! sh -c \"git rebase -i HEAD~$1\" -"

Example usage from the terminal:

$ grn 3 // This should translate to git rebase -i HEAD~3

The issue i'm running into is that the passed integer argument (e.g. 3), is not being passed to my alias so the git alias is effectively always running git rebase -i HEAD~.

Any clues on how to fix this alias?

Robert Cooper
  • 2,160
  • 1
  • 9
  • 22
  • 1
    After checking your alias in [shellcheck](https://www.shellcheck.net/) it said that I am missing space after exclamation mark. Could you try with `alias grn="! sh -c \"git rebase -i HEAD~$1\" -"` then? – dmadic May 05 '18 at 23:26
  • Nice, so the command runs now, but it doesn't take in a passed integer argument. I've updated my question accordingly. – Robert Cooper May 05 '18 at 23:30
  • 1
    Refering to [this question](https://stackoverflow.com/q/7131670/7411306) I would say having an alias which takes parameters is not possible. You could easily do it with single-line function though. Just put `grn() { git rebase -i HEAD~"$1"; }` in `.zshrc` and you can run it the same way as an alias. – dmadic May 05 '18 at 23:39
  • 1
    Oh, that's beautiful! Thanks @dmadic. If you write that up as an answer I'll mark it as accepted ✅ – Robert Cooper May 05 '18 at 23:48
  • I copied it to an answer. Thanks @RobertCooper. :) – dmadic May 05 '18 at 23:51
  • 2
    The alias from the linked article is a git alias, not a shell alias. – rkta May 06 '18 at 08:48
  • 1
    @rkta Oh wow, you're right! If I add the command to my `.gitconfig` as an alias, it works as intended. Thanks for pointing that out :). I wasn't even aware that git aliases were a thing to be honest. – Robert Cooper May 06 '18 at 12:52

2 Answers2

4

Shell alias with parameters is impossible, but git alias is definitely possible. Either

git config alias.grn '! sh -c "git rebase -i HEAD~$1" -'

or

git config alias.grn '!f() { git rebase -i HEAD~$1; }; f'

Run as git grn 3.

phd
  • 82,685
  • 13
  • 120
  • 165
  • so good! Thank you. I've added this alias into my .gitconfig file :) `rb = "!f() { git rebase -i HEAD~${1}; }; f"` – suyeon woo Jul 24 '21 at 15:17
1

Refering to this question I would say having an alias which takes parameters is not possible.

You could easily do it with single-line function though.

Just put:

grn() { git rebase -i HEAD~"$1"; } 

in .zshrc and you can run it the same way as an alias.

dmadic
  • 477
  • 4
  • 7