How to setup Git and Git aliases
After I switched from Subversion to Git (at least for my personal projects) since a while I finally took the time to improve my setup and create various aliases to achieve more using shorter commands.
This is a short reference of various configuration settings I use. Note: all of these settings are made global (which means they are used for all Git repositories of the current system user and are stored in ~/.gitconfig
). By removing the --global
option and firing up the commands in a Git repository only sets these settings for the current project (they’re then stored in .git/config
relative to the project root).
General configuration
Before using Git for the first time you definitely should configure your name and e-mail address. Both of them are part of every commit.
git config --global user.name "John Doe"
git config --global user.email "john.doe@example.com"
After that you can set some more general settings. A good thing is colored output in the terminal, which is activated by setting the color.ui
setting to true
. Because I want to work more with vim, I configure vim to be my Git text editor by changing the core.editor
setting. I also set the merge.tool
setting to use vimdiff
(even if I’m not yet experienced with it). Another setting is core.filemode
which I set to false
because I want Git to ignore file mode changes.
git config --global color.ui "true"
git config --global core.editor "vim"
git config --global merge.tool "vimdiff"
git config --global core.filemode false
Configuring aliases
When working with Git on the command line you quicky detect that you’re typing a few commands very often and that it’d be a lot more convenient if these commands would be shorter. Other commands have tons of parameters which are really hard to remember and impossible to type everytime you need them. The good thing is that Git allows you to configure aliases.
This is my current alias configuration:
git config --global alias.st "status"
git config --global alias.ss "status -s"
git config --global alias.aa '!git add -u && git add . && git status'
git config --global alias.ap "add -p"
git config --global alias.co "checkout"
git config --global alias.df "diff"
git config --global alias.lg "log --graph --pretty=format:'%C(bold red)%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold green)<%an>%Creset' --abbrev-commit --date=relative"
git config --global alias.cm "commit --verbose"
git config --global alias.ca "commit -a --verbose"
git config --global alias.am "commit --amend"
git config --global alias.ph "push"
git config --global alias.ft "fetch"
git config --global alias.pl "pull"
git config --global alias.pr "pull --rebase"
These aliases offers a shorter alternative for frequently used commands like “status”, “checkout”, “diff”, “push”, “fetch” and “pull” – instead of typing git status
you can now just use git st
.
A few more explanations: The alias aa
stages all tracked and untracked files and changes. By using the ap
alias you can use the patch mode to stage even parts of changes in files – it presents you all changes and lets you decide if you want to stage them. The alias lg
does a lot: it specifies the format of the log and adds some nice colors. Both cm
and ca
aliases add the --verbose
option which I saw first in the great screencast Play by Play: Aaron Patterson and Corey Haines. This allows you to review a full diff of the commit in your editor when writing the commit message. pr
is an alias for doing a pull
, but using rebase to integrate your changes.
Your ~/.gitconfig
As noted earlier, all these commands add values to your ~/.gitconfig. You can view and change them there, or just editing this file directly. This is how the file looks after applying all these settings:
[user]
name = John Doe
email = john.doe@example.com
[color]
ui = true
[core]
editor = vim
filemode = false
[merge]
tool = vimdiff
[alias]
st = status
ss = status -s
aa = !git add -u && git add . && git status
ap = add -p
co = checkout
df = diff
lg = log --graph --pretty=format:'%C(bold red)%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold green)<%an>%Creset' --abbrev-commit --date=relative
cm = commit --verbose
ca = commit -a --verbose
am = commit --amend
ph = push
ft = fetch
pl = pull
pr = pull --rebase
Resume
Git is a powerful tool – and sometimes hard to learn. But using aliases makes it a lot more convenient.
I found the following links very helpful: