Best way to import or export the Git Configuration

I have been having this issue for the past few days. I wanted to migrate all my shortcuts and configuration of git in my working machine across all the devices I use. I work in both Windows, Mac, and Linux and cross operate most of the times. I also have a PC in the office that would make things worse, by having a limited installation of Portable Git and I won't be able to do much with it.

At that time, I came to know of an alternate to the following:

git config --global --list

The above command lists out all the configuration made to your current git client. I would be able to only look at it and re-add them to the new device by using:

git config --global property.key value

This is not going to work out if I have a huge load of configuration lines. Accidentally I stumbled upon this source viewing style:

git config --global --edit

And this opens up vim or any of the configured editor of your choice with all the editable and copyable fields. Shows something like this:

# This is Git's per-user configuration file.
[user]
# Please adapt and uncomment the following lines:
#    name = Praveen Kumar Purushothaman
#    email = praveen[@]praveen[.]science
[user]
    email = praveen[@]praveen[.]science
[core]
    preloadindex = true
    fscache = true
[gc]
    auto = 256
[alias]
    a = !git add . && git status
    aa = !git add . && git add -u . && git status
    ac = !git add . && git commit
    acm = !git add . && git commit -m

I was so happy to find that I would be able to access the whole configuration through this file, but unfortunately I wasn't able to access everything and well, they aren't defined the same way from the --list parameter. Unfortunately, to my dismay, in my Mac, it had only the [user] section and nothing else. I was trying so hard to find out where all these configurations are stored.

I had read in Stack Overflow on this, this and this. Since I am using a Mac, I was looking at the macOS related entries but no avail. I have seen a lot of posts saying, the default location for git to store the configuration stuff is:

$(prefix)/etc/gitconfig

I tried a lot of ways to find what's within $prefix, and in one post, I found out that it's an empty string. I was devastated. Luckily I stumbled upon this nifty command that shows something similar to git blame on the various locations, where the configuration is stored:

git config --list --show-origin

And voila, this is the output I got:

$ git config --list --show-origin
file:"C:\\Git\\mingw64/etc/gitconfig"   core.symlinks=false
file:"C:\\Git\\mingw64/etc/gitconfig"   core.autocrlf=true
file:"C:\\Git\\mingw64/etc/gitconfig"   color.diff=auto
file:"C:\\Git\\mingw64/etc/gitconfig"   color.branch=auto
file:"C:\\Git\\mingw64/etc/gitconfig"   color.interactive=true
file:C:/Users/praveen/.gitconfig user.email=praveen[@]praveen[.]science
file:C:/Users/praveen/.gitconfig core.preloadindex=true
file:C:/Users/praveen/.gitconfig core.fscache=true
file:C:/Users/praveen/.gitconfig gc.auto=256
file:C:/Users/praveen/.gitconfig alias.c=commit
file:C:/Users/praveen/.gitconfig alias.ca=commit --amend
file:C:/Users/praveen/.gitconfig alias.cm=commit -m
file:C:/Users/praveen/.gitconfig alias.co=checkout
date=relative
file:C:/Users/praveen.purush/.gitconfig alias.master=checkout master
file:C:/Users/praveen.purush/.gitconfig alias.s=status
file:.git/config        core.repositoryformatversion=0
file:.git/config        core.filemode=false
file:.git/config        core.bare=false
file:.git/config        core.logallrefupdates=true
file:.git/config        core.symlinks=false
file:.git/config        core.ignorecase=true

It looks like git stores its configuration in three different locations (or more, may be) and combines everything.

  • C:\Git\mingw64/etc/gitconfig
  • C:/Users/praveen/.gitconfig
  • .git/config

I just had to copy the lines from the file and put it on the other device's git's configuration file. And voila! It works. This was really interesting to me as I learnt something new and I thought of sharing with the community, if someone would like to use it for themselves.



comments powered by Disqus