Configuring Git in your computer

Hi, I am Malathi Boggavarapu and welcome to my blog.

This tutorial describes how to configure Git in your computer. After you install Git, we can add or update several properties to Git configuration files. I have a seperate post "Git Fundamentals" in my blog which explains everything about Git but i had broken them down to smaller sections to make it easy for everyone to follow. This post will focus on Git configuration.

So let's get started.

Configuring Git

Now the git is installed on system and we will see how we configure them. Git provides three different configuration stores.

1) System level configuration

It is stored in /etc/gitconfig or C:\Program Files(x86)\Git\etc\gitconfig
This git configuration is applied to the entire computer that is installed on and you can access it using
git config --system

2) User level configuration

The second level is user level. We can access it using git config --global. It's global for a particular user and is stored in user home directory in a file called .gitconfig

3) Repository level configuration

You access this by using git config without any specifier and is stored in .git/config file in each repo.


How to configure Git



Let's see how we configure Git. It is not very common to modify system level configuration file but much more common to modify global/user level config and repository config file. Let's try the below commands

git config --global --list
Initially it gives 'Unable to read config file; No such file'

Now we try to add some info to the global or user level config file

git config --global user.name "Malathi Boggavarapu"
git config --global user.email "malathiboggavarapu@gmail.com"
git config --global --list - It shows the global config file that was created with above info.
cat ~/.gitconfig - shows the content of file that was created just now.

git config --global core.editor - The core editor is the default editor that you want to use when editing commit messages or viewing diffs and other pieces of information from Git. If you are EMACS user, you can use emacs or vim user, you can use vim or Notepad++, Notepad.

git config --global help.autocorrect 1

What autocorrect = 1 does is, if you go to your project directory and try git statsu and i misspell it. With autocorrect, it will do a fussy match on that command name and guess what you want to use instead. By setting it to 1, it waits for 0.1 seconds before it actually executing commands. If you set autocorrect to 0, it does not do auto-correcting and if you set to higher number then it will wait for that many tens of milliseconds before performing that action. So i found it very useful especially when you are typing quickly , if you make minor spelling mistake in git command, it will use fussy match to determine which command that you want to use.

git config --global color.ui auto

What auto will do is, it will use colors to show lot of git information. So when we are doing diffs or when we are showing status, it will colours the output.By setting it to auto, it is going to detect whether it is running within a script. If it is running within a script, it will not put color and put escape sequences so that logs are easier to parse. But if it is detected that as running in the terminal, it will be put the escape codes to colorize the output.

git config --global core.autocrlf true|false|input

true - convert CRLF to LF. So when we commit to repository, it will change CRLF combination which is used typically in windows into solely a LF which is then stored in a repository. when you check those files out, it will convert those text files back. It will only perform the actions on text files not on binary files. So it is not going to corrupt binary files.

false - Do nothing. That is commit CRLF to repository and store them there and don't do anything when you pull them back out. If you are only doing Windows development using Git then this might be the option but if you are doing cross platform development, you will end up having CRLF in your repo which will then endup checked out onto other platforms like linux, BSD and Mac OS X which don't generally use CRLF.

input - convert CRLF to LF when you put it into the repo but don't do any conversion on the back out.

So to summarize, on windows, we can use the option true, on Mac or Linux we can use input

So to check the result of all these configuration options we added to the gitconfig file until now, do the below commands.

git config --global --list to see all of the options
cat ~/.gitconfig - Will show the actual contents of the file

If you go to the project repository folder, you will find .git\config file.

cat .git/config
It specifies all the information for the repository. See below for example.


Now if we add a username to the config file, it will override the existing user name

git config user.name "Malathi"
git config --list - you can see that user.name will be overriden to "Malathi".

So these config sources are hierarchial.The user level one overrides any system level settings and repo level one overrides user or global level settings. So if you want to change CRLF just for the repository you can do using git config core.autocrlf true. And you can remove something into a gitconfig, use git config --unset core.autocrlf

You can also simply edit, for example vim .git/config - it will open the file then and there and you can just edit the file as you like.


So we came to the end of the session. Hope it is helpful. Please post your comments and let me know your questions and valuable inputs.

Follow my other posts on Git which were mentioned below

Brief History of Version Control Systems, DVCS Advantages and Git Installation
Working locally with Git
How to Git clone your remote repository
Git Fetching, Pulling from Remote repository and Pushing to Remote repository
Git - Creating, Verifying tags and Pushing to Remote repository
Branching, Merging and Rebasing with Git
Git - Branching, Merging, Rebasing and Cherry-picking

Happy Learning!!

Comments

Popular posts from this blog

Bash - Execute Pl/Sql script from Shell script

How to get client Ip Address using Java HttpServletRequest

How to install Portable JDK in Windows without Admin rights