Create Global gitconfig and Setup git alias

Developers using GIT as their Source Version Control know the troubles in setting it up for new projects. Getting more productive needs setting up of user credentials, alias’ and other git configurations for every new project. But what if you could have a global git configuration to avoid redundant tasks? That’s where global gitconfig comes in.

Git does not automatically create the global .gitconfig file during installation. This file is not created until it is written to for the first time. If you have never set a system variable, it will not be on your file system.

Global gitconfig

Creating your global gitconfig file:

There are several ways in which you can create your global gitconfig file.

  • From the Terminal, execute git config --global -e which will open up a text editor with the contents of the file (only if it has some content already, else it would be empty). You can then update the file with your custom configurations.
  • Executing git config --global --list will show you where the global gitconfig file is expected to be only when it does not exist yet. Create a text file .gitconfig in that expected location and save it with your custom configuration.
  • If the file exists already, the previous step will show you the contents of the global .gitconfig file. To access the file, try searching for it in its usual location as described below.

Where to find your global gitconfig file?

For GIT v2.8 and above:

Execute git config --list --show-origin that will list all the configurations with the corresponding filenames as shown below:

    file:$HOME/.gitconfig     // global gitconfig file
    file:$HOME/.gitconfig             user.override=global
    file:$HOME/.gitconfig             include.path=$INCLUDE_DIR/absolute.include
    file:.git/config                  user.local=true   // local gitconfig file
    file:.git/config                  user.override=local
    file:.git/config                  include.path=../include/relative.include

For GIT below v2.8:

Git looks for the global gitconfig file in the following locations:

  • For *NIX Systems, the file is located at ~/.gitconfig (i.e. $HOME/.gitconfig)
  • For Windows based Systems, this file is located differently depending on the OS Version.
    • Windows XP:  C:\Documents and Settings\%USERPROFILE%\.gitconfig
    • Windows Vista,7,8,10 : C:\Users\%USERPROFILE%\.gitconfig
    • If you use TortoiseGit, you can access the file directly from the Menu – Settings/Git/Config/Edit global .gitconfig
    • If you use Cygwin + Git, then the file is located at C:\cygwin(64)\home\%USERPROFILE%\.gitconfig

Common global gitconfig configurations:

  • User Details: You can provide your name, email to ensure that all your repositories have the same details.
  • Alias: Aliases can be used to shorten git commands and help you be more productive. Check below section on Aliases.
  • Colors: You can customize the color schemes used by default. Check out Git Prompt for having git branch information directly in your terminal.
  • Credential helpers: You can setup credential helpers that would eliminate the need to even enter your password.
  • Core: Helps you to customize core options like providing the path to your global .gitignore file.
  • Diff: Allows you to specify your custom diff tool.

As an example, you can check out this global .gitconfig file. Feel free to fork it and use it as your own global gitconfig.

GIT Alias’

Using Alias makes your Git experience simpler, easier, and more familiar. It comes handy when you don’t want to type the entire text of each of the Git commands.

By executing git config --global checkout in the terminal, you effectively add an alias for git checkout in your global gitconfig file. So henceforth, you could simply type git co (instead of git checkout) to check out a branch.
You can add several alias’ by directly updating the global gitconfig file. A few useful common git alias’ are listed below:

// global .gitconfig file
  aa = add --all
  b = branch -v
  bd = branch -d
  bD = branch -D
  br = branch
  ci = commit
  cim = commit -m
  cima = commit -am
  clr = rm -r --cached
  co = checkout
  cp = cherry-pick -x
  d = diff -w
  dc = diff --cached
  nb = checkout -b
  lg = log --graph --date=relative --pretty=tformat:'%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%an %ad)%Creset' -20
  oups = commit --amend --no-edit
  r = remote -v
  rh = reset --hard
  st = status -sb
  t = tag -l
  unstage = reset HEAD
  uncommit = reset --soft HEAD^
  pushfl = push --force-with-lease

Resource:Global gitconfig file

Further Steps

  • Setup your global gitignore file to avoid creating local gitignore files for each individual project.
  • Setup up a colorful GIT prompt to further improve productivity. It will enable you to see your current branch, status of the repository, whether you have changes/stashed content and so on.
  • Want to improve code quality? See how to use git hooks to automatically lint your code