Git is a Source Code Management (SCM) tool which is used by a lot of developers. Git is the most popular SCM against SVN, Mercurial, CVS, etc. Git provides a lot of features and related configuration. Git is designed to be flexible with different configurations.
git config command provides different usages like changing user name, email address, coloring, HTTP proxy, alias, etc.
git config Command Help Information
We can print
git config help information in different ways. One way is running
git config command without any option. Another way is using the
-h option which will provide the same short help.
$ git config
$ git config -h
We can also print more detailed help or all options provided by git with the
--help option like below.
$ git config --help
Git Configuration Levels
Before starting the usage and examples with the
git config command we should learn some basics about git configuration levels. There are 3 level git configuration files that are used in a hierarchical manner. They are named as
The Local Configuration file is related to the given project. The local configuration file is stored in the project folder under the
.git with the name of
config. Local Configuration is used only on the given project and does not interfere with the other projects. We use
--local option in order to specify, change, set, unset, and manage local configuration. If the same setting is defined in the Local, Global, or System configuration the Local configuration setting is used. This means Local configuration is the most preferred configuration in the git config configuration files.
The Global Configuration file is used for the current system user. All projects belong to the current system user will use this Global Configuration file. The Global configuration file is stored under the user home directory in the
.gitconfig file. If the same setting is defined in Global and System configuration Global configuration setting is used.
The System Configuration file is the most generic git configuration which is used by all users and all projects. It is stored in the
/etc/gitconfig file. The system-level configuration has less preference according to the Global and Local configuration. If there is the same configuration in the Global or Local configurations the System level configuration will be omitted.
Config File Location/Path
Git configurations are stored ina plain text file which can be easily read with a text editor like vim, nano, notepad++, or print to the console with the cat command.
Local Config File Path
The configuration file for the given project is stored in the
.git directory of the project with the name of
config. This configuration file is only used for the current project.
Global Config Path
There are also two more configuration levels like global which is used for the current system user and system-wide which will be used for all users in the system.
$ cat /home/ismail/.gitconfig
System Config Path
And the system-wide configuration file is stored in the
/etc/gitconfig. But keep in mind that in order to change the system-wide configuration file we need root privileges.
$ cat /etc/gitconfig
List All Configuration
As there are 3 level configuration files and some have a preference to others listing them one by one and understanding configuration is a daunting task. We can list the generall configuration which is generated by merging all 3 configurations levels with the
--list option like below.
$ git config --list
List Local Configuration
We can list Local Configuration of the Git by using both
--local options like below.
$ git config --list --local
List Global Configuration
We can list Global Configuration which is user wide configuration with the
--global options like below.
$ git config --list --global
List System Configuration
We can list System Configuration which is user wide configuration with the
--global options like below.
Set Local Configuration
As stated previously there are 3 level configuration files which have different preferences. While setting some configuration we can specify the configuration level. In order to set Local Configuration which is only used in the current project, we will use
--local option like below. In this example, we will set an email address to
$ git config --local user.email "email@example.com"
Set Global Configuration
We can also set a given configuration in the global level with the
--global option like below. In this example, we will set the email address of the user. By default is there is no explicit level specification the global level will be set.
$ git config --global user.email "firstname.lastname@example.org"
$ git config user.email "email@example.com"
Set System Configuration
We can also set a given configuration in the system level with the
--system option like below. In this example, we will set the email address of the user.
$ git config --system user.email "firstname.lastname@example.org"
Set Text Editor
While working from the command line or GUI we need to use some text editor in order to edit project files like source code, configuration, documentation, etc. By default, the system default text editor is used with the different git commands to edit source code, configuration, and documentation, etc. We can specify and set the text editor to be used with the Git commands with the
core.editor option by adding the editor name or path like below. In this example, we will set the editor like vim.
$ git config core.editor "vim"
Alternatively, we can use the following git config commands to set different popular text editors.
#Atom Editor $ git config core.editor "atom" #Emacs Editor $ git config core.editor "emacs" #Nano Editor $ git config core.editor "nano" #Vi Editor $ git config core.editor "vi" #Sublime On MacOS Editor $ git config core.editor "subl -n -w " #Sublime On Windows 32 Bit $ git config core.editor "'c:/program files (x86)/sublime text 3/sublimetext.exe' -w" #Sublime On Windows 64 Bit $ git config core.editor "'c:/program files/sublime text 3/sublimetext.exe' -w"
Exclude Files and Extensions
While using Git files under the project will be versioned and saved into the repository. As there is a lot of different type in a project some files may be unnecessary to save or a version like backup, temporary, intermediate files, images, static files, swap files, etc. We can omit them by excluding with their name or extensions. We will use
core.excludefile attribute of the git configuration in order to specify the file names or extensions. In this example, we will specify the ignore file name which is
$ git config core.excludesfile ".gitignore"
Set or Change User Name
While developing applications with git every developer has his own name and email address. We can specify our name which will make it easier to track changes. The developer’s name will be shown the commits, merges, and changes. We can specify the user name with the
user.name attribute. In this example, we will set the name as
$ git config user.name "İsmail Baydan"
AND print with the following command
$ git config user.name
Set or Change Email Address
Every developer generally has some email address which can be used for communication. We will use
user.email attribute in order to set an email address for the current user. In this example, we will set
email@example.com as an email address.
$ git config user.email "firstname.lastname@example.org"
AND we can use the following command in order to print current email address.
$ git config user.email
While using git there are a lot of different simple and complex commands. Some of them are very long to remember and trivial to write. An alias can be used to create an alias for a command which generally shortens the command usage. For example
commit command can be shortened as
ci as an alias. Git aliases are configured with the
git config command and stored in the git configuration file.
We can create an alias by using
alias keyword like below where we will add the alias to the end of
alias keyword and then provide the real command. In this example, we will create an alias named
ci which will run
$ git config alias.ci commit
We will test
ci alias in the following example.
$ git ci
AND we can also use existing aliases in other aliases. In this example, we will use
ci alias in
$ git config alias.amend ci --amend
Enable Colors In Terminal
Git can be used from the command line or terminal where the output can be colored. By default the git output is colorful. With colors, the output can be interpreted with its colors. We can disable and enable color with the
color.ui attribute with the
true values. The following command will disable terminal colors.
$ git config color.ui "false"
AND we can enable colors like below.
$ git config color.ui "true"
Set HTTP/HTTPS Proxy
While using git in enterprise environments we may need to use an HTTP proxy to access the internet or another network. We can specify an HTTP proxy with the
http.proxy attribute. We can also specify an HTTPS proxy with the
$ git config http.proxy "http://192.168.1.1"
AND for HTTPS proxy
$ git config https.proxy "http://192.168.1.1"
Unset HTTP/HTTPS Proxy
We can also unset to remove already created a proxy with the
--unset option and providing the attributes
https.proxy like below.
$ git config --unset http.proxy $ git config --unset https.proxy
Set Diff/Merge Tool
While developing applications in order to diff and merge different versions of the code diff/merge tools are used. Git uses the default internal diff tool. But we can change and set the diff tool with the
diff.external attribute like below. In this example, we will set the diff tool as
$ git config diff.tool "diff"
Set Windows Automatic End Of Line
Windows and Linux operating systems use the different end of line characters for the plain text files. This may create cross-platform problems. As git can be used both in Windows and Linux operation systems it should be set properly. We can enable the automatic end of line detection with the
core.autocrlf attribute like below. We will set
true value to this attribute.
$ git config core.autocrlf true
Unset A Configuration
Git configuration can be set by using related attributes and by using the
--unset option. In this example, we will unset the
user.name attribute which will be emptied.
$ git config user.name --unset