Skip to content

Git Alias Tutorial

Overview

Git aliases allow you to create shortcuts for longer commands, making your workflow more efficient. This document will cover the basics of creating and using Git aliases with examples.

Table of Contents

  1. Creating a Git Alias
  2. Common Git Aliases
  3. Global vs. Local Aliases
  4. Managing Aliases
  5. Useful Tips

Creating a Git Alias

To create a Git alias, use the git config command with the alias prefix followed by the desired alias and the command it represents.

Example

git config --global alias.co checkout

This command creates a global alias co for the checkout command.

Common Git Aliases

Status

Alias for the status command:

git config --global alias.st status

Example

git st

This will run git status.

Commit

Alias for the commit -m command:

git config --global alias.ci 'commit -m'

Example

git ci "Initial commit"

This will run git commit -m "Initial commit".

Log

Alias for a pretty log format:

git config --global alias.lg "log --oneline --graph --decorate --all"

Example

git lg

This will run git log --oneline --graph --decorate --all.

Checkout

Alias for the checkout command:

git config --global alias.co checkout

Example

git co main

This will run git checkout main.

Branch

Alias for the branch command:

git config --global alias.br branch

Example

git br

This will run git branch.

Global vs. Local Aliases

Aliases can be configured globally (for all repositories) or locally (for a specific repository).

Global Alias

To create a global alias, use the --global flag:

git config --global alias.ci 'commit -m'

Local Alias

To create a local alias, omit the --global flag and run the command within the repository:

git config alias.ci 'commit -m'

Managing Aliases

Listing All Aliases

To list all configured aliases, use:

git config --get-regexp alias

Example

git config --get-regexp alias

This will list all aliases and their corresponding commands.

Removing an Alias

To remove an alias, use the --unset flag:

git config --global --unset alias.ci

Example

git config --global --unset alias.ci

This will remove the global alias ci.

Useful Tips

  • Complex Commands: You can create aliases for complex commands by quoting the entire command.

    git config --global alias.amend 'commit --amend --no-edit'
    
  • Combining Aliases: Aliases can call other aliases, allowing for modular and reusable configurations.

  • Shell Shortcuts: You can combine Git aliases with shell aliases or functions for even more powerful shortcuts.

Summary

Git aliases are a powerful way to streamline your workflow by creating shortcuts for frequently used commands. By understanding how to create, manage, and use aliases, you can make your Git usage more efficient and enjoyable.