Skip to content

gitconfig

Marcel Schmalzl edited this page Aug 29, 2024 · 10 revisions

Introduction

The .gitconfig files provide options in order to configure the git toolchain and workflows.

.gitconfig file locations

There are three ways of how to set git configurations. See it as different scopes. Types are:

  1. System config (--system); applies to entire machine and all users)
  2. User config (--global)
  3. Repo-specific config (in local repo (in .git/config))

Finding our git files

There are some default locations but the easiest way ist to use git config --list --show-origin (all files named .gitconfig are of our interest).

Interesting .gitconfig options

Set editor

Example of adding Notepad++ as editor in Windows:

[core]
    # Pay attention on quoting!
    editor = 'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin
    # or
    editor = "'C:/Program Files (x86)/Notepad++/notepad++.exe' -multiInst -nosession"

    # or also
    editor = 'vim'

    # or for sublime
    editor = 'subl'

Enable long path names (Windows)

Path names are restricted to a certain length (4096 Bytes by git; 260 Bytes by the Windows restriction). Therefore you can set the longpath option:

[core]
	longpaths = true

Adding a proxy

[user]
	name = Master Git
	email = [email protected]

# Use a proxy
[http]
	proxy = http://bestboy:@192.0.2.150:3128
[https]
	proxy = http://bestboy:@192.0.2.150:3128

# Don't use a proxy for these URLs
[http "http://some.url.com"]
    proxy = ""
[https "https://some.url.com"]
    proxy = ""

Sign your commits via GPG

[commit]
    gpgsign = true                  # To sign all commits by default in any local repository on your computer
[user]
    # Generate key: gpg --full-generate-key
    # Show keys: gpg --list-secret-keys --keyid-format=long
    # pub   rsa4096/<someHash> 2020-12-12 [SC]
    #
    # Add key (-> <someHash>) to .gitconfig manually or via: git config --global user.signingkey <Key>
    signingkey = 55555F4C861DA238
    # Export format for GitHub: gpg --armor --export <someHash>

Check if commit is signed: git verify-commit <commitID>

Timout for credentials

[credential]
	helper = cache --timeout=3600

Set merge/diff tool

# Example for Windows
[merge]
    tool = kdiff3
    guitool = kdiff3
[mergetool "kdiff3"]
    path = "/c/Program Files/KDiff3/kdiff3.exe"
[diff]
    tool = kdiff3
    guitool = kdiff3
    path = "/c/Program Files/KDiff3/kdiff3.exe"

Set alias

Note: overwriting an existing (build-in) command via an alias is not possible and will be ignored

[alias]
    log = log -p              # NOT possible since `log` is a built-in command -> git ignores this
    lg = log -p               # This is fine
    cmt = commit --signoff    # Also fine

Multiple git configs

You can define references to multiple git configs based on repository locations:

# Check config (in repo dir) with: $ git config --show-origin --get user.email

# Example for Windows
# `gitdir` paths are Windows paths (not git bash paths)
# The slashes (`/`) in the `gitdir` paths are important!

[includeIf "gitdir:C:/work-repos/"]
    path = ~/.gitconfig-priv                    # Path to the git config

[includeIf "gitdir:C:/priv-repos/exception/"]
    path = ~/.gitconfig-work
	
[includeIf "gitdir:C:/priv-repos/"]
    path = ~/.gitconfig-work

# Generic options for all configs:
[credential]
	helper = cache --timeout=3600
# ...

Checking config locations

git config --list --show-origin

to find out the git config location and the corresponding options.

cd into a repository and execute to see repo-specific options.

Manage multiple ssh keys

Add in your sub-.gitconfig:

[core]
   sshCommand = "ssh -i ~/.ssh/private_rsa_key"

Further Reading

Clone this wiki locally