Monday, 2 October 2017

Setting up a Git alias with a shell function

This short article will show you how to search in the log of a Git repository with a shell function. This shell function can also receive a positional parameter to use in the shell function. First off, edit the .gitconfig file in your user's home folder. On Linux Mint for example, the folder should reside in the /home directory. You can use nano for example. (apt-get install nano)

.gitconfig : 

        searchlog = "!f() { git log --all --decorate --graph -i --grep \"$1\";  }; f"

Note the positonal parameter $1 here, we escape the quote also. The function inside the alias is a shell function.

To use this alias command, type for example:
git searchlog test
This will search the Git log for the parameter passed in (test) and include some flags to decorate the log displayed. Using Git aliases, we can do lenghty Git commands with shorter aliased commands.

No comments:

Post a Comment