Git aliases give frequently used commands shorter names. You can use them for a single command or combine several commands into a shortcut.
Table of Contents
- 1. What are Git Aliases?
- 2. Setting Up Aliases
- 3. Useful Git Aliases
- 4. Advanced Aliases
- 5. Best Practices
- 6. Conclusion
1. What are Git Aliases?
Git aliases are custom shortcuts that you can create to replace longer Git commands. They allow you to save time by typing less while performing common Git operations. Aliases can be as simple as shortening a command or as complex as combining multiple commands into a single alias.
2. Setting Up Aliases
There are two main ways to set up Git aliases: temporary and permanent.
2.1 Temporary Aliases
Temporary aliases are created using the git config command with the --global flag. These aliases are stored in your global Git configuration file and are available across all your repositories.
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commitWith these aliases set up, you can now use git st instead of git status, git co instead of git checkout, and so on.
2.2 Permanent Aliases
For more permanent aliases, you can edit your Git configuration file directly. This file is typically located at ~/.gitconfig on Unix-based systems or C:\Users\YourUsername\.gitconfig on Windows.
[alias]
st = status
co = checkout
br = branch
ci = commit
unstage = reset HEAD --
last = log -1 HEAD
visual = !gitkThe ! at the beginning of the visual alias indicates that it's an external command rather than a Git subcommand.
3. Useful Git Aliases
The following aliases cover status, commits, branches, and logs.
3.1 Status Aliases
git config --global alias.st "status -sb"
git config --global alias.ll "log --stat --abbrev-commit"The st alias provides a more concise status output with branch information, while the ll alias shows a detailed log with statistics for each commit.
3.2 Commit Aliases
git config --global alias.amend "commit --amend --no-edit"
git config --global alias.cm "commit -m"
git config --global alias.cam "commit -am"These aliases make it easier to amend commits, create commits with messages, and add and commit changes in one step.
3.3 Branch Aliases
git config --global alias.b "branch -v"
git config --global alias.new "checkout -b"
git config --global alias.delete "branch -d"3.4 Log Aliases
git config --global alias.lg "log --color --graph \
--pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' \
--abbrev-commit"4. Advanced Aliases
Beyond simple command shortcuts, Git aliases can be used to create more complex workflows and custom scripts.
4.1 Custom Scripts
git config --global alias.cleanup \
"!git branch --merged | grep -v '\\*\\|master\\|main' | xargs -n 1 git branch -d"
git config --global alias.uncommit "reset --soft HEAD~1"The cleanup alias deletes all merged branches except master/main, while the uncommit alias undoes the last commit while keeping the changes staged.
4.2 External Commands
git config --global alias.visual "!gitk"
git config --global alias.gui "!git gui"
git config --global alias.difftool "!git difftool"5. Best Practices
- Keep it Simple: Create aliases that are easy to remember and type. Avoid overly complex aliases that might confuse you or others.
- Be Consistent: Use consistent naming conventions for your aliases. For example, use
stfor status,cofor checkout, etc. - Document Your Aliases: Keep a list of your aliases and their purposes, especially if you create complex ones.
- Share with Your Team: If you're working in a team, consider sharing useful aliases with your colleagues to improve everyone's productivity.
- Regularly Review and Update: As your workflow evolves, review and update your aliases to ensure they remain useful and relevant.
6. Conclusion
Git aliases reduce the typing needed for repeated commands and workflows.
Start with basic aliases, then add more complex ones as you find commands you repeat.
Choose names you can remember and shortcuts you will use.
Test Your Understanding
Question 1 of 5 · Score: 0/0 correct
Your first answer counts toward your score.
