Question

Is there an option to show the full command when using an alias?

Example:

$ git ci -m "initial commit"
Full command: git commit -m "initial commit"
...
$ git lg
Full command: git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
...

Aliases are very convenient, but I like to learn/be reminded what my alias actually do (most of my aliases are copied from the Internet)

Était-ce utile?

La solution

As an example:

log-1 = "!sh -c 'echo \"Full command: git log --graph --decorate --pretty=oneline --abbrev-commit\"; git log --graph --decorate --pretty=oneline --abbrev-commit' -"

You call the shell and execute the given commands.

In your lg example, you would have to do a whole lot of escaping as you have quotes inside qoutes and characters that need to be escaped. I suggest you create your own pretty format and use that in the alias. Let;s assume we call your format mine. This is what you need to do:

git config --add pretty.mine "%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%C(bold blue)<%an>%Creset"

and the alias would be

lg = "!sh -c 'echo \"Full command: git log --graph --pretty=mine --abbrev-commit --date=relative\"; git log --graph --pretty=mine --abbrev-commit --date=relative' -"

Autres conseils

Another option is the command ideas listed in the Git wiki section on Aliases wich gives, for the alias section of .git/config

[alias]

    aliases = !git config --get-regexp 'alias.*' | colrm 1 6 | sed 's/[ ]/ = /'

This then lists all your aliases as command lines.

If you're using bash you can add "set -x" to the alias. So, as a simple example, if you have an alias:

ol = !sh -c 'git log --oneline'

you'd change it to:

ol = "!sh -c 'set -x; git log --oneline'"

It needs the double quotes around because of the semi-colon.

In order to allow the rest of the command line, you'd add in "$@", and also put a "-" so that the arguments start from $1. This gives you:

ol = "!sh -c 'set -x; git log --oneline $@' -"

More complicated aliases will probably have this stuff in already.

I don't know of a way to set this globally for all aliases though it would be nice to have!

EDIT: This actually works a little easier using a shell function instead. You can do something like:

git config alias.ol '!f() { set -x; git log --oneline $@; }; f'

to get the same effect as above.

Enabling git's trace feature using environment variable GIT_TRACE will show the exact command executed by git after expansion of the git alias.

This will also alleviate any overhead incurred by spawning a(nother) shell and whatever processing it does.

More complex git commands will provide verbose output explaining what git is doing. If you just want to know the result of the alias expansion, you could catch stderr and grep for the expansion (see example below). However, watch out for doing this using commands that actually modify your repository (use --dry-run if applicable).

Example

Having an alias ls1 defined as:

~$ cat .gitconfig|grep ls1
    ls1 = log --oneline -1

Will produce output like (performed on git's own repository after commit 2befe97 on default branch master):

~/git (master=)$ GIT_TRACE=1 git ls1
11:14:16.075127 git.c:561               trace: exec: 'git-ls1'
11:14:16.075182 run-command.c:334       trace: run_command: 'git-ls1'
11:14:16.078823 git.c:278               trace: alias expansion: ls1 => 'log' '--oneline' '-1'
11:14:16.079032 git.c:344               trace: built-in: git 'log' '--oneline' '-1'
11:14:16.080189 run-command.c:334       trace: run_command: 'pager'
11:14:16.080536 run-command.c:193       trace: exec: 'pager'
2befe97 Eighth batch
~/git (master=)$ 

Catching Expansion

~/git (master=)$ GIT_TRACE=1 git ls1 2>&1 |grep "trace: alias expansion"
11:37:45.014231 git.c:278               trace: alias expansion: ls1 => 'log' '--oneline' '-1'
~/git (master=)$ 
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top