Configuring Git with .gitconfig

Updated . Posted . Visible to the public.

Basic configuration

Please keep this config simple. It should be a starting point for new developers learning Git.

[user]
  name = Your Name
  email = your.name@domain.com

[branch]
  sort = -committerdate
[color]
   ui = auto
[color "branch"]
  current = yellow reverse
  local = yellow
  remote = green
[color "diff"]
  whitespace = white reverse
  meta = blue reverse
  frag = blue reverse
  old = red
  new = green
[color "status"]
  added = green
  changed = yellow
  untracked = cyan
[interactive]
  singlekey = true # Do not require [enter] in interactive rebase
[pull]
  default = current # Match remote branch with same name
[push]
  autosetupremote = true # Start tracking the remote branch; enables pulling
  default = current # Match remote branch with same name

For more configuration examples, read on. Please only use configuration that you understand.

Aliases

[alias]
  amend = commit -v --amend # Amend changes to last commit
  b = branch -v --sort=committerdate # Sort branches by latest update ASC + show latest commit message
  c = commit -v -m # Commit from command line. Usage: git c "commit message"
  du = diff @{upstream} # Diff against upstream
  lc = show --name-status # Show last commit
  lou = log --oneline @{upstream}.. # List unpushed commits
  uncommit = reset HEAD~ --soft
  riom = rebase -i origin/master # Handy with prior `git fetch` to rebase a feature branch onto its remote target
  riu = rebase -i @{upstream}

"Upstream" is the remote branch where your current local branch is pulling from and pushing to.

Special for Gitlab

These might require extra configuration in Gitlab.

[alias]
  # Push branch, create a merge request and set the branch to be removed on merge
  pmr = push -u origin HEAD -o merge_request.create -o merge_request.remove_source_branch
  
  # If you do not need a code review, but use the merge request to run a test pipeline:
  # Push branch, create a merge request, set the branch to be removed on merge and set to merge when all tests are green
  ptm = push -u origin HEAD -o merge_request.create -o merge_request.merge_when_pipeline_succeeds -o merge_request.remove_source_branch

Miscellaneous

[core]
  editor = vim # Set the editor for commit messages, interactive rebases etc.
  excludesfile = ~/.gitignore # Global .gitignore, see https://makandracards.com/makandra/15947
[diff]
  algorithm = patience # A slower algorithm that sometimes produces better diffs
[fetch]
  prune = true # Remove local remote-tracking branches that have been deleted
[rebase]
  autoStash = true # Stash your local changes while rebasing, and re-apply afterwards

Rebase

If you prefer to rebase instead of merge:

[branch]
  autosetuprebase = always
[pull]
  rebase = true

tig Show archive.org snapshot

tig is a powerful Git interface. Highly recommended.

[tig]
  main-view = date:relative author:abbreviated commit-title:graph=true,refs:true

A better diff tool

You may switch to delta Show archive.org snapshot , a fancy diff tool with syntax highlighting and inline change highlighting.

Installation

  1. Download a git-delta_*_amd64.deb file from the releases page Show archive.org snapshot . On Ubuntu 20.04, I had to use v14.0 because of dependencies
  2. Install with dpkg -i path/to/git-delta*.deb
  3. Edit your ~/.gitconfig file like this:
    [core]
      pager = delta
    [interactive]
      difffilter = delta --color-only
    [delta]
      light = true # only set this if you use a light terminal theme
    

Improving Diffs for Ruby, RSpec and Cucumber files

See Rails developers: Have better context in Git diffs. This will correctly identify the beginning of a method instead of showing the complete class within the diff.

Colors

The value for color configuration variables is a list of 1-2 colors and 0-1 attributes, separated by spaces.

  • Colors: normal, black, red, green, yellow, blue, magenta, cyan and white
  • Attributes: bold, dim, ul, blink and reverse
    The first color is the foreground; the second is the background. The position of the attribute, if any, doesn’t matter.
Dominik Schöler
Last edit
Michael Leimstädtner
Keywords
config
License
Source code in this card is licensed under the MIT License.
Posted by Dominik Schöler to makandra dev (2010-08-25 12:09)