Skip to main content
Chapter 13 of 61 · 9 sections · Page 78

Git: Configuration

Set default editors, auto-correct typos, manage multi-account identities, line endings, and proxies.

Original chapter reference: Chapter 13: Configuration. The command options and documentation recipes below are fully transcribed from Git Notes for Professionals.

Parameter Details Edits the system-wide configuration file, which is used for every user (on Linux, this file is located at

Source example · PDF page 78
--system
          $(prefix)/etc/gitconfig)

Edits the global configuration file, which is used for every repository you work on (on Linux, this file is

Source example · PDF page 78
--global

located at ~/.gitconfig Edits the respository-specific configuration file, which is located at .git/config in your repository; this

Source example · PDF page 78
--local

is the default setting

Section 13.1: Setting which editor to use

There are several ways to set which editor to use for committing, rebasing, etc.

Change the core.editor configuration setting.

Source example · PDF page 78
      $ git config --global core.editor nano

Set the GIT_EDITOR environment variable.

For one command:

Source example · PDF page 78
      $ GIT_EDITOR=nano git commit

Or for all commands run in a terminal. Note: This only applies until you close the terminal.

Source example · PDF page 78
      $ export GIT_EDITOR=nano

To change the editor for all terminal programs, not just Git, set the VISUAL or EDITOR environment variable. (See VISUAL vs EDITOR.)

Source example · PDF page 78
      $ export EDITOR=nano

Note: As above, this only applies to the current terminal; your shell will usually have a configuration file to allow you to set it permanently. (On bash, for example, add the above line to your ~/.bashrc or

Source example · PDF page 78
     ~/.bash_profile.)

Some text editors (mostly GUI ones) will only run one instance at a time, and generally quit if you already have an instance of them open. If this is the case for your text editor, Git will print the message Aborting commit due to empty commit message. without allowing you to edit the commit message first. If this happens to you, consult your text editor's documentation to see if it has a --wait flag (or similar) that will make it pause until the document is closed.

Section 13.2: Auto correct typos

Source example · PDF page 78
git config --global help.autocorrect 17

This enables autocorrect in git and will forgive you for your minor mistakes (e.g. git stats instead of git status). The parameter you supply to help.autocorrect determines how long the system should wait, in tenths of a second, before automatically applying the autocorrected command. In the command above, 17 means that git should wait 1.7 seconds before applying the autocorrected command.

However, bigger mistakes will be considered as missing commands, so typing something like git testingit would result in testingit is not a git command.

Section 13.3: List and edit the current configuration

Git config allows you to customize how git works. It is commonly used to set your name and email or favorite editor or how merges should be done.

To see the current configuration.

Source example · PDF page 79
$ git config --list
...
core.editor=vim
credential.helper=osxkeychain
...

To edit the config:

Source example · PDF page 79
$ git config <key> <value>
$ git config core.ignorecase true

If you intend the change to be true for all your repositories, use --global

Source example · PDF page 79
$ git config --global user.name "Your Name"
$ git config --global user.email "Your Email"
$ git config --global core.editor vi

You can list again to see your changes.

Section 13.4: Username and email address

Right after you install Git, the first thing you should do is set your username and email address. From a shell, type:

Source example · PDF page 79
git config --global user.name "Mr. Bean"
git config --global user.email mrbean@example.com

git config is the command to get or set options --global means that the configuration file specific to your user account will be edited user.name and user.email are the keys for the configuration variables; user is the section of the configuration file. name and email are the names of the variables. "Mr. Bean" and mrbean@example.com are the values that you're storing in the two variables. Note the quotes around "Mr. Bean", which are required because the value you are storing contains a space.

Section 13.5: Multiple usernames and email address

Since Git 2.13, multiple usernames and email addresses could be configured by using a folder filter.

Example for Windows: .gitconfig

Edit: git config --global -e

Add:

Source example · PDF page 80
[includeIf "gitdir:D:/work"]
  path = .gitconfig-work.config

[includeIf "gitdir:D:/opensource/"]
  path = .gitconfig-opensource.config

Notes

The order is depended, the last one who matches "wins". the / at the end is needed - e.g. "gitdir:D:/work" won't work. the gitdir: prefix is required.

.gitconfig-work.config

File in the same directory as .gitconfig

Source example · PDF page 80
[user]
  name = Money
  email = work@somewhere.com

.gitconfig-opensource.config

File in the same directory as .gitconfig

Source example · PDF page 80
[user]
  name = Nice
  email = cool@opensource.stuff

Example for Linux

Source example · PDF page 80
[includeIf "gitdir:~/work/"]
  path = .gitconfig-work
[includeIf "gitdir:~/opensource/"]
  path = .gitconfig-opensource

The file content and notes under section Windows.

Section 13.6: Multiple git configurations

You have up to 5 sources for git configuration:

6 files: %ALLUSERSPROFILE%\Git\Config (Windows only) (system) <git>/etc/gitconfig, with <git> being the git installation path. (on Windows, it is <git>\mingw64\etc\gitconfig) (system) $XDG_CONFIG_HOME/git/config (Linux/Mac only) (global) ~/.gitconfig (Windows: %USERPROFILE%\.gitconfig) (local) .git/config (within a git repo $GIT_DIR) a dedicated file (with git config -f), used for instance to modify the config of submodules: git

Source example · PDF page 80
          config -f .gitmodules ...

the command line with git -c: git -c core.autocrlf=false fetch would override any other core.autocrlf to false, just for that fetch command.

The order is important: any config set in one source can be overridden by a source listed below it.

git config --system/global/local is the command to list 3 of those sources, but only git config -l would list all resolved configs. "resolved" means it lists only the final overridden config value. Since git 2.8, if you want to see which config comes from which file, you type:

Source example · PDF page 81
git config --list --show-origin

Section 13.7: Configuring line endings

Description

When working with a team who uses different operating systems (OS) across the project, sometimes you may run into trouble when dealing with line endings.

Microsoft Windows

When working on Microsoft Windows operating system (OS), the line endings are normally of form - carriage return + line feed (CR+LF). Opening a file which has been edited using Unix machine such as Linux or OSX may cause trouble, making it seem that text has no line endings at all. This is due to the fact that Unix systems apply different line-endings of form line feeds (LF) only.

In order to fix this you can run following instruction

Source example · PDF page 81
git config --global core.autocrlf=true

On checkout, This instruction will ensure line-endings are configured in accordance with Microsoft Windows OS (LF -> CR+LF)

Unix Based (Linux/OSX)

Similarly, there might be issues when the user on Unix based OS tries to read files which have been edited on Microsoft Windows OS. In order to prevent any unexpected issues run

Source example · PDF page 81
git config --global core.autocrlf=input

On commit, this will change line-endings from CR+LF -> +LF

Section 13.8: configuration for one command only

you can use -c <name>=<value> to add a configuration only for one command.

To commit as an other user without having to change your settings in .gitconfig :

Source example · PDF page 81
git -c user.email = mail@example commit -m "some message"

Note: for that example you don't need to precise both user.name and user.email, git will complete the missing information from the previous commits.

Section 13.9: Setup a proxy

If you are behind a proxy, you have to tell git about it:

Source example · PDF page 81
git config --global http.proxy http://my.proxy.com:portnumber

If you are no more behind a proxy:

Source example · PDF page 82
git config --global --unset http.proxy
Buy me a coffee
Daily Practice
0/0