2

Is there a way to have Git print the semantics of an aliased command before executing it?

Assuming I have aliased a to add, I'd like the command git a to show

Executing: git add

before the actual output. Or perhaps there's an echo subcommand so that I alias a to echo add and the subcommand prints and actually executes the command?

krlmlr
  • 25,056
  • 14
  • 120
  • 217
  • 1
    have you found a better way than the 2 answers ? i'd really just would like so see the alias expansion and not the rest of the trace and transforming all aliases to `!echo ... ` ain't cutting it either for me – elonderin Jan 29 '21 at 11:01

2 Answers2

2

If you don't mind that a little bit more is printed, you can set the GIT_TRACE variable to 1 and git will print (among other) some information about alias expansion (cf. the man page).

In my case (I have defined an alias st for status), this looks like this:

$ GIT_TRACE=1 git st
trace: exec: 'git-st'
trace: run_command: 'git-st'
trace: alias expansion: st => 'status'
trace: built-in: git 'status'

Of course, you can for example put export GIT_TRACE=1 in your .bashrc to have this always enabled.

If you really want to show only the alias expansion, you could probably define a shell alias for git that runs git and filters out all lines from the output that start with trace: but do not contain alias expansion:.

Philipp Wendler
  • 11,184
  • 7
  • 52
  • 87
  • If I `.bashrc` it, isn't it going to add trace output on other occasions, too? – krlmlr Nov 16 '15 at 21:37
  • Sure, this would affect all your git processes. But you can set and unset it as you like, and also prefix the command with it as in my example to run git only a single time with it. – Philipp Wendler Nov 16 '15 at 21:41
1

You will need to define the alias to include the echo statement for you.

One way would be to define the alias like this:

"!echo \"executing git add\"; git add" 

There are a few different ways to do it that you can find in this question:

How to embed bash script directly inside a git alias

Community
  • 1
  • 1
Schleis
  • 41,516
  • 7
  • 68
  • 87