Last updated April 5, 2015.

Where?

Locate the .gitconfig file for your user account:

  • On Unix-like systems (OS X, Linux, BSD), check ~/.gitconfig
  • On Windows, check C:\Users\[username]\.gitconfig

If the file does not exist yet, create it.
(If the directory does not exist, search harder.)

You can also open or create it in your default terminal editor via git using the command:
git config -e --global

Within a git repo, omitting the --global flag will allow you to edit the config for that specific repo.

How?

.gitconfig is a simple text file in .ini format. Open it in the text editor of your choice.

Add the following configuration directives:

  • Always use Unix line-endings in checked out files
    [core]
      autocrlf = false
      safecrlf = false

    All code and patches will use Unix line-endings (LF) instead of Windows line-endings (CRLF). Unix line-endings are the common standard in open-source projects.

  • Do not ignore letter casing in filenames on case-insensitive filesystems (like FAT on Windows)
    [core]
      ignorecase = false

    Note: Even with this setting, Git might not catch some file/directory renames. In those cases, the only workaround is:

    git mv original original.tmp
    git mv original.tmp Original
  • Automatically rebase when pulling
    [branch]
      autosetuprebase = always

    This setting prevents accidental "merge bubbles" (detailed explanation) and is recommended for working on Drupal.org projects. (You still can, and should, merge explicitly when adding large change sets; the purpose is just to prevent unintentional merges when pushing.)

  • Use color highlighting for diffs and the git log
    [color]
      ui = true
  • Optimize diffs for renamed and copied files
    [diff]
      renames = copies

    When renaming or copying files, git diff will not show the entire file content for the rename/copy, but merely a single line denoting the rename/copy instead. Subsequent diff hunks in a patch may perform additional changes to the renamed/copied file. This keeps patches small.

    Note: git diff/format-patch sometimes may not adhere to renames = copies. The actual limit is not documented, but git only checks for renames/copies up to a similarity of 50-70%. In order to enforce the renames/copies diff behavior, pass an explicit smaller limit:

    git diff -M25%

    Do not use values lower than 25%, since that will cause git to find false-positive matches on source files; i.e., "copying" a completely different file that just happens to be somewhere in the code-base.

    Note: Patches involving renamed and/or copied files will be incompatible with patch file parsers that do not support this diff extension from git.

  • Create alias for properly applying patches
    [alias]
      a = apply --index

    git a some.patch will apply the patch and also add any new/renamed/copied/deleted files accordingly to git's index, so you do not forget to add them manually before committing the patch.

    git am some.patch (built-in) applies a patch-set. A patch-set is a patch file that contains multiple patches, using a special format that looks similar to e-mail headers for each patch in the set.

    git am only works for patch-sets. git a only works for regular diffs.

  • Create alias for creating a patch-set
    [alias]
      p = format-patch --stdout

    git p -1 > my.patch will create a patch-set that contains the last commit from the current branch.

    git p 8.x > my.patch will create a patch-set that contains all commits from the current branch since 8.x.

    Note: As of now, the common Drupal development workflow prefers regular git diff patches over patch-sets containing multiple commits. Patch-sets containing one commit are OK. Patch-sets with multiple commits are only used in edge-cases.

  • Create a global .gitignore file
    [core]
      excludesfile = ~/.gitignore

    In addition to a project-specific .gitignore file in your working directory, git will take into account the .gitignore file in your user profile directory.

    Create the ~/.gitignore file:
    For 7.x

    # Patch/diff artifacts.
    *.patch
    *.diff
    *.orig
    *.rej
    interdiff*.txt

    # emacs artifacts.
    *~
    \#*\#

    # VI swap file
    *.swp

    # Hidden files.
    .DS*
    .project

    # Windows links.
    *.lnk

    # Temporary files.
    tmp*

    # Excludes for installed Drupal site
    sites/*/files/
    sites/*/settings.php

    # Exclude IDE's project directory
    nbproject
    .idea

    For 8.x:

    # Patch/diff artifacts.
    *.patch
    *.diff
    *.orig
    *.rej
    interdiff*.txt

    # emacs artifacts.
    *~
    \#*\#

    # VI swap file
    *.swp

    # Hidden files.
    .DS*
    .project

    # Windows links.
    *.lnk

    # Temporary files.
    tmp*

    # Excludes for installed Drupal site
    sites/*/files/
    sites/*/settings.php
    sites/*/services.yml

    # Exclude IDE's project directory
    nbproject
    .idea

    Thus, git will ignore all .patch, .orig, .rej, .lnk and other files and dirs, everywhere. You can still force-add an ignored file by doing git add -f my.patch, but you usually don't want to do that.

In total for 7.x?

~/.gitconfig:

[core]
  autocrlf = false
  safecrlf = false
  ignorecase = false
  excludesfile = ~/.gitignore
[branch]
  autosetuprebase = always
[diff]
  renames = copies
[color]
  ui = true
[alias]
  a = apply --index
  p = format-patch --stdout

~/.gitignore:

# Patch/diff artifacts.
*.patch
*.diff
*.orig
*.rej
interdiff*.txt

# emacs artifacts.
*~
\#*\#

# VI swap file
*.swp

# Hidden files.
.DS*
.project

# Windows links.
*.lnk

# Temporary files.
tmp*

# Excludes for installed Drupal site
sites/*/files/
sites/*/settings.php

# Exclude IDE's project directory
nbproject
.idea

In total fo 8.x?

~/.gitconfig:

[core]
  autocrlf = false
  safecrlf = false
  ignorecase = false
  excludesfile = ~/.gitignore
[branch]
  autosetuprebase = always
[diff]
  renames = copies
[color]
  ui = true
[alias]
  a = apply --index
  p = format-patch --stdout

~/.gitignore:

# Patch/diff artifacts.
*.patch
*.diff
*.orig
*.rej
interdiff*.txt

# emacs artifacts.
*~
\#*\#

# VI swap file
*.swp

# Hidden files.
.DS*
.project

# Windows links.
*.lnk

# Temporary files.
tmp*

# Excludes for installed Drupal site
sites/*/files/
sites/*/settings.php
sites/*/services.yml

# Exclude IDE's project directory
nbproject
.idea

Comments

kay_v’s picture

For fans of gitimmersion.com, WARNING: their line ending preferences are significantly different (see Lab 1: Setup)

Setup Line Ending Preferences 02
Also, for Unix/Mac users:
Execute:
git config --global core.autocrlf input
git config --global core.safecrlf true
And for Windows users:
Execute:
git config --global core.autocrlf true
git config --global core.safecrlf true

Heads up if you're following their instructions!

ownsourcing.com - Drupal training

jesse.d’s picture

As a heads up on the above, we're using the following configuration internally for windows users:

git config --global core.autocrlf true
git config --global core.safecrlf false

And we're including a .gitattributes file that defines which files are text and which are binary based on the discussion here (configuration of a .gitattributes file shipping with core in D8): http://drupal.org/node/1803766

As far as I can tell, safecrlf is a guard against accidental corruption of binary files. This shouldn't be an issue when including a .gitattributes file that defines which extensions are binary.

jbrown’s picture

Why is core.safecrlf = false recommended? Isn't that dangerous?

https://www.kernel.org/pub/software/scm/git/docs/git-config.html

--
Jonathan Brown
http://jonathanpatrick.me/

rudivanes’s picture

In Drupal in the section "Create the ~/.gitignore file:" a line needs to be added to exclude sites/default/seervices.yml
I am changing it.

pinueve’s picture

Hi drupal friends.

I'm confused, in THIS POST says to set config to :
autocrlf = false
safecrlf = false

and in node https://www.drupal.org/node/1997092 says:
core.autocrlf = true
core.safecrlf = true

so, wich one is right?