Skip to content

Basic Git configuration

Let's customize the git configuration for an improved DX.

3 min read

To show where you global config file is:

Shell command
git config --list --show-origin

Set up our user and email:

Shell command
git config --global user.name "Your name goes here"
git config --global user.email "your@email.goes.here"

Set the default branch name for your new repos:

Shell command
git config --global init.defaultBranch main

Always git pull with rebase:

Shell command
git config --global pull.rebase true

Let's have colored output for git status and git diff:

Shell command
git config --global color.ui "auto"

Enable the automatic detection of CPU threads to use when packing repositories:

Shell command
git config --global pack.threads "0"

Define a global .gitignore file:

Shell command
git config --global core.excludesfile ~/.config/git/globalexcludes

Let's use it to ignore vscode project files and files generated by the ElixirLS plugin:

Shell command
echo ".vscode/" >> ~/.config/git/globalexcludes
echo ".elixir_ls/" >> ~/.config/git/globalexcludes
echo ".impeccable/" >> ~/.config/git/globalexcludes

Here are some very useful aliases.
Let's add them to our ~/.config/git/config:

~/.config/git/config
# remove any pre-existing aliases set by omarchy...
[alias]
l = log --oneline --decorate --graph
ll = log --graph --abbrev-commit --decorate \
--format=format:'%C(yellow)%h%C(reset) %<(14)%C(cyan)%ar%C(reset) \
%C(white)%s%C(reset) %C(bold dim magenta) %an%C(reset) \
%C(bold green)%d%C(reset) %C(brightcyan)%G?%C(reset)'
llb = log --graph --abbrev-commit --decorate \
--format=format:'%C(yellow)%h%C(reset) %<(14)%C(cyan)%ar%C(reset) \
%C(white)%s%C(reset) %C(bold dim magenta) %an%C(reset) \
%C(bold green)%d%C(reset) %C(brightcyan)%G?%C(reset)' --branches
lll = log --graph --abbrev-commit --decorate \
--format=format:'%C(yellow)%h%C(reset) %C(cyan)%ai%C(reset) \
%C(white)%s%C(reset) %C(bold dim magenta) %an%C(reset) \
%C(bold green)%d%C(reset) %C(brightcyan)%G?%C(reset)'
lllb = log --graph --abbrev-commit --decorate \
--format=format:'%C(yellow)%h%C(reset) %C(cyan)%ai%C(reset) \
%C(white)%s%C(reset) %C(bold dim magenta) %an%C(reset) \
%C(bold green)%d%C(reset) %C(brightcyan)%G?%C(reset)' --branches
sl = stash list --format='%gd: %C(bold yellow)%gs'
slp = stash list -p --format='%gd: %C(bold yellow)%gs'
wl = worktree list
wa = worktree add
wr = worktree remove
co = checkout
ci = commit
cis = commit -S -s
man = help
h = help
a = add
f = fetch
d = diff
dc = diff --cached
dt = difftool
dtc = difftool --cached
ds = diff --stat
dsc = diff --stat --cached
s = status --short --branch
b = branch
pfl = push --force-with-lease
pp = pull --prune

And some extras:

~/.config/git/config
[merge]
conflictstyle = zdiff3
[log]
date = iso
[credential]
helper = cache
[diff]
algorithm = histogram

Dealing with line endings:

Shell command
git config --global core.autocrlf input
git config --global core.safecrlf true

That's all. Easy peasy, lemon squeezy. 🍋

Bonus: rebase interactive script

Add a new ~/bin/git-ri file:

~/bin/git-ri
#!/bin/bash
git rebase -i HEAD~$1

Then chmod u+x ~/bin/git-ri.

And now you can use git ri 2 inside any git repo instead of typing git rebase -i HEAD~2 whenever you want to squash, rename, etc.

How to delete a remote branch

Shell command
git push -d origin remote-branch

From: How do I delete a Git branch locally and remotely? 

More posts connected by shared tags.