Read more

Configuring Git with .gitconfig

Martin Straub
August 25, 2010Software engineer at makandra GmbH

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

[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]
  default = current # Match remote branch with same name
Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

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

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.

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
[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

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.
Martin Straub
August 25, 2010Software engineer at makandra GmbH
Posted by Martin Straub to makandra dev (2010-08-25 14:09)