image-blog-vcs-git-aliases
January 3, 2020

Become a Git Pro With Git Alias

Git at Scale
Version Control

Git alias is a shortcut. It enables developers to execute git commands faster. This can help you conquer roadblocks that can slow down newbies.

Back to top

So, What Is a Git Alias?

Git alias shortens commonly used commands allowing developers to do more in less keystrokes. Teams can use them to create a shorthand for longer commands that can include arguments in the process. It is similar to a bash (or other shell) alias because it defines a piece of text as a key. 

Using Git aliases can increase your team's velocity. It is especially useful for frequently used commands, for example git branch. Creating an alias, like git br, can expedite workflows.

Back to top

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.

Back to top

How to Use Git Alias

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.

Back to top

Limitations for Git Alias

Although Git aliases help, there are limitations:

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

Git Alias 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.

Back to top

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.

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.

See for yourself how Perforce Streams in Helix Core can improve your workflows. Try it out for free for up to 5 users and 20 workspaces.

Try Perforce Streams In Helix Core

 

Related Content:

Back to top