Git Config



After the installation of Git on your system, the next step is to customize your Git environment. You will do the configuration only once on any given computer.

Git provides a command called git config that allows you to get and to set configuration variables that control how Git looks and operates.

Git config levels

The git config command allows arguments to define the configuration level.

The following configuration levels are available:

  • --local It's the default level in Git. Git config will write to a local level if no configuration argument is passed. Local configuration values are saved
    in .git/config in the project's directory.
  • --global The global level configuration is user-specific. It is available for the current user for all projects and stored in the user's home directory.
    ~/.gitconfig on Unix systems and c:\Users.gitconfig on windows.
  • --system The system-level configuration is applied to the entire system. This means for all users and all projects on an operating system. The system-level file is stored in $(prefix)/etc/gitconfig on Unix systems and C:\ProgramData\Git\config on Windows.

Note: The order of priority for configuration is local, global, system. This means Git will start at the level and go up to the system level.


Git config command

The git config command is used to set and to get Git configuration values.

Make sure you don't forget to set up your user.name and user.email because it will show up in your commit messages.

Setting username

The username will be visible on each commit.

$ git config --global user.name  "expectocode" 

Setting useremail

The useremail will be visible on each commit.

$ git config --global user.email "user@expectocode.com"

Setting default editor

By default, Git uses the default system editor when you need to write a message.

You can configure a different one by the following command :

For Vim Editor

$ git config --global core.editor "vim"

For Notepad Editor

$ git config --global core.editor "notepad++.exe -multiInst -nosession"

Color highlighting

You can enable color highlighting by typing the following commands:

$ git config --global color.ui true
$ git config --global color.branch auto
$ git config --global color.status auto

Setting default merge tool

When a merge conflict occurs, Git will run a merge tool. By default, Git uses an implementation of the standard Unix diff program. The internal Git diff is a limited merge conflict viewer. You can use other external third party merge conflict that will give you more options.

In the following commands, we use P4Merge, which is a Free Merge conflict viewer.

$ git config --global merge.tool p4merge
$ git config --global mergetool.p4merge.path "C:/Program Files/Perforce/p4merge.exe"
$ git config --global mergetool.prompt false

The last command is to tell Git to not prompt whether or not to launch P4Merge when a merge conflict occurs.

Setting default difftool

If you find yourself limited by the internal diff tool that Git used by default, you can set up a third-party diff tool.

In the following commands, we use p4Merge.

$ git config --diff.tool p4merge
$ git config --global difftool.p4merge.path "C:/Program Files/Perforce/p4merge.exe"
$ git config --global difftool.prompt false

The last command is to tell Git not to prompt whether or not to launch P4Merge when Git tries to run diff command.

Aliases

Git offers the option to make a shortcut for your commands. So you can set up an alias if you don't want to type the entire word. Aliases help you save time and energy cost of typing frequent commands.

$ git config --global alias.ci commit
$ git config --global alias.st status
$ git config --global alias.co checkout
$ git config --global alias.br branch

You can also reference other aliases to create a powerful association.

$  git config --global alias.bra br -a

This example creates an alias bra, which composes the br alias into a new alias that uses -a flag.

Checking your settings

You can verify your configuration settings by using the git config --list command as below :

$ git config --list 

Output

pull.rebase=true
user.name=expectocode
user.email=user@expectocode.com
core.editor=notepad++.exe -multiInst -nosession
merge.tool=p4merge
mergetool.p4merge.path=C:/Program Files/Perforce/p4merge.exe
mergetool.prompt=false
diff.tool=p4merge
difftool.p4merge.path=C:/Program Files/Perforce/p4merge.exe
difftool.prompt=false


ExpectoCode is optimized for learning. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 2020-2021 by ExpectoCode. All Rights Reserved.