Skip to main content
  1. Notes/
  2. git/

git configuration

Table of Contents

Git configuration
#

Git’s behavior can be influenced by a configuration file. This file can be located in three different places:

  1. System-wide configuration: This file is located in /etc/gitconfig and applies to all users on the system. You can edit this file with sudo git config --system -e.
  2. Global configuration: This file is located in ~/.gitconfig and applies to the current user. You can edit this file with git config --global -e.
  3. Local configuration: This file is located in the .git/config directory of a specific repository and applies only to that repository. You can edit this file with git config -e.

The configuration file is in the ini format and consists of sections, each starting with a section name in square brackets (e.g., [user]), followed by key-value pairs. The key-value pairs are separated by an equal sign (=).

Minimal configuration
#

To set up a minimal configuration, you can use the following commands:

git config --global user.name "Firstname Lastname"
git config --global user.email "firstname.lastname@example.com"

This is the minimum required to set up Git. The user.name and user.email fields are used to identify the author of commits. The email address is also used for notifications and other purposes.

If commands are not your thing, they aren’t working or you are looking to automate your configuration you can also simply edit the configuration file:

[user]
	name = Firstname Lastname
	email = firstname.lastname@example.com

Extended configuration
#

The above configuration is a good starting point, but you can customize Git further by adding more sections and options.

Some of the most useful sections to add are:

  • alias: allows you to create shortcuts for commonly used Git commands.
  • color: allows you to enable or disable color output in the terminal.
  • core: allows you to set the default editor for Git commands that require a text editor.
  • diff: allows you to set the default diff tool for Git commands that require a diff tool.
  • merge: allows you to set the default merge tool for Git commands that require a merge tool.

alias
#

Aliasses allow you to create shortcuts for commonly used Git commands. This can greatly speed up your workflow and make it easier to remember complex commands.

[alias]
	st = status -s
    pushthis = push origin $(git rev-parse --abbrev-ref HEAD) --tags

Description of the above aliases:

  • st: This alias is a shortcut for git status -s, which shows the status of the working directory in a short format.
  • pushthis: This alias is a shortcut that identifies the current branch and pushes it to the remote repository, along with any tags.

Color configuration
#

Git supports color output for many commands, which can make it easier to read and understand the output. You can enable color output by adding the following configuration:

[color]
	ui = true

[color "branch"]
	current = yellow reverse
	remote = green
	local = yellow

[color "diff"]
	frag = magenta bold
	new = green bold
	meta = yellow bold
	old = red bold

[color "status"]
	untracked = red
	added = yellow
	changed = green

This configuration enables color output for the branch, diff, and status commands. The colors can be customized to your liking.

core
#

The core section allows you to set the default editor for Git commands that require a text editor. You can set the editor option to your preferred text editor (e.g., vim, nano, code). For example, to set the default editor to vim, you can add the following configuration:

[core]
    editor = vim

diff
#

The diff section allows you to set the default diff tool for Git commands.

[diff]
	tool = vimdiff

[difftool]
	prompt = false