How to embed a bash script directly inside a git alias

Is it possible to insert the following bash shell code:

for name in $(git diff --name-only $1); do git difftool $1 $name & done 

directly in creating a git alias:

 git config --global alias.diffall ***my-bash-code-here*** 

This leads to my previous question / answer on SO, where I put the code in a .sh file and then smooth the file:

 git config --global alias.diffall '!sh diffall.sh' 

But in an endless quest, for simplicity, should there be a way to skip a file and paste the code directly into an alias? I can not understand the format ...

+63
git bash shell alias
Aug 20 '09 at 23:42
source share
5 answers
 git config --global alias.diffall '!sh diffall.sh' 

It is redundant in one direction. If you add 'diffall.sh' to your $ PATH anyway, why not save it as 'git-diffall' and save yourself the trouble of declaring an alias. Yes, "git diffall" will launch it.

+70
Aug 21 '09 at 1:29
source share

To run commands inside a git alias and, in particular, pass arguments to these commands, you probably have to create a temporary function that you call immediately:

 $ vim ~/.gitconfig ... [alias] # compare: foo = "! echo begin arg=$1/$2/end" foo2 = "!f() { echo "begin arg=$1/$2/end"; }; f" 

In this example, you probably need a function (and also more flexible as to what you can do in one β€œstatement”); and you can probably say that for both options the rest of the git command arguments are simply passed as args for the alias, whether it is "echo" or "f"; the function call simply absorbs the arguments, ignoring what is not explicitly used:

 $ git foo abc begin arg=a/b/end abc $ git foo2 abc begin arg=a/b/end 

Another example (all aliases based on the matching pattern are listed) (note: you can reuse the same function name "f ()" in the .gitconfig file):

 [alias] alias = "!f() { git config --get-regexp "^alias.${1}$" ; }; f" 

The first returns an alias only for "foo $", the second for "foo. *":

 $ git alias foo alias.foo ! echo begin arg=$1/$2/end $ git alias 'foo.*' alias.foo ! echo begin arg=$1/$2/end alias.foo2 !f() { echo begin arg=$1/$2/end; }; f 

(nb: actual results may vary by shell, I use this with bash on Linux, Unix, and Cygwin (Windows).)

+22
Feb 21 '13 at 5:04 on
source share

I could not find in the documentation, but if you create the script path "git - <name>", you can call it "git name" in your repo.

Cm:

 $ cd ~/bin $ echo "echo I love this log: >pwd >git log --graph --summary --decorate --all" > git-logg $ chmod +x git-logg $ cd /path/to/your/repo $ git logg I love this log: /path/to/your/repo * commit 3c94be44e4119228cc681fc7e11e553c4e77ad04 (whatever-branch) | Author: myself <my@Laptop.(none)> | Date: Fri Apr 1 16:47:20 2011 +0200 | | would have been better not to do it at all | ... $ 

So, you can write any alias you like with this (rather obscure) way.

Moreover, you can add autocomplete to this new command defining a function. Info here

 $ _git_logg () { # you can return anything here for the autocompletion for example all the branches __gitcomp_nl "$(__git_refs)" } 
+20
Jul 16 '11 at 19:29
source share

Adding these two lines to your .git / config file should do the trick.

 [alias] diffall = '!for name in $(git diff --name-only $1); do git difftool $1 $name & done' 

Edit: perhaps the git -config version also works, but I like to save my aliases in the configuration file for easy management.

The wiki git has a nice page that explains aliases very clearly: http://git.or.cz/gitwiki/Aliases In particular, read "advanced aliases with arguments

+13
Aug 21 '09 at 0:03
source share

(From ProGit documentation: http://progit.org/book/ch7-3.html )

Have you tried adding a script to .git / hooks?

For example, if you create script.git / hooks / post-checkout:

 #!/bin/bash echo "This is run after a 'git checkout'" 

and then run the command:

 $ git checkout master Switched to branch 'master' This is run after a 'git checkout' 
-3
Dec 05 2018-11-12T00:
source share



All Articles