Skip to content

Instantly share code, notes, and snippets.

@PeterFaiman
Last active August 29, 2015 14:00
Show Gist options
  • Save PeterFaiman/11009710 to your computer and use it in GitHub Desktop.
Save PeterFaiman/11009710 to your computer and use it in GitHub Desktop.

Git Config

You can set an option for a single repo with:

git config <option> <value>

You can set an option globally (in your ~/.gitconfig file) with:

git config --global <option> <value>

For example:

git config --global color.ui true

Useful Options

Git has a ton of configuration options, and I recommend these as a good starting place.

Interface

git config --global color.ui true

Enables colorized output, which is especially helpful for diffs and git status.

git config --global core.editor vim

This sets the editor that git will launch to vim. Mostly this is for writing commit messages, but it sometimes uses it for other things too.

Of course you can set it to something besides vim, like nano, gedit, or sublime. I recommend picking a command line editor, since you work with git on the command line and it's annoying when an editor window pops up. Personally, it breaks my train of thought.

git config --global color.diff.whitespace 'red reverse'

Highlights extra whitespace in diffs such as spaces at the end of a line. Many projects have strict rules about extra whitespace in commits because it can be extremely annoying to deal with, especially for certain tools that don't realize it isn't important and show whitespace changes everywhere.

This isn't that important when working by yourself, but I find whitespace annoying and this helps with keeping it out of the repo.

Behaviour

git config --global diff.renames copies

Tells git to pay attention to when files are renamed or copied. Without this, renames can show up as a full delete and full create of the same file in a different place. Copies can show up as a full create of the same file as well. It's easier to quickly see what has happened when it simplifies those into renames and copies.

git config --global push.default upstream

Sets the default push behaviour when running just git push to push only the current branch to its upstream branch, which is the corresponding branch in the remote repo.

By default, in older versions of git, all of your branches are pushed to the remote repo, which is often not what you want and a pain to fix. In newer versions of git, simple mode is the default, which is like upstream except it will only push if the branches have the same name. If you prefer this, simply use simple instead of upstream as the config value.

Aliases

Git allows you to configure aliases for certain commands, to save time on typing. You configure aliases by adding properties to the alias option.

git config --global alias.c clone

This would make git c the same as git clone.

git config --global alias.p 'pull --ff-only'

Makes git p pull with the --ff-only flag, which aborts the pull if you will have to do a merge. Often I want to pull if I can, but not if I have to bother with a merge, especially if I'm in the middle of working on something and don't want to break my flow.

git config --global alias.s 'status --short --branch'

Makes git s produce a short form version of git status, which is a little harder to read but convenient if you just want to glance at what has changed.

git config --global alias.l 'log --graph --oneline'

Makes git s produce a short form version of git log, which lets you see more commits at once. It also adds a nice little text drawing of the commit history, showing where different clones diverged and were merged back together, which makes the history much easier to understand.

git config --global alias.d 'diff --patience'

Just a shortcut for git diff, but also adds --patience which makes the diff algorithm be more patient. It often makes diffs more readable because the algorithm waits longer to find the most accurate representation of changes, instead of finding the first that is technically correct.

git config --global alias.ds 'diff --patience --staged'

Another diff shortcut including --staged. Normally diff shows what changes haven't been added to the staging index yet. --staged does the opposite, and shows what changes have been staged in the index. It's nice for previewing what you are about to commit right before actually issuing the commit command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment