Skip to content

Instantly share code, notes, and snippets.

@ChrisKitching
Created July 26, 2018 15:40
Show Gist options
  • Save ChrisKitching/5df3de5ffbe6fc330afe7fdfa088687c to your computer and use it in GitHub Desktop.
Save ChrisKitching/5df3de5ffbe6fc330afe7fdfa088687c to your computer and use it in GitHub Desktop.
Git Configuration Hints

Git Configuration

Suggested git configuration options (put in ~/.gitconfig).

Nicer teminal output

Enables colour and columnised output when output is to a terminal.

[color]
        ui = auto
[column]
        ui = auto

Don't create useless merge commits on pull

[pull]
        rebase = true

Automatically set up local branches to mirror remote ones

By default, if you want to grab a branch from the remote and work on it the process is tedious. You have to:

  1. git checkout origin/someBranch: Now you have detached-head at the position of the branch
  2. git branch myLocalBranch: Make a local branch to refer to this remote one.
  3. git branch --set-upstream-to=origin/someBranch myLocalBranch: Make that new local branch track the remote one
  4. git checkout myLocalBranch: Check out your local one.

If, like me, you don't care about being able to give the local one a different name from the remote one, you can configure git such that just executing git checkout someBranch will automatically do all of the above, creating a local branch called someBranch tracking the unique remote branch by that name. You'll still have to do it the long way in cases where this is ambiguous.

[branch]
        autosetuprebase = always
        autosetupmerge = true

Automatically resolve stupid problems caused by working with Windows users.

[core]
        autocrlf = false
        trustctime = false

Reduce merge conflicts

Renormalize tells git to ignore whitespace and line-ending changes for the purposes of conflict resolution, and patience has it use a more computationally expensive (but better) diff algorithm which will tend to produce smaller conflict regions when a conflict does occur.

[merge]
        renormalize = true

Improve diffs

Enable the patience diff algorithm to get smaller diffs in some corner cases.

colorMoved will highlight moved lines in a different colour from adds/deletions in git diff output. This is very nifty.

noprefix prevents git diff from prefixing a/ and b/ to every filename in the output, which is fairly useless in the first place...

renames = copy enables copy detection as well as rename detection, and renameLimit prevents git from giving up on copy/rename detection (you have enough compute power, honest).

[diff]
        algorithm = patience
        colorMoved = true
        noprefix = true
        renames = copy
        renameLimit = 999999

Submodule status summary in git status

[status]
        submoduleSummary = true

Enable concurrent fetching of submodules

[submodule]
        # Fetch submodules in five threads.
        fetchJobs = 5

Show changes in git stash show

[stash]
        showPatch = true

Detect whitespace issues in diff views

The only difference from the default here is to enable tab-in-indent, which makes git detect and highlight indentation using tabs instead of spaces.

[core]
        whitespace=blank-at-eol,space-before-tab,tab-in-indent,trailing-space

Automatically cope with typos

If you typo a git command, but it is within a small edit distance of only one other command, this will cause git to simply run that command instead.

[help]
        autocorrect = -1

Setting a positive number instead will cause git to tell you what it's going to do, and only run the command after that many deciseconds.

Automatically parallelise pack operations

Use all cores when packing.

[pack]
        threads = 0

Set up GPG-signing of commits

See the manual.

Template directory

Git allows you to specify a "template directory" which is copied into .git whenever a repo is cloned or initialised.

[init]
        # Set a git template directory.
        templatedir = /home/whatever/.git-templates

A suggested templatedir contents is provided here. If you use this one verbatim, you'll also want to add the following hook configuration block.

[hooks]
        stopbinaries = true
        stopsecrets = true

To retroactively apply a templatedir to existing repos, just run git init in them after configuring it.

Hide IDE files (and similar)

.git/info/exclude allows you to configure local-only gitignore rules. This allows you to set up local ignores for files created by your editors/IDEs/tools without having to fill the repo's own .gitignore with stuff specific to your environment. It's a good idea to set this file in your templatedir to cover tools you like to use, so you can forget the issue forever.

Hooks to prevent stupid mistakes

The example templatedir in this repo includes hooks that:

  • Stop you committing private keys. (toggle with hooks.stopsecrets setting)
  • Stop your committing large binaries without using git lfs. (toggle with hooks.stopbinaries setting)
  • Stop you from pushing when duplicated submodules are locally checked out to different states. This indicates you might not have tested what you think you did, so may be about to push a mistake.
  • Prevents you from doing merges/rewrites when you have un-initialised submodules.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment