Boost productivity while programming with Git aliases
Git aliases are abbreviated versions of Git commands that you can use instead of the full command name, which can boost your productivity as a programmer.
Git is still one of the most effective tools for keeping track of all the code you create and would want to share with the programming community. It allows you to partition your projects into several branches. It is quite a complex tool, so if using this tool is proving difficult for you, you are not alone.
Since you must know specific codes and construction, working with Git is daunting and may reduce your overall productivity.
Nevertheless, what if there were a way to reduce the exact steps you had to follow when dealing with Git? What if you had the option to configure it with your commands so that it only used structures you could recall? This challenge is where the Git alias comes into play.
Read on to learn about Git and how to create custom aliases.
Watch the lesson
What are Git aliases?
Git aliases are shortened versions of commands that are long, repetitive, and typically difficult to memorize.
Aliases assist you in installing a more user-friendly command you can call to carry out a particular Git command. Similar to other configuration parameters, creating aliases can be done on a local or global scope. And you can use the git config
command and the Git configuration files to create aliases.
For instance, you could write git com
instead of git checkout master
or combine git checkout master
, git add .
, and git commit -m
into a single command.
Why are Git aliases useful?
Using Git aliases has several benefits. Let's examine some further usefulness of Git aliases.
Shortening commands: You can create short, easy-to-remember aliases for long and complex commands. For example, the alias
lv
is a shortcut forgit log --pretty=oneline --abbrev-commit
Using more descriptive commands: An alias can make it easier to understand what your command does by providing additional detail about its function. This benefit can help junior developers get up to speed quicker, too.
Avoiding typos: Git aliases allow you to avoid typing errors and typos by letting you enter less text at the command line.
In a nutshell, the sole purpose of aliases is to make frequently used commands more efficient by shortening their titles! Let's look at how to create custom Git aliases to demonstrate how simple it is.
How to create Git aliases
There are over 150 commands in Git, ranging from the well-known git-status
to the mysterious git-get-tar-commit-id
and other complex commands.
There is a dizzying amount to remember because each command supports a unique set of arguments. So the best way to use these commands without stress is to create an alias.
Before generating Git aliases, it's crucial to understand that they can be kept globally or in local repositories. A local alias will only apply to one repository, while a global Git alias extends to other repositories.
You can use the git config
command and configuration files to create aliases. A git config
command is an easy-to-use tool for quickly creating aliases and a support tool for editing both the global and local Git config files.
Let us create an alias using VS Code.
With your terminal open, enter
git config --global alias.st status
and click enterOn the following line, enter
git st
to test the aliases you created
Remember that the —global
flag specifies that an alias is added to the global operating system configuration file for Git. On Linux platforms, the global configuration file is in the user's home directory with the name /.gitconfig
.
You can create aliases for checkout, branches, and other commands.
$ git config --global alias.br branch
git config --global alias.co checkout
$ git config --global alias.ci commit
To get a list of the aliases you have created for your project globally, enter git config --global -l
[alias]st = statusbr = branchco = checkoutci = commit
This list demonstrates that the source commands are now the same as the aliases.
Similar to editing other configuration parameters, you can add new alias commands to your /.gitconfig file
.
Let's say this is an output from this command, git config;
[alias]st = statusbr = branchco = checkoutci = commit
You can add other aliases by entering the following:
unstage = 'reset HEAD --'cmm = 'commit -m'cb = 'checkout -b'sc = 'switch -c'
The following output after adding the above Git commands will be:
[alias]st = statusbr = branchco = checkoutci = commitunstage = reset HEAD --cmm = commit -mcb = checkout -bsc = 'switch -c'
A few examples of helpful Git aliases
You can increase your productivity when working with Git by using one of the many Git aliases. Here are a few examples of helpful Git aliases.
Alias to display commits as single lines
$ git config --global alias.ll 'log --oneline'
Alias to list all configured remote repositories
$ git config --global alias.rv 'remote -v'
Alias to list all user configurations
$ git config --global alias.gl 'config --global -l'
Alias for the "git log" command in pretty format
$ git config --global alias.ls 'log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate'
Alias to see all branches in the graph
$ git config --global alias.graph 'log -all -decorate -oneline -graph'
Alias to remove a branch
$ git config --global alias.del 'branch -D'
Alias to clean all changes to avoid committing to the repository
$ git config --global alias.res '!git reset --hard'
Note: You can follow the procedure shown in this post to create an alias for codes you use for your projects, as there are many of them.
Conclusion
In short, Git aliases are a great way to improve your productivity. They help you work faster by reducing the number of keystrokes required to perform common tasks and make it easier for new team members to get up-to-speed with Git commands.
Git aliases facilitate quicker and more efficient development, enhancing team or individual Git workflow. Regardless of the strategy when creating aliases, they enhance your overall Git experience.