Sometimes you run commands that are a little complicated or just too long to write. Git has support for command aliases that are very easy to set up.

Some people like the short form of many VCS commands: co instead of checkout, ci instead of commit, di instead of diff, etc. On the face of it, git doesn’t support these. Instead it offers something more powerful: configurable aliases.

To make git ci into git commit it’s a simple git config --global alias.ci commit.

If you want to call external programs, use an exclamation mark (!). If you always find you’re running gitk --left-right HEAD...MERGE_HEAD to visually compare merge conflicts, execute git config --global alias.gitkconflict '!gitk --left-right HEAD...MERGE_HEAD'.

“What if I want to execute something that needs arguments?” you ask. If you just want the arguments appended to the end of the command, then you don’t have to do anything. The git ci example above works fine if you want to pass certain files to it. If you want more complicated arguments you can specify them with $<variable> notation.

A more complicated example that uses external commands and arguments comes from the git alias wiki page: git config alias.funcdiff '!sh -c "git show \"\$0:\$2\" | sed -n \"/^[^ \t].*\$3(/,/^}/p\" > .tmp1 && git show \"\$1:\$2\" | sed -n \"/^[^ \t].*\$3(/,/^}/p\" > .tmp2 && git diff .tmp1 .tmp2"'

When you call git funcdiff <old-rev> <new-rev> <path> <function> it will create two files (.tmp1 and .tmp2) that contain only <function> from <path> from the two different revisions and run git diff on them.

References:

Advertisements