DEV Community

Cover image for Creating Git Aliases
Ambrose Otundo
Ambrose Otundo

Posted on

Creating Git Aliases

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

  1. 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

  2. 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 git push:

git config --global alias.p 'push'
Enter fullscreen mode Exit fullscreen mode

Make sure you start each alias initilization with git config --global

  1. To check your git aliases simply type:
    git config --global -l

  2. The above command will show this:

alias.p=push
Enter fullscreen mode Exit fullscreen mode
  1. To use the just created alias just type:
git p
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

If you use this alias on a clean branch, your output looks like this:

$  git st
## main
Enter fullscreen mode Exit fullscreen mode

Using it on a branch with changed and untracked files produces this output:


$ git st
## main
 M test2
?? test3
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

Using this alias provides a short list of all commits:

$ git ll
33559c5 (HEAD -> main) Another commit
17646c1 test1
Enter fullscreen mode Exit fullscreen mode

3. Git last commit
This shows details about the most recent commit you made.

$ git config --global alias.last 'log -1 HEAD --stat'
Enter fullscreen mode Exit fullscreen mode

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(+)
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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(+)
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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 vimdiffand use the -y parameter to skip the confirmation prompt:

$ git config --global alias.dv 'difftool -t vimdiff -y'
Enter fullscreen mode Exit fullscreen mode

Use this alias to display file1 differences between two commits:

$ git dv 33559c5 ca1494d file1
vim-diff results
(hello there)
Enter fullscreen mode Exit fullscreen mode

7. Git config list
The gl alias makes it easier to list all user configurations:

$ git config --global alias.gl 'config --global -l'
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

There you go. You can also check on more commands in the GIT documentation and make cool aliases.

Top comments (0)