When working with Git CLI we usually type lots of verbose and boilerplate commands. However, Git has alias support that might be handy and save a lot of “typing” energy as well as increase your productivity.
Getting Started
What is Git Alias
Basically Git Aliases gives the ability to save some command with different name/nickname.
How to add Git Alias
There are 2 ways of adding new Git Alias:
-
Adding to the git config file:
$ git config --global --edit
... [alias] co = commit
-
Using Git CLI:
git config --global alias.co checkout
Note: This is just automated version of “Option 1” for adding single git alias.
How to use Git Alias
- So using the above example, instead of typing
git checkout
, it’s now possible to use justgit co
- Any additional parameters basically passed to the underlying command. Example, if you want to create and switch to a branch, instead of
git checkout -b my-branch
, you can usegit co -b my-branch
Conclusion
Besides using Git CLI and aliases I would also suggest learning/mastering other Git tooling tips and tricks too in order to maximise your productivity and minimise repetitive work. Like IDE’s builtin Git support or separate Git UIs like Github for Desktop or Sourcetree etc.
Bonus Tip. List of useful aliases
# ...
[aliases]
# basic commands
co = checkout
br = branch
st = status
ci = commit
# quick --fixup commit
# ex: git fixup [hash]
fixup = commit --fixup
# quick --amend commit
# ex: git amend [hash]
amend = commit --amend
# quick rebase on top of latest remote master
# ex: git rom
rom = rebase origin/master
# safe force push if no one else pushed yet
# ex: git pf
# note: suggestion to use whenever "git rom"
pf = push --force-with-lease
# safe switch to another branch
# ex: "git switch another-branch"
# note: it will stage all changes and save to stash before checkout
switch = !git add . && git stash save WIP && git co
# compact commit history flat view
# ex: "git ls"
# ex: "git ls -5" - show last 5 commits
# see: https://git-scm.com/docs/pretty-formats for more details
ls = log --pretty=format:'%C(yellow)%h %C(green) %cr%C(red) %d%C(reset) %s%C(bold blue) <%an>%C(reset)' --abbrev-commit
# compact commit history graph view
# ex: "git lg"
# see: https://git-scm.com/docs/pretty-formats for more details
lg = log --pretty=format:'%C(yellow)%h %C(green) %cr%C(red) %d%C(reset) %s%C(bold blue) <%an>%C(reset)' --abbrev-commit --graph
# compact statistics for certain commit
# ex: "git stat [hash]"
stat = show --stat --oneline