git-alias
Create shorthand commands for Git operations
TLDR
List all aliases
Create a new alias
Search for an existing alias
SYNOPSIS
Git aliases are a feature managed primarily via the git config command. Below are common operations to manage aliases:
git config [--global | --local | --system] alias.<name> <command>
Define or update an alias named <name> to execute <command>.
git config [--global | --local | --system] --unset alias.<name>
Remove a previously defined alias.
git config [--global | --local | --system] --get-regexp alias.
List all defined aliases in the specified or default scope.
PARAMETERS
alias.<name>
The configuration key specifying the alias. <name> is the desired shortcut name you'll use (e.g., co for checkout).
<command>
The Git command(s) or shell command string to be aliased. If the string starts with !, it will be executed as a shell command (e.g., '!git status --short').
--global
Applies the configuration to the user's global Git configuration file (typically ~/.gitconfig), affecting all Git repositories for that user.
--local
Applies the configuration only to the current repository's Git configuration file (.git/config). This is the default scope if no scope is specified.
--system
Applies the configuration to the system-wide Git configuration file (e.g., /etc/gitconfig), affecting all users and repositories on the system. Requires administrative privileges.
--unset
Used with alias.<name> to remove the specified alias from the configuration.
--get-regexp alias.
Lists all configuration entries that match the regular expression 'alias.', effectively displaying all defined Git aliases and their corresponding commands.
DESCRIPTION
Git aliases provide a powerful way to create custom shortcuts for longer Git commands or sequences of commands. They are defined using the git config command and stored in Git's configuration files (e.g., ~/.gitconfig for global aliases, or .git/config for local repository aliases).
When you define an alias like git config --global alias.co checkout, you can then simply type git co instead of git checkout. Aliases can be simple command substitutions or execute complex shell commands by prefixing the aliased command with an exclamation mark (!). For example, git config --global alias.st 'status -sb' allows git st to show a short, branch-aware status. A shell alias like git config --global alias.hist '!git log --pretty=format:"%h %ad | %s%d [%an]" --date=short' executes a custom log format.
Using aliases significantly improves workflow efficiency, reduces typing, and allows users to personalize their Git experience to match their preferences and common operations. They are resolved before Git attempts to find a built-in command, making them highly effective for overriding or shortening existing commands.
CAVEATS
Resolution Order: Git aliases are resolved before built-in Git commands. This means if you define an alias named 'status', it will override the default git status command when you type git status.
Shell Commands: Aliases prefixed with ! execute as shell commands, with their current working directory set to the top-level directory of the Git repository. Be cautious with complex shell scripts and ensure proper quoting to prevent unexpected behavior.
Security Considerations: Running arbitrary shell commands via aliases can pose security risks if configurations are shared or if you clone untrusted repositories with potentially malicious aliases. Always review aliases from unknown sources.
GIT ALIASES VS. SHELL ALIASES
It's important to distinguish between Git aliases and shell aliases. A shell alias (e.g., alias gco='git checkout' in Bash or Zsh) is defined in your shell's configuration and expands before the command is even passed to Git. It's purely a shell-level shortcut. A Git alias, conversely, is processed by Git itself. This means Git aliases are portable across different shells and systems (as long as Git is configured), and they can intelligently interact with Git's internal state (especially when used with the ! prefix for shell commands within a Git context). Git aliases are generally preferred for Git-specific shortcuts.
PRACTICAL EXAMPLES OF GIT ALIASES
Here are some common and highly useful Git aliases you can add to your global configuration (using git config --global):
git config --global alias.co checkout (shorthand for checkout)
git config --global alias.br branch (shorthand for branch)
git config --global alias.ci commit (shorthand for commit)
git config --global alias.st 'status -sb' (short status with branch info)
git config --global alias.unstage 'reset HEAD --' (unstage files)
git config --global alias.last 'log -1 HEAD' (show last commit)
git config --global alias.visual '!gitk' (launches Gitk, if installed)
git config --global alias.alias '!git config --get-regexp alias.' (a self-referential alias to list all your Git aliases!)
HISTORY
The concept of Git aliases has been a fundamental part of Git from its early development, providing a flexible mechanism for users to customize their command-line interface. While there isn't a dedicated 'git-alias' command executable, the functionality has always been integral to personalizing and streamlining Git workflows via git config, reflecting Git's design philosophy of being highly configurable. This feature has evolved alongside Git's configuration system, allowing for increasing levels of customization and powerful scripting capabilities.
SEE ALSO
git-config(1), git(1)