For heavy Git users, adding aliases for some common used Git commands brings big convenience especially when a command is too long to remember. Here you will see some useful alias commands. You can rename them according your personal habits and add your own alias gradually.
Add Git aliases
You may add alias with git config
one by one or just edit your ~/.gitconfig
.
Add an alias with git config
command.
# Add alias "st" for "status" command
$ git config --global alias.st status
# Add alias "last" for "log -1 HEAD" command
$ git config --global alias.last "log -1 HEAD"
~/.gitconfig
:
[alias]
st = status
co = checkout
last = log -1 HEAD
cm = commit --message
ca = commit --amend
cane = commit --amend --no-edit
lg = log --all --graph --oneline --decorate
The last alias above git lg
is very useful, it gives you a visual representation of the commit history in the terminal. See viewing git commit history for more information about the command.
$ git log --all --graph --oneline --decorate
* c3324ea (HEAD -> master, origin/master, origin/HEAD) feat: allow uplading file
| * d7fff0f (feat) up: hello.php
| * 3a2201f add: build
|/
| * 99b000f (clean-branch) add: a graph
| * b495f71 add: new.md
| * 625cdcb add: clean-branch.md
| * f0d56f9 (refs/stash) WIP on master: b1f092d update: hello.php
| |
|/ /
| * ef288eb index on master: b1f092d update: hello.php
|/
* b1f092d feat: update form
* 5439504 Revert "hello.php : add input tag"
* f82d809 readme.md: add new line2
* 9da2b81 Merge branch 'test'
|
| * 4f10b4a hello.php : add new line
* | 2a12f93 readme.md: adde new line
|/
* 5f49a56 update readme and hello
* cd6b3aa add hello.php
* a9d90e9 initial commit
If you like, you can make some commands very short.
[alias]
a = add
b = branch
c = commit
d = diff
f = fetch
g = grep
l = log
m = merge
o = checkout
p = pull
r = remote
s = status
How many keystrokes saved by using aliases
Below is a table tells how many keystrokes you will save for using some aliases.
Original command | Alias | Keystrokes saved |
---|---|---|
git commit –message | git cm | 14 |
git commit –amend | git ca | 12 |
git commit –amend –no-edit | git cane | 20 |
git log –all –graph –oneline –decorate | git lg | 35 |
… |
Gitalias repository
If you want to use aliases as many as possible, there is a GitHub repository gitalias which provides many Git alias commands with explaination that you can use as you like.
Below are some complex examples:
# log like - we like this summarization our key performance indicators. Also aliased as `log-like`.
ll = log --graph --topo-order --date=short --abbrev-commit --decorate --all --boundary --pretty=format:'%Cgreen%ad %Cred%h%Creset -%C(yellow)%d%Creset %s %Cblue[%cn]%Creset %Cblue%G?%Creset'
# log like long - we like this summarization our key performance indicators. Also aliased as `log-like-long`.
lll = log --graph --topo-order --date=iso8601-strict --no-abbrev-commit --abbrev=40 --decorate --all --boundary --pretty=format:'%Cgreen%ad %Cred%h%Creset -%C(yellow)%d%Creset %s %Cblue[%cn ]%Creset %Cblue%G?%Creset'