Sometimes you have gone through the hassle of writing the whole git commands in the git CLI. Let me now tell you that there is way to make that work easier. What is an alias? An alias is a shortcut that references a command.Aliases are mostly used to replace long commands, improving efficiency and avoiding potential spelling errors.
How to create an alias
Open your command line that can initialize a git cli. You can do that by using either Windows terminal, git bash, CMD and writing
git init
To define a Git alias, use the git config command with the alias and the command you want to substitute. For example, to create the alias
p
for gitpush
:
git config --global alias.p 'push'
Make sure you start each alias initilization with git config --global
To check your git aliases simply type:
git config --global -l
The above command will show this:
alias.p=push
- To use the just created alias just type:
git p
Popular Git aliases
1. Git status
Git command line users often use the status command to see changed or untracked files. By default, this command provides verbose output with many lines, which you may not want or need. You can use a single alias to address both of these components: Define the alias st
to shorten the command with the option -sb
to output a less verbose status with branch information:
$ git config --global alias.st 'status -sb'
If you use this alias on a clean branch, your output looks like this:
$ git st
## main
Using it on a branch with changed and untracked files produces this output:
$ git st
## main
M test2
?? test3
2. Git log --oneline
Create an alias to display your commits as single lines for more compact output:
$ git config --global alias.ll 'log --oneline'
Using this alias provides a short list of all commits:
$ git ll
33559c5 (HEAD -> main) Another commit
17646c1 test1
3. Git last commit
This shows details about the most recent commit you made.
$ git config --global alias.last 'log -1 HEAD --stat'
Use it to see the last commit:
$ git last
commit f3dddcbaabb928f84f45131ea5be88dcf0692783 (HEAD -> branch1)
Author: ambrose <ambrose@hello.com>
Date: Sun Jun 3 00:12:22 2022 +0000
Commit to branch1
test2 | 1 +
test3 | 0
2 files changed, 1 insertion(+)
4. Git commit
You use git commit a lot when you're making changes to a Git repository. Make the git commit -m
command more efficient with the cm
alias:
$ git config --global alias.cm 'commit -m'
Because Git aliases expand commands, you can provide additional parameters during their execution:
$ git cm "Just a commit message"
[branch1 0baa729] Just a commit message
2 file changed, 3 insertions(+)
5. Git remote
The git remote -v command lists all configured remote repositories. Shorten it with the alias rv
:
$ git config --global alias.rv 'remote -v'
6. Git diff
The git diff command displays differences between files in different commits or between a commit and the working tree. Simplify it with the d
alias:
$ git config --global alias.d 'diff'
The standard git diff command works fine for small changes. But for more complex ones, an external tool such as vimdiff
makes it more useful. Create the alias dv to display diffs using vimdiff
and use the -y
parameter to skip the confirmation prompt:
$ git config --global alias.dv 'difftool -t vimdiff -y'
Use this alias to display file1 differences between two commits:
$ git dv 33559c5 ca1494d file1
vim-diff results
(hello there)
7. Git config list
The gl alias makes it easier to list all user configurations:
$ git config --global alias.gl 'config --global -l'
Now you can see all defined aliases (and other configuration options):
$ git gl
user.name=ricardo
user.email=ricardo@example.com
alias.p=push
alias.st=status -sb
alias.ll=log --oneline
alias.last=log -1 HEAD --stat
alias.cm=commit -m
alias.rv=remote -v
alias.d=diff
alias.dv=difftool -t vimdiff -y
alias.gl=config --global -l
alias.se=!git rev-list --all | xargs git grep -F
There you go. You can also check on more commands in the GIT documentation and make cool aliases.
Top comments (0)