Skip to content

Configuration

Eric Bouchut edited this page Mar 13, 2021 · 27 revisions

Say git who you are

First thing first, you need to tell git who you are. But you only need to do this once, so that it remembers and uses this afterwards anytime it needs your name and email, like for instance when you commit.

git config --global user.name  'Marthy Mc Fly'
git config --global user.email [email protected]

Color

Switching to color mode really pays off when you are staring at the output of git status all day long.

git config --global color.ui auto

## Merge Tool

Configure git to use your preferred merge tool, which is meld in my case:

git config --global  merge.tool meld

Later on when a conflict arises, ask git to launch your merge tool to resolve the conflict like so:

git mergetool

Disable fast-forward during merges

You should do this once in your repository to preserve the entire commit history and properly track file changes when you merge. This creates a merge commit.

git config --add merge.ff false

Once this configuration is in place, won't have to use the option --no-ff each time you merge a branch.

git checkout master
git merge my_feature_branch 

Without this configuration, you should instead do the following.

git checkout master
git merge --no-ff  my_feature_branch # Merge my_feature_branch into master without doing a fast forward

Rebase by default when doing git pull

To prevent unnecessary merge commits when doing a git pull, you can either do git pull --rebase every time, or configure it globally once and for all (that is for all your repositories unless overridden in a specific repository), like so.

git config --global pull.rebase true

Remove --global if you only want this setting for the current repository.

Ignoring Files and Folders

Read More

Pager

git config --global core.pager 'less -FRSX'

I find this pager configuration useful in several ways. The -X option does not clear the screen when you quit the pager and provides an interesting side effect. It keeps the command output (log) in the screen history. Say you run git log this opens up the output of the command in the pager. Once you quit the pager, the screen is not blanked, and you can scroll back to see the log output.

Without the -X option, the log output is not part of the scrollback history.

It is a matter of personal preference, but I find it useful to be able to scroll back and see the output of previous git log command. This also works for whatever git command that generates an output longer than one page.

Better Colored Diff Output

When there a section of a line has changed git diff displays both the old line and new line, without any clue about what has changed inside. Using the following git contribution you will now be able to have a visual indication of what has been added / removed.

cd ~/bin
curl -O https://raw.githubusercontent.com/git/git/master/contrib/diff-highlight/diff-highlight
chmod +x diff-highlight

git config --global pager.log  'diff-highlight | less -FRSX'
git config --global pager.show 'diff-highlight | less -FRSX'
git config --global pager.diff 'diff-highlight | less -FRSX'

Congratulations. You now have a better diff/log/show output.

diff-hightlight

Another option: git diff --color-words='[^[:space:]]|([[:alnum:]]|UTF_8_GUARD)+'

Source:

Thank you Pat for sharing this tip.

push.default

TODO: https://notes.pinboard.in/u:ebouchut/3d0f62b1dc6f06f01dea

More

To read more about git's configuration:

Git Commands Directory

To know where your git commands are stored use the --exec-path option.

git --exec-path
  usr/local/Cellar/git/2.11.0/libexec/git-core

Source: Openwest 2015 - Ingy döt Net - "2 New Powerful Git Commands

Project Root Directory

The command below displays the project's absolute root directory.

git rev-parse --show-toplevel

  /Users/ebouchut/dev/my_project

This command displays the project's root directory relative to the current directory

cd src/main/java
git rev-parse --show-cdup

  ../../..

Dot Git Directory

Displays the path of the .git folder for your current local git repository.

git rev-parse --git-dir

  /Users/ebouchut/dev/my_project/.git
Clone this wiki locally