Oh git configurations! Let's simplify
Tags: git, devops, linux, versioncontrol
Published: 2018-03-21 19:51:50

---
title: Oh git configurations! Let's simplify
published: true
description: Get to know git configurations
tags: git, devops, linux, versioncontrol
---

## Oh Git is complex!

Git is a complex system.  To see the proof check out:

1. [ohshitgit.com](http://ohshitgit.com)
1. [gitfightrules.com](https://github.com/k88hudson/git-flight-rules)
1. [git-man-page-generator.net](https://git-man-page-generator.lokaltog.net/)

Prooved. ;)

To mitigate that **we are going to get inside the head of `Linus Torvalds`!** (Just a little bit maybe into a single neuron), and, in this part, we will focus on configurations.  I always thought that to understand a system I need to understand it's configurations (or even better it's installation).  

When I get to a new company the first thing I try to figure out is:

1. How do I install this?
2. How do I configure this?

## How does Linus Torvalds think? 

> Everything is a file

**Yes this is how Linus thinks: everything is a file**

As Linus loves this everything is a file, you can just view git configurations as a file.

**So if we manage to get which files Linus uses in git we might be able to penetrate into oh-my-complex-git!**

## 3 Fallback layers of config


**1. System - Your OS git config - `git config --system`**
**2. Global - Your User git config - `git config --global`**
**3. Local - Your Repository git config - `git config --local`**

```bash
git config --system # => /etc/gitconfig

git config --global # => ~/.gitconfig or ~/.config/git/config

git config --local # => .git/config
```

> Git first read git config from .git/config -> [fallback to] ~/.gitfconfig -> [fallback to] /etc/gitconfig

## Email per repo

So if you have different email addresses for different repository this goes into `.gitconfig == git config --local`

## Get the global config: `git config --list --global`

```bash
➜  tmp git config --list --global

user.name=Tomer Ben David
user.email=tomer.bendavid@ohohoh.com
core.autocrlf=input
format.pretty=format:%h %Cblue%ad%Creset %ae %Cgreen%s%Creset
```

Would that be the same as `cat ~/.gitconfig` what do you think?

```bash
➜  tmp cat ~/.gitconfig
[user]
	name = Tomer Ben David
	email = tomer.bendavid@ohohoh.com
[core]
	autocrlf = input
[format]
  pretty = format:%h %Cblue%ad%Creset %ae %Cgreen%s%Creset
```

Yes and no! different notation but generally the same!

## Get the merged config: `git config --list`

The merged config is the combination of all configs with the hierarchy, let's see it on my machine ;)

```bash
git config --list

➜  tmp git config --list
core.excludesfile=~/.gitignore
core.legacyheaders=false
core.quotepath=false
mergetool.keepbackup=true
push.default=simple
color.ui=auto
color.interactive=auto
repack.usedeltabaseoffset=true
alias.s=status
alias.a=!git add . && git status
alias.au=!git add -u . && git status
alias.aa=!git add . && git add -u . && git status
alias.c=commit
alias.cm=commit -m
alias.ca=commit --amend
alias.ac=!git add . && git commit
alias.acm=!git add . && git commit -m
alias.l=log --graph --all --pretty=format:'%C(yellow)%h%C(cyan)%d%Creset %s %C(white)- %an, %ar%Creset'
alias.ll=log --stat --abbrev-commit
alias.lg=log --color --graph --pretty=format:'%C(bold white)%h%Creset -%C(bold green)%d%Creset %s %C(bold green)(%cr)%Creset %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
alias.llg=log --color --graph --pretty=format:'%C(bold white)%H %d%Creset%n%s%n%+b%C(bold blue)%an <%ae>%Creset %C(bold green)%cr (%ci)' --abbrev-commit
alias.d=diff
alias.master=checkout master
alias.spull=svn rebase
alias.spush=svn dcommit
alias.alias=!git config --list | grep 'alias\.' | sed 's/alias\.\([^=]*\)=\(.*\)/\1\	 => \2/' | sort
include.path=~/.gitcinclude
include.path=.githubconfig
include.path=.gitcredential
diff.exif.textconv=exif
credential.helper=osxkeychain
user.name=Tomer Ben David
user.email=tomer.bendavid@ohohohoh.com
core.autocrlf=input
format.pretty=format:%h %Cblue%ad%Creset %ae %Cgreen%s%Creset
```

That was it **ALL** the git config on my machine, you have it! I hope I didn't put any password there ;) If I did please let me know, code review guys, this is importanto!

## git config --local/global/system user.name - get a single config

**Ask layer by layer for a config**

```bash
➜  tmp git config --local user.name
fatal: BUG: setup_git_env called without repository # We called from a non-repository folder.
➜  tmp git config --global user.name
Tomer Ben David
➜  tmp git config --system user.name # => nothing in the global config file.
➜  tmp
```

Aha! so it's coming from the global (user) config!

Note that in the file it's with `[user]` and the name, and the git config returns the combined name `user.name`

if you need a hierarchy larger than 3 then in the file it will look like:

```bash
[branch "gke"]
	remote = origin
	merge = refs/heads/gke
```

so this one should be `branch.gke.remote`, let's verify this:

```bash
➜  .git git:(gke) git config branch.gke.remote # => yes it is branch.gke.remote!
origin
```

## Set a new config with `git config mysection.mykey myvalue`

```bash
➜  .git git:(gke) git config mysection.mykey myvalue
➜  .git git:(gke) git config mysection.mykey
myvalue
```

So we were able to set it let's look at the file:

```bash
➜  .git git:(gke) cat config
[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
	ignorecase = true
	precomposeunicode = true
[remote "origin"]
	url = https://bitbucket.org/yshenbaum/k8s-course.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "gke"]
	remote = origin
	merge = refs/heads/gke
[mysection] ##### =====> Here it is! see the section in []
	mykey = myvalue
```

## Summary

You now know actually exactly where your git config files are this is very helpful and much more explainable then using the git commands. Rereading this post not sure was able to simplify, but it makes some sense, at least some sense at the end of the day for us programmers! One last note

## Resources 

**[Ry's Git Tutorial (BOOK)](http://devatrest.blogspot.com/2018/03/rys-git-tutorial-book-review.html)**