Git aliases are shortcuts that you can define to replace long Git commands with shorter ones, making your workflow faster and more efficient. They work by allowing you to customize your Git command set-up, making frequent commands easier to execute.
You create these aliases using the git config
command, which modifies Git's configuration files. These files are where user-specific and project-specific settings are stored. Aliases can be defined locally for a specific project or globally for all projects owned by specific users.
How Git aliases work
When you enter a Git command (any terminal command beginning with git
), Git first checks if it's a built-in command. If it isn't, Git then looks up your aliases to see if there's a match. If an alias exists, Git replaces the alias with its corresponding command as defined in the configuration file.
Types of git configurations for aliases
- Local aliases: These are specific to a single repository. They are stored in the
.git/config
file within the repository. - Global aliases: These apply to all repositories on your system and are stored in your global Git configuration file, typically found at
~/.gitconfig
.
Setting up git aliases
To create an alias, you can use the git config
command, specifying whether you want the alias to be local or global. Here’s the syntax:
git config [--global] alias.[alias_name] "[command]"
Replace [alias_name]
with the name of your new alias, and [command]
with the Git command you want the alias to execute.
Examples of git aliases
Alias for checking the status of your repo:
Terminalgit config --global alias.st "status"This alias allows you to type
git st
instead ofgit status
to see the current status of your Git repository and working directory.Alias for checking the branch:
Terminalgit config --global alias.br "branch"Now,
git br
will show you the list of branches, just likegit branch
.Alias for viewing logs:
Terminalgit config --global alias.logs "log --oneline --graph --decorate"This creates a more readable and visually appealing log output with
git logs
.
Listing all defined Git aliases
To see all the aliases you currently have configured, you can list them by looking at the Git configuration:
git config --get-regexp alias
This command fetches all the settings that begin with alias
, effectively listing all your aliases.
Common Git aliases
To get you started, here are some common Git aliases you may want to use:
# Defines 'st' as an alias for 'status', which shows the working directory status.alias.st status# Defines 'di' as an alias for 'diff', which displays differences between Git objects.alias.di diff# Defines 'co' as an alias for 'checkout', which switches branches or restores working tree files.alias.co checkout# Defines 'ci' as an alias for 'commit', which records changes to the repository.alias.ci commit# Defines 'cp' as an alias for 'cherry-pick', which applies changes introduced by some existing commits.alias.cp cherry-pick# Defines 'br' as an alias for 'branch', which lets you create, list, or delete branches.alias.br branch# Defines 'sta' as an alias for 'stash', which temporarily stores modified, tracked files.alias.sta stash# Defines 'llog' as an alias for 'log' with local date format, to show commit logs.alias.llog log --date=local# Defines 'flog' as an alias for a more detailed 'log' format including decorations.alias.flog log --pretty=fuller --decorate# Defines 'lg' as an alias for 'log' with a graph of commits, visually descriptive commit messages.alias.lg log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative# Defines 'lol' as an alias for a concise 'log' with a graph showing only one line per commit.alias.lol log --graph --decorate --oneline# Defines 'lola' as an alias for 'lol' but includes all branches in the log graph.alias.lola log --graph --decorate --oneline --all# Defines 'blog' as an alias to show the log from the local branch to the 'origin/main' branch, using left-right markers.alias.blog log origin/master... --left-right# Defines 'ds' as an alias for 'diff --staged', which shows changes between the staging area and the last commit.alias.ds diff --staged# Defines 'fixup' as an alias for creating a 'commit --fixup', which is useful for a later rebase that can automatically squash this commit with a previous one.alias.fixup commit --fixup# Defines 'squash' as an alias for creating a 'commit --squash', setting up changes to be squashed with a previous commit.alias.squash commit --squash# Defines 'amendit' as an alias for modifying the most recent commit with 'commit --amend' without changing the commit message.alias.amendit commit --amend --no-edit# Defines 'unstage' as an alias for 'reset HEAD', which unstages files.alias.unstage reset HEAD# Defines 'rum' as an alias for rebasing the current branch with the upstream branch of 'master'.alias.rum rebase master@{u}# Defines 'addall' as an alias for 'add -A', which stages all changes.alias.addall add -A
For more information on Git aliases, see the official Git documentation.