Git config

From wikinotes

Locations

~/.gitconfig
${PROJECT}/.git/config
git config (pull style, line endings, aliases, log format, ...)
~/.gitignore
${ANY_PROJ_DIR}/.gitignore
define files ignored by git
~/.gitattributes
${ANY_PROJ_DIR}/.gitattributes
define files ignored by git
${PROJECT}/.git/hooks/* hooks for shellscripts

.gitconfig

INI file that can be managed interactively or by hand.

.gitconfig format

# ~/.gitconfig
# ${PROJECT}/.git/config
[user]
email = willjpittman@gmail.com
name = Will Pittman

[pager]
# add /usr/share/git/ to $PATH so that 'diff-highlight' is executable
log = diff-highlight | less
show = diff-highlight | less
diff = diff-highlight | less

[pull]
rebase = False

[push]
autoSetupRemote = true


[alias]
s = status -s
st = status

h = log --name-only --abbrev-commit --graph
hs = log --pretty=oneline --abbrev-commit --graph

Interactively setup git-config

git config --global user.name      "Will Pittman"
git config --global user.email     willjpittman@gmail.com
git config --global core.editor    vim
git config --global merge.tool     vimdiff
 
git config --global core.autocrlf  false   # ignore windows line endings
git config --global core.autolf    true    # force unix line endings
git config --global core.fileMode  false   # ignore 755 >> 644 permission changes on windows

git config --global --add --bool push.autoSetupRemote true  # auto push upstream on first git push

.gitignore

Define files that should not be committed to git repo.

# ~/.gitignore
# ${PROJECT}/../.gitignore
tags               # exclude file 'tags' anywhere it exists
build/**/manifest  # '**' matches any level of directory
*.json             # '*' glob matches

You can also define an allowlist, excluding everything by default.

*
!*/
!.gitignore
!/devices/**
!/folders/**
!/bin/**

interactive use

git config --global core.excludesfile ~/.gitignore

If a file has already been committed to git, to untrack it, you must git rm --cached path/to/file.

.gitattributes

Match files, and configure how they are managed by git (ex: line endings).

# ${PROJECT}/**/.gitattributes
*.py       eol=lf
*.rst      eol=lf
*.vcsproj  eol=crlf   # windows style line-endings for vcxproj files
*.exe      !eol       # do not modify line-endings of .exe files

.git/hooks/

Git provides hooks so that jobs can be done automatically at key points.

prepare-commit-msg

Customize your commit messages automatically with additional info

CONFIG_MSG=$1
echo "my new line in commit message" >> $CONFIG_MSG

smudge/clean

NOTE:

Requires the package `expand`. Under windows, it can be installed by copying the gnu-coreutils program executables somewhere on your %PATH%

git config --global filter.tabspace.clean  'expand --tabs=2'  #clean is outgoing  (to   HEAD)
git config --global filter.tabspace.smudge 'expand --tabs=2'  #smudge is incoming (from HEAD)
#### <git-project-root>/.git/info/attributes
## MAKE SURE YOU EXPLICITLY STATE THE FILETYPES YOU WANT TO STRIP TABS
## FROM!!! Stripping tabs from binary files is not safe and will cause issues.
 
*.mel filter=tabspace
*.py  filter=tabspace
####

post-merge

post-merge is run before every push, and after every pull/merge operation. This is tremendously useful for:

  • removing all .pyc files (so not sourced accidentally)
  • deleting empty folders (keep uncluttered)
  • triggering ansible playbook deployment.

Extras

diff-highlight

per-character diff highlighting.
see https://github.com/git/git/tree/master/contrib/diff-highlight

export PATH=${PATH}:/usr/share/git
# ~/.gitconfig

[pager]
  log = diff-highlight | less
  show = diff-highlight | less
  diff = diff-highlight | less

troubleshooting

early EOF

This can happen if you are synchronizing very large files.
Try adding the following to ~/.gitconfig.

# ~/.gitconfig
[core] 
packedGitLimit = 512m 
packedGitWindowSize = 512m 
[pack] 
deltaCacheSize = 2047m 
packSizeLimit = 2047m 
windowMemory = 2047m