Git Config Command Tutorial – with Username, Password, Email, Alias Examples

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.

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

OR

$ git config -h
Git Config Help
Git Config Help

We can also print more detailed help or all options provided by git with the  --help option like below.

$ git config --help
Git Config Detailed Help
Git Config Detailed 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 local , global and system configurations.

Local

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 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.

Global

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.

System

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. 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.

Config File Location/Path
Config File Location/Path

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
Global Git Configuration File
Global Git Configuration File

System Config Path

And the system-wide configuration file is stored in the /etc/gitconfig. But keep in mind that in order to change system-wide configuration file we need root privileges.

$ cat /etc/gitconfig
System Git Configuration File
System Git Configuration File

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 All Configuration
List All Configuration

List Local Configuration

We can list Local Configuration of the Git by using both  --list and --local options like below.

$ git config --list --local
List Local Configuration
List Local Configuration

List Global Configuration

We can list Global Configuration which is user wide configuration with the --list and --global options like below.

$ git config --list --global
List Global Configuration
List Global Configuration

List System Configuration

We can list System Configuration which is user wide configuration with the --list and --global options like below.

List System Configuration
List System Configuration

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 ibaydan@poftut.com.

$ git config --local user.email "ibaydan@poftut.com"

Set Global Configuration

We can also set given configuration in 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 "ibaydan@poftut.com"

OR

$ git config  user.email "ibaydan@poftut.com"

Set System Configuration

We can also set given configuration in 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 "ibaydan@poftut.com"

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 .gitignore.

$ git config core.excludesfile ".gitignore"
Exclude Files and Extensions
Exclude Files and Extensions

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 easier to track changes. The developer 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 İsmail Baydan.

$ git config user.name "İsmail Baydan"

AND print with the following command

$ git config user.name
Set or Change User Name
Set or Change 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 set an email address for the current user. In this example, we will set ibaydan@poftut.com as email address.

$ git config user.email “ibaydan@poftut.com”

AND we can use the following command in order to print current email address.

$ git config user.email
Set or Change Email Address
Set or Change Email Address

Aliases

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.

Create Alias

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 commit command.

$ git config alias.ci commit

We will test ci alias in the following example.

$ git ci
Run Alias
Run Alias

AND we can also use existing aliases in other aliases. In this example, we will use ci alias in amend alias.

$ 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 false and 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 http.proxy attribute.

$ 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 http.proxy and 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 version 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 diff command.

$ 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

Leave a Comment