Version Control with Git

Setting Up Git

Learning Objectives

  • Configure git the first time is used on a computer.
  • Understand the meaning of the --global configuration flag.

When we use Git on a new computer for the first time, we need to configure a few things. Here’s how Dracula sets up his new laptop:

$ git config --global user.name "Vlad Dracula"
$ git config --global user.email "vlad@tran.sylvan.ia"
$ git config --global color.ui "auto"

(Please use your own name and email address instead of Dracula’s.)

He also has to set his favorite text editor, following this table:

Editor Configuration command
nano $ git config --global core.editor "nano -w"
Text Wrangler $ git config --global core.editor "edit -w"
Sublime Text (Mac) $ git config --global core.editor "subl -n -w"
Sublime Text (Win) $ git config --global core.editor "'c:/program files/sublime text 2/sublime_text.exe' -w"
Notepad++ (Win) $ git config --global core.editor "'c:/program files (x86)/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"
Kate (Linux) $ git config --global core.editor "kate"
Gedit (Linux) $ git config --global core.editor "gedit -s"

Another set of optional but useful settings provides shortcut aliases:

git config --global alias.st status 
git config --global alias.ci commit 
git config --global alias.co checkout 
git config --global alias.hist 'log --graph --decorate --pretty=oneline --abbrev-commit'
git config --global alias.ls 'log --pretty=format:"%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)[%an]%Creset" --abbrev-commit'
git config --global alias.ll 'log --pretty=format:"%C(yellow)%h%Cred%d %Creset%s%Cblue [%cn]" --decorate --numstat'

Lastly, the bash prompt can be modified by using a helper function best called from ~/.bashrc:

# Set the prompt to show the current git branch:
function parse_git_branch {
    ref=$(git symbolic-ref HEAD 2> /dev/null) || return
    echo "("${ref#refs/heads/}")"
}
PS1="\u@\h:\w\$(parse_git_branch)\$ "

The gitbash Windows installer already has this set.

Git commands are written git verb, where verb is what we actually want it to do. In this case, we’re telling Git:

  • our name and email address,
  • to colorize output,
  • what our favorite text editor is,
  • which alias’ed commands we want to use, and
  • that we want to use these settings globally (i.e., for every project),

The few commands above only need to be run once: the flag --global tells Git to use the settings for every project, in your user account, on this computer.

You can check your settings at any time:

$ git config --list

You can change your configuration as many times as you want: just use the same commands to choose another editor or update your email address.