2.1 The .gitconfig File

There are three places that Git stores configuration information. Which one you use depends on how widely you want those configurations to apply.

The first is System level configuration, they apply to every user of your computer. These are the default configurations.

On Unix that will be inside the etc directory, in a file called .gitconfig.

The second is User level configurations. These are going to apply to a single user.

On Unix, that's going to be in your home directory, inside a file called .gitconfig.

@ user directory

cat .gitconfig

The third is on a project-by-project basis. These configurations will apply only to that project.

If there is something specific to a single project, you can put it inside of the project. Look for a folder inside the project called .git, and inside that folder a file called config.

Git gives us some commands that we can use to make editing these configurations easy. For all three of them, it's going to be git config, followed by a modifier that tells at what level we want to do the configuration, and then followed by the configuration itself that we want to do.

So if we want to do a system-wide configuration, then it's —system at the end, if it's User level then that's —global, (global doesn't mean system, it means global to the user,) and then if we don't have any modifier then it's just on a single project basis.

Setting configuration at the user level: set the user's name

git config --global user.name "name"

Setting configuration at the user level: set the user's email

git config --global user.email "email address"

To read configurations:

git config --list

To read a specific configuration, you can say user.name, user.email, etc...

git config user.email

This is the minimum that you need to configure and to start working with git.

Optional, useful configurations:

Tell Git what Text Editor you will be using.

For Nano

git config --global core.editor "nano -wl1"

or for Textmate

git config --global core.editor "mate -wl1"

In addition to just launching it though, we need to provide a couple of options with it, which are:

  • the -w option tells Git to wait until the editor is finished (save and close the document) before continuing.
  • the l1 option tells Git to start the editor at line 1.

Next you can tell Git to use colors when outputting things to the command line.

git config --global color.ui=true

By setting this option, it will allow Git to try and use colors like red and green and blue to help illustrate what point it's trying to get across, what information it's trying to convey.

2.2 Git Auto-Completion

@ user directory

curl -OL https://github.com/git/git/raw/master/contrib/completion/git-completion.bash

check the file is there

ls -la

Rename the file (adding a dot in front of the name...)

mv ~/git-completion.bash ~/.git-completion.bash

Tell UNIX to load the file if its there

# Load git-competion.bash if it exists
if [ -f ~/.git-completion.bash ]; then
    source ~/.git-completion.bash
fi

Now if you type git h followed by the tab it will auto-complete the command to git help

2.3 Informative Git Prompt

If you want to install a more informative Git prompt then you can check out this project on Github which is a port of the "Informative git prompt for zsh".

In particular the branch name, difference with remote branch, number of files staged, changed, etc. will be displayed

2.3.1 Examples

  • (master↑3|✚1): on branch master, ahead of remote by 3 commits, 1 file changed but not staged
  • (status|●2): on branch status, 2 files staged
  • (master|✚7…): on branch master, 7 files changed, some files untracked
  • (master|✖2✚3): on branch master, 2 conflicts, 3 files changed
  • (master|⚑2): on branch master, 2 stash entries
  • (experimental↓2↑3|✔): on branch experimental; your branch has diverged by 3 commits, remote by 2 commits; the repository is otherwise clean
  • (:70c2952|✔): not on any branch; parent commit has hash 70c2952; the repository is otherwise clean

2.4 Using Git Help

git help