Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Creating a Happy Git Environment on OS X

Creating a Happy Git Environment on OS X

Step 1: Install Git

brew install git bash-completion

Configure things:

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global alias.co checkout
git config --global apply.whitespace nowarn

Setup an SSH key

ssh-keygen

Hit return a couple of times -- leave password blank if you want.

cat ~/.ssh/id_rsa.pub | pbcopy

Paste that code into your settings page on your repository host(s).

Get happy Git colors. Paste the following into your ~/.gitconfig file:

[color]
    branch = auto
    diff = auto
    status = auto
[color "branch"]
    current = yellow reverse
    local = yellow
    remote = green
[color "diff"]
    meta = yellow bold
    frag = magenta bold
    old = red bold
    new = green bold
[color "status"]
    added = yellow
    changed = green
    untracked = cyan

Create a ~/.gitexcludes file and paste in this:

.DS_Store

There, now you don't have to ignore that every time.

Bash Fanciness

Add the following to your ~/.bash_profile or ~/.bashrc:

source /usr/local/git/contrib/completion/git-completion.bash
GIT_PS1_SHOWDIRTYSTATE=true
export PS1='[\u@mbp \w$(__git_ps1)]\$ '

That will add tab auto-completion for Git branches, display the current branch on your prompt, and show a '*' after the branch name if there are unstaged changes in the repository, and a '+' if there are staged (but uncommitted) changes. It will look something like this:

[user@computer ~/Sites/example.com (master*)]$ 

Bonus

If you want to have a different email address for a particular project (a personal project on your work computer, perhaps?), just run this command inside that project's folder:

git config user.email "you@example.com"

It's the same command as before, this time just omitting the --global.

Sources

@trey
Owner
trey commented May 18, 2012

former location

This is a Solutions Log post.

@geoffalday

I'm not sure these bits are still needed:

git config --global github.user [your_username]
git config --global github.token [your_token]

Mine works fine without them. Also see http://help.github.com/mac-set-up-git/ ("Set your GitHub token").

@trey
Owner
trey commented May 19, 2012

Thanks, @geoffalday! I removed that bit.

@readeharris

The alias.co checkout appears to be unnecessary, at least on my install of 1.7.10.3 via Homebrew (brew install git). It's the only alias that's there by default.

Also, thanks for this! It has been a big help and I've used it a bunch of times while setting up new systems.

@readeharris

Oh, also, when installing git via Homebrew (brew install git) and using the git in /usr/local/bin, the bash completion line in the Bash Fanciness section should be changed from:

source /usr/local/git/contrib/completion/git-completion.bash

to:

source /usr/local/etc/bash_completion.d/git-completion.bash

@pkshiu
pkshiu commented Jul 9, 2013

I am on OSX 10.8.2
I needed to source git-prompt.sh to get the "show branch" to work, i.e. add this to your .bashrc or .bash_profile:

source /usr/local/git/contrib/completion/git-prompt.sh

@mastbaum

On 10.9 / Xcode 5.0.1, Xcode's git has moved to inside Xcode.app. So if you're using that:

source /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-completion.bash
source /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh
GIT_PS1_SHOWDIRTYSTATE=true
export PS1='\u@\h:\w$(__git_ps1)$ '
@dacook
dacook commented May 2, 2014

Might need to tell Git to use your .gitexcludes file, like this
git config --global core.excludesfile ~/.gitexcludes

@AnselAtoms

Should be
export PS1='[\u@\h \w$(__git_ps1)]\$ '

@fightbulc

If you get an error like this:

-bash: __git_ps1: command not found

replace the following source /usr/local/etc/bash_completion.d/git-completion.bash
with source /usr/local/etc/bash_completion.d/git-prompt.sh.

That fixed the issue for me (mac yosemite 10.10.2)

@andrewtweber

My version of git seemed to be missing git-completion.bash entirely. Here's the direct link to the contents of that file, you can place it anywhere

https://github.com/git/git/blob/master/contrib/completion/git-completion.bash

@Lanik42
Lanik42 commented Jul 3, 2015

to find the location of the git-prompt or git-completion (none of the paths listed here were correct for me), use locate git-completion and locate git-prompt - locate may not be configured at first, but it tells you how to do so.

@funkyfuture

this is also nice: brew install bash-git-prompt to setup a clever prompt.

@saurabh6790

for mac 10.10.5 add aboce code to ~/.profile

@bennekrouf

@funkyfuture good suggestion. For the install, brew install is not enough. See https://github.com/magicmonty/bash-git-prompt for the complete install procedure.

@Serhioromano

@andrewwtweber

My version of git seemed to be missing git-completion.bash entirely. Here's the direct link to the contents of that file, you can place it anywhere

https://github.com/git/git/blob/master/contrib/completion/git-completion.bash

this file have no __git_ps1 function too.

@all
I could not set up this thing.

@cheezedigital

forgive me for imposing, however, I'm running into difficulties. currently running 10.11.5 el captain

in terminal:

-bash: /Users/CheezeDigital/.git_completion: No such file or directory
-bash: /Users/CheezeDigital/.git-completion.bash: No such file or directory

though in .bash_profile:

source ~/.git_completion
source ~/.git-prompt.sh
export PS1='\W$(__git_ps1 "(%s)")\$'

if you have a minute, mind shedding some lite?

again, forgive me for imposing on this thread..

@jeffmcfadden

@cheezedigital You'll need to download the git completion file from here: https://github.com/git/git/blob/master/contrib/completion/git-completion.bash and install it to the location your script is looking for it.

@derwiki
derwiki commented Jun 16, 2016

@cheezedigital or, if you have Xcode installed, you can add these two lines to your ~/.bash_profile:

. /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-completion.bash
. /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.