DEV Community

Tomer Ben David
Tomer Ben David

Posted on • Updated on

Oh git configurations! Let's simplify

Oh Git is complex!

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

  1. ohshitgit.com
  2. gitfightrules.com
  3. git-man-page-generator.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

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

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

git config --local # => .git/config
Enter fullscreen mode Exit fullscreen mode

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

➜  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
Enter fullscreen mode Exit fullscreen mode

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

➜  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
Enter fullscreen mode Exit fullscreen mode

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 ;)

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
Enter fullscreen mode Exit fullscreen mode

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

➜  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
Enter fullscreen mode Exit fullscreen mode

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:

[branch "gke"]
    remote = origin
    merge = refs/heads/gke
Enter fullscreen mode Exit fullscreen mode

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

➜  .git git:(gke) git config branch.gke.remote # => yes it is branch.gke.remote!
origin
Enter fullscreen mode Exit fullscreen mode

Set a new config with git config mysection.mykey myvalue

➜  .git git:(gke) git config mysection.mykey myvalue
➜  .git git:(gke) git config mysection.mykey
myvalue
Enter fullscreen mode Exit fullscreen mode

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

➜  .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
Enter fullscreen mode Exit fullscreen mode

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)

Discussion (3)

Collapse
ericcurtin profile image
Eric Curtin

And to make this more complex. You can have different global configs per directory. See dev.to/fedekau/automatically-manag... I find this feature quite useful because in open source I use @gmail.com email and at work I use @hpe.com email

Collapse
tomerbendavid profile image
Tomer Ben David Author • Edited

thanks! great point! very useful!

Collapse
mayannaoliveira profile image
Mayanna Oliveira

Very good!!! ❤️😃