Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed bash script directly inside a git alias

Can I embed the following bash shell code:

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

directly into the creation of a git alias:

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

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

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

But in the never-ending quest for simplicity, there's gotta be a way to skip the file and insert code directly into the alias? I can't figure out the format...

like image 580
Seba Illingworth Avatar asked Aug 20 '09 23:08

Seba Illingworth


People also ask

Can you use an alias in a bash script?

BASH Alias is a shortcut to run commands using some mapping. It is a way to create some shortcut commands for repetitive and multiple tasks. We create an alias in a BASH environment in specified files. We can also incorporate BASH functions, variables, etc to make the Alias more programmatic and flexible.

How can add a git URL as an alias?

Add git alias The simplest way to add a git alias is by running a command to add the alias to the git global configuration file. For example, running the command git config --global alias. hist "log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short" will add the alias git hist .

Where do I put git alias?

Your git aliases are often stored per your user's configuration at ~/. gitconfig . You can also manually set aliases using, for example, the command git config alias. s 'status -s' .

How do I edit a git alias?

Start a new branch. gitconfig file and add each alias under [alias], like so: Additionally, you can have repo-specific aliases. Just edit . git/config in the repo where you want to add the alias, and follow the same syntax.


2 Answers

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

This is redundant in one way. If you are going to add 'diffall.sh' into your $PATH anyway, why not save it as 'git-diffall', and save yourself from declaring an alias. Yes, "git diffall" will run it.

like image 72
u0b34a0f6ae Avatar answered Sep 21 '22 12:09

u0b34a0f6ae


To run commands inside of a git alias, and in particular to pass arguments to those commands, you will likely have to create a temporary function which you then immediately invoke:

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

In this example, the function is probably what you need (and is also more flexible as to what you can do in a single "statement"); and you can probably tell that for both options, the remaining args to the git command are simply passed as args to the alias, regardless if it's "echo" or "f"; invoking the function simply consumes the args, ignoring what's not explicitly used:

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

Another example (lists all aliases, based on matching pattern) (note: you can keep reusing the same function name "f()" throughout the .gitconfig):

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

The first returns the alias for just "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 based on shell; I'm using this with bash on Linux, Unix & Cygwin (Windows).)

like image 33
michael Avatar answered Sep 23 '22 12:09

michael