March 7, 2015

Git – Setting up Git and respective config files

Right after installing Git, you need to configure git before you can start using it.


You can find all my latest posts on medium.

There are 3 files where git configuration settings are stored. These files actually stores the same info but at different scopes, repo scope, user scope, and machine level scope:

  • /etc/gitconfig – machine level
  • ~/.gitconfig or ~/.config/git/config – user level
  • config file in a Git directory (i.e., .git/config) of whatever repository you’re currently using. Specific to that single repository. Repo level

Note: These locations will be different on a windows machine.

These config files have over-ride feature, where the repo-level settings has the highest power, followed by user level (aka global), and then finally machine level.

You can read/edit these files straight from the command line. For example to get a list of all overall active git settings you do:

[root@localhost ~]# git config --list        # this lists settings after all over-rides are evaluated.
fatal: error processing config file(s)
[root@localhost ~]# git config --list --global
fatal: unable to read config file '/root/.gitconfig': No such file or directory
[root@localhost ~]# git config --list --system
fatal: unable to read config file '/etc/gitconfig': No such file or directory
[root@localhost ~]#

As you can see all of the above commands have failed. That’s because none of these config files exists yet. however they get generated when apply setting from the command line. The two main settings that you need to set before you can start using git are what your name and email address is. Git needs this info so that git tags all the commits with these details for everyone to keep track of who commited what. You can set the name and email-address setting like this:

[vagrant@localhost ~]$ git config --global "Sher Chowdhury"
[vagrant@localhost ~]$ git config --global ""
[vagrant@localhost ~]$

Running the above commands generate the following file in the user’s home directory where these info are stored:

[vagrant@localhost ~]$ cat .gitconfig
        name = Sher Chowdhury
        email =
[vagrant@localhost ~]$

Now we can view this info from the command line:

[vagrant@localhost ~]$ git config --list Chowdhury
[vagrant@localhost ~]$ git config --list --global Chowdhury
[vagrant@localhost ~]$ 

Note, if you omit the “global” scope setting, then git will automatically apply the setting at the repo level, if it exists, otherwise it fails:

[vagrant@localhost ~]$ git config "Sher Chowdhury"
error: could not lock config file .git/config: No such file or directory
[vagrant@localhost ~]$

Now here’s how you do it at the system level:

[vagrant@localhost ~]$ sudo git config --system ""
[vagrant@localhost ~]$ cat /etc/gitconfig
        email =
[vagrant@localhost ~]$ git config --list --system
[vagrant@localhost ~]$