An illustration of using Git aliases.
January 3, 2020

Become a Git Pro With Git Alias

Git at Scale
Version Control

Some of the most frequently used git commands can be described as… well, long. It’s one of the main roadblocks that can slow down newbies. To help teams code faster, start using git aliases.

So, What Is a Git Alias?

A git alias is a shortcut. It is similar to a bash (or other shell) alias because it defines a piece of text as a key. Git is able to recognize and expand the key. It is used by developers to create a shorthand for longer commands that can include arguments in the process.

Using git aliases allows you to accomplish more with less keystrokes. It is especially useful for frequently used commands, for example git branch. Creating an alias, like git br, can expediate workflows.

Why Use Git Aliases

Providing new developers with pre-defined .gitconfig file can include a variety of useful aliases to save them time, provide examples, and standardize commands across teams or the organization as a whole.

Git Aliases can also help smooth the transition from other version control systems, insofar as they can provide commands more familiar to users.

How to Use Git Aliases

It is important to note, there is no git alias command. You need to set them up first.

For example, consider you have a developer that has just started using Git. They have experience using Visual SourceSafe, therefore they are familiar with the history command. Setting up a Git alias provides them with the same history command, but a better output in Git.

[alias]
history = log --pretty=format:'%C(cyan)%h%Cred%d %Creset%s%Cgreen [%cn]' --decorate

NOTE: This git alias will work out of the box on any ANSI-aware shell. Windows users will still need an ANSI-aware alternative such as the open-source Ansicon or JP Software’s TakeCommand. Microsoft does not include support in the default command processor.

Using the Git Config File

To set up this alias, copy can define the history alias by putting in your .gitconfig file. You may also define aliases from the command line via the following pattern:

git config --global alias.key 'value'

 

Replace key with the text that should trigger the alias. It is important to note that some aliases, like the above history example, are easier to define by editing the configuration file due to argument quoting (and other) issues.

Limitations for Git Aliases

Although git aliases help, there are limitations:

  • Cannot have parameters.
  • Cannot execute multiple commands.
  • Cannot use | (pipes) or greps.

Git Aliases for Advanced Users

To set up more advanced git aliases, you need to use the “!”. The “!” allows you to open up the alias to include parameters, multiple commands, command line tools, and more. It works like a bash or zsh. Then you can define the function and call it. A basic template would include:

advanced_alias ="!f() { your advanced command }; f"

 

By wrapping the function and then calling it, you ensure that the commands and parameters defined do not escape the shell. Using this function, you can build aliases that allow you to clean up merges, simplify diffs across branches, and more.

Create Cleaner Workflows (Without Using Git Alias)

The point of using git aliases is to streamline developer’s workflows. But there is a better way. Instead of shaving off seconds with shorter commands, you can automate more with Perforce Streams –– branching in Helix Core.

Compare Git vs. Perforce >>

Streams offers built-in best practices that allow developers to innovate without worrying about where code goes. They can branch more, merge easier, and switch between codelines fast. Plus Perforce and Git commands are similar, allowing developers to adapt to a new version control system easier.

CHECK OUT OUR GIT TOOLS

Want to learn more? Explore Git best practices.