Git aliases are shortcuts for frequently used Git commands, saving you time and keystrokes. This page explains how to create, manage, and use custom Git aliases effectively.
Git aliases allow you to define custom shortcuts for long or complex Git commands. For example:
- Replace
git status
withgit st
. - Combine multiple Git commands into a single alias.
- Save Time: Shorten repetitive commands.
- Increase Efficiency: Simplify complex workflows with custom shortcuts.
- Personalize Git: Tailor Git to match your workflow preferences.
To create a Git alias, use the git config
command.
git config --global alias.<alias-name> "<command>"
-
Create an alias for
git status
:git config --global alias.st "status"
-
Use the alias:
git st
Shorten commonly used commands:
git config --global alias.co "checkout"
git config --global alias.br "branch"
git config --global alias.cm "commit"
Pass arguments to aliases:
git config --global alias.undo "reset HEAD~1"
Run the alias to undo the last commit:
git undo
Combine multiple commands:
git config --global alias.lg "log --oneline --graph --decorate --all"
Use git lg
for a visual commit history:
git lg
To list all configured aliases:
git config --get-regexp alias
Example output:
alias.st status
alias.co checkout
alias.cm commit
To delete an alias:
git config --global --unset alias.<alias-name>
Example:
git config --global --unset alias.st
Alias | Command | Usage |
---|---|---|
st |
status |
git st for git status . |
co |
checkout |
git co for git checkout . |
br |
branch |
git br for git branch . |
lg |
log --oneline --graph --decorate --all |
git lg for a compact commit history. |
last |
log -1 HEAD |
git last to view the last commit. |
undo |
reset HEAD~1 |
git undo to undo the last commit. |
save |
stash save -u |
git save to stash all changes. |
rbi |
rebase -i |
git rbi HEAD~n for interactive rebase. |
amend |
commit --amend --no-edit |
git amend to modify the last commit. |
Instead of running git config
commands, you can define aliases directly in the .gitconfig
file.
-
Open your global Git configuration file:
nano ~/.gitconfig
-
Add aliases under the
[alias]
section:[alias] st = status co = checkout br = branch lg = log --oneline --graph --decorate --all
-
Save and close the file.
- Keep Aliases Simple: Use aliases for commands you frequently type.
- Use Descriptive Names: Choose alias names that are intuitive and easy to remember.
- Document Custom Aliases: If sharing repositories, provide documentation for custom aliases in use.
git config --global alias.co "checkout"
git config --global alias.cm "commit -m"
git config --global alias.st "status"
- Check the status of your repository:
git st
- Commit changes with a message:
git cm "Add new feature"
git config --get-regexp alias
Git aliases are a simple yet powerful way to enhance productivity and streamline workflows. By customizing Git to fit your needs, you can focus more on development and less on repetitive commands.
Next Steps: Code Reviews