How To Create Custom Git Commands With Aliases
- LN Webworks
- Jul 10, 2024
- 3 min read

In the world of development, efficiency is key. As developers, we constantly seek ways to streamline our workflows and save precious time. One powerful method to achieve this is by creating custom Git commands with aliases. By doing so, we can reduce the need to repeatedly type out long commands and instead use concise, easy-to-remember shortcuts. In this blog post, we at LN Webworks will guide you through the process of setting up Git aliases to enhance your productivity.
Why Use Git Aliases?
Git aliases allow you to create shortcuts for commonly used commands, making your workflow faster and more efficient. Instead of typing out lengthy commands every time, you can use a short alias to achieve the same result. This not only saves time but also reduces the likelihood of making typos or errors.
Creating Git Aliases Using git config
The most straightforward way to create Git aliases is by using the git config command. This method allows you to set aliases globally, meaning they will be available across all your Git repositories.
Here's the basic syntax for creating a Git alias:
git config --global alias.<alias-name> "<git-command>"
Replace <alias-name> with your desired shortcut.
Replace <git-command> with the full Git command you want to alias.
Examples:
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.st status
git config --global alias.cm "commit -m"
With these aliases, you can now use git co instead of git checkout, git br instead of git branch, git st instead of git status, and git cm instead of git commit -m.
Editing the .gitconfig File
Another way to create Git aliases is by directly editing the .gitconfig file. This file is typically located in your home directory (~/.gitconfig). You can open this file in any text editor and add your aliases under the [alias] section.
Here's an example of how to set up aliases in the .gitconfig file:
[alias]
co = checkout
br = branch
st = status
cm = commit -m
lg = log --oneline --graph --decorate --all
This method offers the same functionality as using git config but allows you to see and manage all your aliases in one place.
Using Your Custom Git Aliases
Once you have created your aliases, using them is simple. Just type the alias instead of the full command. Here are some examples:
git co main # Equivalent to "git checkout main"
git st # Equivalent to "git status"
git cm "Initial commit" # Equivalent to "git commit -m 'Initial commit'"
Creating Complex Aliases
Git aliases are not limited to simple commands; you can also create complex aliases with multiple options and arguments. For instance, you might want a more detailed log view:
git config --global alias.lga "log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all"
This alias generates a visually enhanced log with a graph, abbreviated commit hashes, relative dates, author names, and branch/tag information.
Listing Your Aliases
To view all your configured aliases, you can use the following command:
git config --get-regexp alias
This command will display all the aliases you have set up, allowing you to keep track of them easily.
Conclusion
Creating custom Git commands with aliases is a powerful way to streamline your development workflow and save time. Whether you prefer using the git config command or editing the .gitconfig file directly, setting up aliases can significantly enhance your productivity. Start by creating aliases for your most frequently used commands and explore more complex setups as you become comfortable. At LN Webworks, we believe that every small efficiency gain adds up, helping you to focus more on coding and less on repetitive tasks. Happy coding!
Comments