Skip to content

Instantly share code, notes, and snippets.

@cassiozen
Last active April 23, 2019 19:24
Show Gist options
  • Save cassiozen/340b664c6b0c4b01d17dd15f835344e4 to your computer and use it in GitHub Desktop.
Save cassiozen/340b664c6b0c4b01d17dd15f835344e4 to your computer and use it in GitHub Desktop.

Now that you have Git on your system, you’ll want to do a few things to customize your Git environment. You should have to do these things only once on any given computer; they’ll stick around between upgrades. You can also change them at any time by running through the commands again.

Git comes with a tool called git config that lets you get and set configuration variables that control all aspects of how Git looks and operates.

Let's take a look at some common configurations that you might want to do:

Identity

git config --global user.name "John Doe"

Your full name to be recorded in any newly created commits.

git config --global user.email johndoe@example.com

Your email address to be recorded in any newly created commits.

Editor

git config --global core.editor "code --wait"

Commands such as commit and tag that let you edit messages by launching an editor use the value of this variable when it is set.

Aliases

Aliases are custom shortcuts that expand to longer or combined commands. Here are a few common aliases:

git config --global alias.ci commit
git config --global alias.co checkout
git config --global alias.st status

You can also check git's documentation for some more alias examples.

Colors

Git fully supports colored terminal output, which greatly aids in visually parsing command output quickly and easily. A number of options can help you set the coloring to your preference.

Git automatically colors most of its output, but there’s a master switch if you don’t like this behavior. To turn off all Git’s colored terminal output, do this:

git config --global color.ui false

The default setting is auto, which colors output when it’s going straight to a terminal, but omits the color-control codes when the output is redirected to a pipe or a file.

You can also set it to always to ignore the difference between terminals and pipes. You’ll rarely want this; in most scenarios, if you want color codes in your redirected output, you can instead pass a --color flag to the Git command to force it to use color codes. The default setting is almost always what you’ll want.

Individual color options

Git provides verb-specific coloring settings, so you can choose the color & character setting for each Git command individually, for example:

  • color.branch
  • color.diff
  • color.status

To set the meta information in your diff output to blue foreground, black background, and bold text, you can run

git config --global color.diff.meta "blue black bold"

Available colors include: normal, black, red, green, yellow, blue, magenta, cyan, or white.

Autocorrect

$ git config --global help.autocorrect 1

If you mistype a command, it shows you something like this:

$ git chekcout master
git: 'chekcout' is not a git command. See 'git --help'.

Did you mean this?
    checkout

Git helpfully tries to figure out what you meant, but it still refuses to do it.

If you set help.autocorrect to 1, Git will actually run this command for you:

$ git chekcout master
WARNING: You called a Git command named 'chekcout', which does not exist.
Continuing under the assumption that you meant 'checkout'
in 0.1 seconds automatically...

Note that “0.1 seconds” business. help.autocorrect is actually an integer which represents tenths of a second. So if you set it to 50, Git will give you 5 seconds to change your mind before executing the autocorrected command.

Bonus: Excludes File

Setting up a .gitexcludes file means you'll never accidentally commit something you didn't intend to, just like a .gitignore file, but for all your local repositories.

Create your excludes file

touch ~/.gitexcludes

Add some useful ignores

# OS-generated files
.DS_Store
._*
.Spotlight-V100
.Trashes
Thumbs.db

# Logs
*.log

Update git's config

git config --global core.excludesfile ~/.gitexcludes

Bonus: Adding git information to your command line prompt

If you're using Z Shell with Oh-my-zsh, simply use a theme that shows git information (such as the current branch and the change status). Common options include Simple, Muse and Agnoster.

If you're using the default bash, you can add the following to your ~/.bash_profile or ~/.bashrc:

### Add Git Status to bash prompt
git_branch() {
     git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}

export PS1="\u@\[\033[32m\]\w\[\033[33m\]\$(git_branch)\[\033[00m\]\$ " 
PROMPT_COMMAND='echo -ne "\033]0;${PWD}\007"'

It will look something like this:

user@~/Sites/example.com (master) $ 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment