Git Alias with Arguments

Posted by Mike Barkas on Thursday February 01, 2018

Git Commands with Parameters

It is common to create aliases to command-line programs you use often when working in a terminal. The Bash aliases help reduce typing and trying to remember all the program options that are available at the command-line.

In this article I try to describe a few different methods I use to make specific aliases for Git commands. Some of the Git aliases are a simple shortcut to the command to reduce redundant typing. There are a couple different ways to implement these simple shortcuts and I give examples below. There are also more complex Git commands that you may want to run with an alias. These commands become complex when needing to call a function and possibly including parameters or running multiple commands.

There are many opinions of what makes a useful alias of the shortest or most clever shortcut. You will need to decide what is best for your workflow and how you remember them.

Git Config

The most common way to add a simple Git alias to reduce typing is in your .gitconfig file. This file should be located in your home directory on Unix and Linux type systems.

    co = checkout
    l = log --oneline --graph

The above examples will create the co shortcut for checkout. This will save you from having to type out the word "checkout", and shorten it to "co". The shortcut l will result in the same as typing out log --oneline --graph, and that will save a lot of typing.

The aliases created in the config file above will apply your shortcut in the Git program. You will still have to initiate the Git program and append your alias. Example git l

Bash Alias for Git Commands

I am using the Bash shell in my examples. The same concept can apply to different shell programs and the syntax may be a little different.

As opposed to using the Git configuration file for my aliases, I usually make my common Git aliases in the same way as a Bash alias. In your shell configuration files you can define aliases.

This example defines the alias in the home directory ~/.bash_profile

alias gco="git checkout"
alias gl="git log --oneline --graph"

Compare this type of alias with the Git config example above. The difference is the program execution of "git" is included in the alias. This means you will only have to type gl to execute the full command of git log --oneline --graph. Note the syntax and spacing in Bash config files.

Git Aliases with Functions

If you need to do more advanced things or pipe multiple Git commands together, you can start a shell process to run more complex logic by using functions. You can think of these functions as anonymous functions that get defined and then immediately invoked each time you call your alias. Starting your alias definition with a ! will allow for your shell function definitions.

These examples are done in your .gitconfig file and should be found in your home directory.

  example1 = "!f() { echo "Listing branches"; git branch -vva; }; f"
  example2 = "!f() { echo "value passed as parameter is ${1}"; }; f"

In the examples above, the alias starts with ! allowing you to run shell commands then the function is defined with f() { . . . };. After the definition, the function is immediately called with the last f. You can substitute "f" with whatever you prefer. The naming convention is "f" meaning "function".

Example1 above demonstrates using a Bash command (echo) and then another statement using a Git command.

Example2 demonstrates passing a parameter to your function from the command-line in the standard way Bash functions take parameters. For example using example2 like example2 testvalue would print out value passed as a parameter is testvalue.

The examples above are not very exciting, but are simple to demonstrate the structure and how they are used. You can use your imagination how powerful this can be in your daily workflow.

Git Alias in Your Path

Git provides a mechanism for parsing and finding files in your $PATH that may contain commands or functions. It is common to have a bin directory in your home directory on Linux and Unix types systems. This example is going to assume you have a bin directory in your home directory and it is in your $PATH.

Git will use files found with the naming convention of git-example and run the code in these files using the alias example. Git removes the git- part. These files also need to have the executable bit set.

Example git file and directory: ~/bin/git-example

This example file will contain your shell commands and Git commands and you can pass parameters to them from the command-line like any other shell program.

Example file contents:


echo "This is the value passed as a parameter: ${1}"

Then you can run this alias script with git example testvalue and it will print out This is the value passed as a parameter: testvalue.

All the normal shell parameters rules apply like $1, $2, $@ etc. You can create very complex functions, scripts, or any combination of Git commands as well.

In my examples I am using Linux/Unix type operating systems. I also use Bash as an example and may use Bash and shell interchangeably just for reference.

I try to limit how many aliases I have in general. I also consider length and complexity and how often I may use them. If I don't use an alias commonly in my daily workflow, I will consider not having it. I also may have specific aliases I used to experiment and learn with. I also think about how dependent I become of them because they are not always available on remote servers.


Find Git commands you use often in your daily workflow. Make simple Git aliases to remove repetitive typing of commands you use. Experiment and build functions combining shell commands and Git commands that may take parameters from the command-line to learn with. You may even write a program to update, pull, push, or create branches.