Course Tonight

Course Tonight

Did You Know?

We design Docy for the readers, optimizing not for page views or engagement

Chapter 13: Configuration

Estimated reading: 6 minutes 8 views
Parameter Details
--system Edits the system-wide configuration file, used for every user (on Linux, located at $(prefix)/etc/gitconfig).
--global Edits the global configuration file, used for every repository you work on (on Linux, located at ~/.gitconfig).
--local Edits the repository-specific configuration file, located at .git/config in your repository (default setting).

Section 13.1: Setting which editor to use There are multiple ways to set the preferred editor for committing, rebasing, etc.

Change the core.editor configuration setting globally:

$ git config --global core.editor nano

Set the GIT_EDITOR environment variable for a single command:

$ GIT_EDITOR=nano git commit

Alternatively, set the GIT_EDITOR environment variable for all commands in a terminal session (valid until the terminal is closed):

$ export GIT_EDITOR=nano

To change the editor for all terminal programs, including Git, set the VISUAL or EDITOR environment variable:

$ export EDITOR=nano

Note: To make the change permanent, consult your shell’s configuration file (e.g., ~/.bashrc or ~/.bash_profile) to set the environment variable.

Some text editors (mostly GUI ones) allow only one instance at a time and quit if an instance is already open. If this occurs, Git will display the message “Aborting commit due to empty commit message” without allowing you to edit the message. In such cases, consult your text editor’s documentation to see if it supports a --wait flag or similar option that pauses until the document is closed.

Section 13.2: Auto-correct typos To enable auto-correct in Git and forgive minor mistakes (e.g., git stats instead of git status), use the following command:

git config --

global help.autocorrect 17

The value supplied to help.autocorrect determines the system’s wait time, in tenths of a second, before automatically applying the auto-corrected command. In the example above, 17 corresponds to a wait time of 1.7 seconds before applying the auto-corrected command.

However, significant mistakes will be treated as missing commands. For example, typing git testingit would result in the message “testingit is not a git command.”

Section 13.3: List and edit the current configuration Git config allows you to customize how Git works, such as setting your name, email, favorite editor, or merge preferences. To view the current configuration, use the following command:

$ git config 

--list ... core.editor=vim credential.helper=osxkeychain ...

To edit the configuration, use the git config command followed by the key-value pair you want to modify:

$ git config <key> <value>
$ git config core.ignorecase true

If you want the change to apply globally for all your repositories, use the --global flag:

$ git config --global user.name "Your Name"
$ git config --global user.email "Your Email"
$ git config --global core.editor vi

After making the changes, you can list the configuration again to verify the updates.

Section 13.4: Username and email address One of the first things you should do after installing Git is to set your username and email address. Use the following commands in a shell:

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 specifies that the configuration file specific to your user account will be edited.
  • user.name and user.email are the keys for the configuration variables, where user is the section of the configuration file.
  • "Mr. Bean" and mrbean@example.com are the values being stored in the respective variables. Note the quotes around "Mr. Bean" because the value contains a space.

Section 13.5: Multiple usernames and email addresses Starting from Git 2.13, you can configure multiple usernames and email addresses using folder filters. Here are examples for both Windows and Linux.

Example for Windows:

Edit the .gitconfig file using git config --global -e, and add the following:

[includeIf “gitdir:D:/work”]
path = .gitconfig-work.config

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

Notes:

  • The order matters, with the last matching filter taking precedence.
  • The / at the end is necessary. For example, “gitdir:D:/work” won’t work without it.
  • The gitdir: prefix is required.

Create the .gitconfig-work.config file in the same directory as .gitconfig with the following content:

[user]
name = Money
email = work@somewhere.com

Create the .gitconfig-opensource.config file in the same directory as .gitconfig with the following content:

[user]
name = Nice
email = cool@opensource.stuff

Example for Linux:

Edit the .gitconfig file and add the following:

git lol

[includeIf “gitdir:~/work/”]
path = .gitconfig-work
[includeIf “gitdir:~/opensource/”]
path = .gitconfig-opensource

The file content and notes under the Windows section apply here as well.

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 repository $GIT_DIR)
    • a dedicated file (with git config -f), used for instance to modify the config of submodules: git config -f .gitmodules ...
  • The command line with git -c: git -c core.autocrlf=false fetch would override any other core.autocrlf setting, just for that fetch command.
  • The order is important: any config set in one source can be overridden by a source listed below it.

    To list 3 of those sources, you can use git config --system/global/local, 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 can use:

    git config --list --show-origin

    Section 13.7: Configuring Line Endings When working with a team that uses different operating systems (OS) across the project, line endings can cause trouble.

    >

  • Microsoft Windows: On Windows, line endings are normally carriage return + line feed (CR+LF). Opening a file edited on Unix systems (Linux or OSX) may cause issues as Unix systems use only line feeds (LF) for line endings. To fix this, run the following command:
  • git config --global core.autocrlf=true

    This instruction ensures line endings are configured according to the Windows OS (LF -> CR+LF) during checkout.

    • Unix Based (Linux/OSX): Similarly, when users on Unix-based OS try to read files edited on Windows, there might be unexpected issues. To prevent such issues, run the following command:
    git config --global core.autocrlf=input

    This will change line endings from CR+LF to LF on commit.

    Section 13.8: Configuration for One Command Only You can use -c <name>=<value> to add a configuration only for one command. For example, to commit as another user without changing your settings in .gitconfig, use the following command:

    git -c user.e

    mail=mail@example commit -m "some message"

    Note that you don’t need to specify both user.name and user.email in this example. Git will complete the missing information from previous commits.

    Section 13.9: Setup a Proxy If you are behind a proxy, you need to inform Git about it. Use the following command:

    git config --

    global http.proxy http://my.proxy.com:portnumber

    If you are no longer behind a proxy, unset the proxy configuration using:

    git config --global --unset http.proxy

    Leave a Comment

    Share this Doc

    Chapter 13: Configuration

    Or copy link

    CONTENTS