20

Let say I have an alias like this in my .gitconfig:

alias.showlog = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' 

and now I want a similar alias like this:

alias.sl = showlog --abbrev-commit

When I try the command git sl it say he does not know the showlog command.

I know it is still possible to copy the same command like the other alias, but I just want to know if there is any possibility to refer another alias in an alias?

tshepang
  • 12,111
  • 21
  • 91
  • 136
рüффп
  • 5,172
  • 34
  • 67
  • 113
  • I think no. Have you tried? – Jepessen Feb 27 '14 at 12:18
  • i tried to put the two mentionned lines in my `.gitconfig`; after there is perhaps a special notation for doing that but did not find anything – рüффп Feb 27 '14 at 13:06
  • I don't have git in this computer. if "alias.sl = alias.showlog --abbrev-commit" does not work either I can't find other solutions. – Jepessen Feb 27 '14 at 13:43
  • 2
    That will be possible with Git 2.20 (released in Q4 2018). See [my answer below](https://stackoverflow.com/a/52863852/6309). – VonC Oct 17 '18 at 21:27

2 Answers2

31

In versions of Git before 2.20:

Not that way, but you can make an alias run a command through the shell, hence running another instance of git which resolves the second alias:

alias.sl = !git showlog --abbrev-commit

In 2.20 or later, see VonC's answer.

torek
  • 448,244
  • 59
  • 642
  • 775
  • 1
    Works, but returns: `error: git showlog --abbrev-commit died of signal 13` when quitting out of the log view. To prevent this, add an `exit` command to the end of the alias running the shell command like this: `sl = !git showlog --abbrev-commit && exit ` – Brice Jun 29 '16 at 17:25
  • @briceshatzer: Interesting, signal 13 is SIGPIPE. Shells generally don't complain about SIGPIPE in part because `... | less` often causes the `...` part to quit with SIGPIPE. I recall seeing some SIGPIPE handling changes recently in Git, but adding one extra shell command like that will make the shell eat the SIGPIPE, and should work pretty much everywhere. – torek Jun 29 '16 at 19:10
  • Shell is no longer needed. See @vonc's answer. – Joshua Goldberg Nov 05 '19 at 17:58
17

Update Q4 2018: Yes, it is possible possible with Git 2.20: an alias that expands to another alias has so far been forbidden, but now it is allowed to create such an alias.

See commit fef5f7f, commit 82f71d9, commit c6d75bc (16 Sep 2018) by Tim Schumacher (timschumi).
(Merged by Junio C Hamano -- gitster -- in commit 506ee60, 16 Oct 2018)

alias: add support for aliases of an alias

Aliases can only contain non-alias git commands and their arguments, not other user-defined aliases. Resolving further (nested) aliases is prevented by breaking the loop after the first alias was processed.
Git then fails with a command-not-found error.

Allow resolving nested aliases by not breaking the loop in run_argv() after the first alias was processed.
Instead, continue the loop until handle_alias() fails, which means that there are no further aliases that can be processed. Prevent looping aliases by storing substituted commands in cmd_list and checking if a command has been substituted previously.

So... this will be possible now:

git config alias.nested-internal-1 nested-internal-2
git config alias.nested-internal-2 status
git nested-internal-1

That would be a git status.


With Git 2.30 (Q1 2021), the command line completion script (in contrib/) learned to expand commands that are alias of alias.

See commit e4c75ed (12 Nov 2020), and commit c2822a8, commit 9414938 (09 Nov 2020) by Felipe Contreras (felipec).
(Merged by Junio C Hamano -- gitster -- in commit fd6445a, 25 Nov 2020)

completion: bash: support recursive aliases

Signed-off-by: Felipe Contreras

It is possible to have recursive aliases like:

l = log --oneline
lg = l --graph  

So the completion should detect such aliases as well.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250