Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Owner

trey commented May 18, 2012

former location

This is a Solutions Log post.

@geoffalday

This comment has been minimized.

geoffalday commented May 18, 2012

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

This comment has been minimized.

Owner

trey commented May 19, 2012

Thanks, @geoffalday! I removed that bit.

@readeharris

This comment has been minimized.

readeharris commented May 30, 2012

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

This comment has been minimized.

readeharris commented May 30, 2012

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

This comment has been minimized.

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

This comment has been minimized.

mastbaum commented Oct 30, 2013

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

This comment has been minimized.

dacook commented May 2, 2014

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

@LanceOlsen

This comment has been minimized.

LanceOlsen commented Aug 15, 2014

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

@fightbulc

This comment has been minimized.

fightbulc commented Feb 3, 2015

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

This comment has been minimized.

andrewtweber commented Jun 25, 2015

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

@clayharris

This comment has been minimized.

clayharris 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 comment has been minimized.

funkyfuture commented Aug 11, 2015

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

@saurabh6790

This comment has been minimized.

saurabh6790 commented Sep 14, 2015

for mac 10.10.5 add aboce code to ~/.profile

@bennekrouf

This comment has been minimized.

bennekrouf commented Dec 14, 2015

@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

This comment has been minimized.

Serhioromano commented Jun 3, 2016

@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

This comment has been minimized.

cheezedigital commented Jun 4, 2016

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

This comment has been minimized.

jeffmcfadden commented Jun 6, 2016

@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

This comment has been minimized.

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
@niderhoff

This comment has been minimized.

niderhoff commented Jun 30, 2016

On El Capitan the git-completion/prompt files might be in /Library/Developer/CommandLineTools/usr/share/git-core/

@FedC

This comment has been minimized.

FedC commented Jul 11, 2016

RE: sourcing git-completion.bash, I used a more elegant solution (IMHO)

if [ -f `brew --prefix`/etc/bash_completion ]; then
  . `brew --prefix`/etc/bash_completion
fi
@macjules

This comment has been minimized.

macjules commented Jul 24, 2016

@FrontendFed even more slightly elegant:

if [ -f $(brew --prefix)/etc/bash_completion ]; then
   . $(brew --prefix)/etc/bash_completion
fi
@veganista

This comment has been minimized.

veganista commented Jul 27, 2016

Add a bit of colour to the prompt

screen-shot-2016-07-27-at-10 03 42

YELLOW="\[$(tput setaf 3)\]"
RESET="\[$(tput sgr0)\]"

PS1="\h:\W \u\$(__git_ps1 \" ${YELLOW}(%s)${RESET} \")\$ "

I've gone for yellow here but you can change the number here YELLOW="\[$(tput setaf 3)\]" from 0 - 6.

@Hareramrai

This comment has been minimized.

Hareramrai commented Aug 21, 2016

Hi ,

I am getting an extra "[" at the start of each line in terminal. Please see below.

[Harerams-MacBook-Air:demoapp hareramrai (master *) $ 
[Harerams-MacBook-Air:demoapp hareramrai (master *) $

Please help me to remove it .

@twodayslate

This comment has been minimized.

twodayslate commented Oct 3, 2016

Another way to incorporate bash_completion and git-prompt

if [ -f $(brew --prefix)/etc/bash_completion ]; then
    source $(brew --prefix)/etc/bash_completion.d/git-completion.bash
    source $(brew --prefix)/etc/bash_completion.d/git-prompt.sh
fi
GIT_PS1_SHOWDIRTYSTATE=true
export PS1='[\u@mbp \w$(__git_ps1)]\$ '
@vklogin

This comment has been minimized.

vklogin commented Oct 17, 2016

Above comments were very useful to get this working. In addition, the colors show up properly on the command line once I read the git-prompt.sh comments and used below:
PROMPT_COMMAND='__git_ps1 "\u@\h:\w" "\$ "'

@abhinaykumar

This comment has been minimized.

abhinaykumar commented Oct 17, 2016

@twodayslate your solution works on Sierra. thanks

@onpaws

This comment has been minimized.

onpaws commented Feb 8, 2017

Thanks @trey and @twodayslate and others.

I was additionally able to setup a color bash prompt by adding
GIT_PS1_SHOWCOLORHINTS=true to .bash_profile
and exporting PROMPT_COMMAND instead of PS1

@SystemDisc

This comment has been minimized.

SystemDisc commented Feb 10, 2017

This is my preferred PS1: PS1='\n\[\033[32m\]\u@\h\[\033[00m\]:\[\033[34m\]\w\[\033[31m\]$(__git_ps1)\[\033[00m\]\n\$ '

@hos4m

This comment has been minimized.

hos4m commented Feb 17, 2017

Thanks for this.

@thdatnguyen

This comment has been minimized.

thdatnguyen commented Feb 28, 2017

so the best solution for me to add color is in .bash_proflie, I add:

if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi

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
GIT_PS1_SHOWCOLORHINTS=true

export PS1='\n[\033[32m]\u@\h[\033[00m]:[\033[34m]\w[\033[31m]$(__git_ps1)[\033[00m]\n$ '

Thanks for all of advise!

@prkirby

This comment has been minimized.

prkirby commented Mar 4, 2017

You rock.

@nuboat

This comment has been minimized.

nuboat commented Mar 14, 2017

Thank

@lazlyhu

This comment has been minimized.

lazlyhu commented May 8, 2017

@trey, you should use \h instead hardcoded hostname on bash config :)

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

This comment has been minimized.

ivana4matic commented Jun 2, 2017

Thank you all! @fightbulc's answer worked for me (OS X 10.11.6).

@nicregez

This comment has been minimized.

nicregez commented Jul 7, 2017

brew bash-completion-1.3_2.sierra.bottle.tar.gz says:

Add the following line to your ~/.bash_profile:
  [ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion

You might want to replace the line

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

with the above.

@karlpokus

This comment has been minimized.

karlpokus commented Aug 11, 2017

@niderhoff Thanks! I found them in /Library/Developer/CommandLineTools/usr/share/git-core/ as well. I'm on MacOS Sierra.

@megahall

This comment has been minimized.

megahall commented Aug 31, 2017

I used the following for the CLI prompt:

export PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\[\033[31m\]$(__git_ps1)\[\033[00m\]\$ '

@leobeal

This comment has been minimized.

leobeal commented Oct 5, 2017

@megahall nice colours! thanks!

@ferantivero

This comment has been minimized.

ferantivero commented Oct 9, 2017

@megahall it looks good! Thanks,

@criticalbh

This comment has been minimized.

criticalbh commented Oct 12, 2017

Thanks!

@criticalbh

This comment has been minimized.

criticalbh commented Nov 10, 2017

Those are my preferred and working settings:
$ brew install git bash-completion

.bash_profile

c_cyan=tput setaf 6
c_sgr0=tput sgr0

if [ -f $(brew --prefix)/etc/bash_completion ]; then
source $(brew --prefix)/etc/bash_completion.d/git-completion.bash
source $(brew --prefix)/etc/bash_completion.d/git-prompt.sh
fi
GIT_PS1_SHOWDIRTYSTATE=true
PS1='[\033[32m]\u@\h[\033[00m]:[[${c_sgr0}]\w[[${c_cyan}]$(__git_ps1)[\033[00m]$ '

@hunterloftis

This comment has been minimized.

hunterloftis commented Nov 16, 2017

I like your version, @criticalbh, because it's less likely to break on future osx versions.

@agarny

This comment has been minimized.

agarny commented Nov 30, 2017

Here is a solution that doesn't require bash-completion (but still a version of __git_ps1):

Reset="\[\033[0m\]"
Red="\[\033[1;31m\]"
Green="\[\033[1;32m\]"
Blue="\[\033[1;34m\]"

gitPs1()
{
        branch="$(git symbolic-ref HEAD 2> /dev/null)";

        if [ -n "$branch" ]; then
                printf " (%s)" "${branch##refs/heads/}";
        fi
}

export PS1="$Green\u@\h$Reset:$Blue\w$Reset"'$(git branch > /dev/null 2>&1; \
if [ $? -eq 0 ]; then \
        echo "$(echo `git status` | grep "nothing to commit" > /dev/null 2>&1; \
        if [ $? -eq 0 ]; then \
                echo "'$Green'"; \
        else \
                echo "'$Red'"; \
        fi)$(gitPs1 " (%s)")'$Reset'"; \
fi)'"\$ "
@gitfilix

This comment has been minimized.

gitfilix commented Jan 3, 2018

Thanks a lot guys. Homebrew is still THE toolset to make it work.

@davejtoews

This comment has been minimized.

davejtoews commented Mar 13, 2018

With Xcode, on macOS Sierra, the solution posted by @mastbaum above still works. Homebrew is unnecessary.

https://gist.github.com/trey/2722934#gistcomment-940455

@Mohammed8960

This comment has been minimized.

Mohammed8960 commented Apr 19, 2018

Thank you @nicregez, your answer worked for me

@sayfulloev

This comment has been minimized.

sayfulloev commented Jun 9, 2018

For those on macOS High Sierra and without XCode this will work
put it into your /etc/bashrc/

if [ -f $(brew --prefix)/etc/bash_completion ]; then
    source $(brew --prefix)/etc/bash_completion.d/git-completion.bash
    source $(brew --prefix)/etc/bash_completion.d/git-prompt.sh
fi
GIT_PS1_SHOWDIRTYSTATE=true
export PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\[\033[31m\]$(__git_ps1)\[\033[00m\]\$ '

thanks @megahall for the PS1 colors.

@dbadilla-aeropost

This comment has been minimized.

dbadilla-aeropost commented Jul 27, 2018

This wasn't working for me at first. The colors, if set as suggested by @megahall were static, and I wanted red if there were changes (in addition to the star or any other symbol added by git), green if clean, etc.

I opened $(brew --prefix)/etc/bash_completion.d/git-prompt.sh, and in there I found that the colors will only work if using the PROMPT_COMMAND and the GIT_PS1_SHOWCOLORHINTS is set to anything other than empty. Here's how it ended working for me:

brew install git bash-completion

per recommendation when the above command finishes, I added the following line to ~/.bash_profile (though not 100% sure what it does, I believe it makes the bash completion functions available for use locally):

[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion

Then I added these lines:

GIT_PS1_SHOWDIRTYSTATE=true
GIT_PS1_SHOWCOLORHINTS=true

PROMPT_COMMAND='__git_ps1 "\u@local:\W" "\$ "'

@Jujenji

This comment has been minimized.

Jujenji commented Sep 3, 2018

@mastbaum this worked for me, thx

@Sauraus

This comment has been minimized.

Sauraus commented Nov 7, 2018

I've had to re-write the PS1 export to not look really messy with way too many brackets [ / ] oh and the tput commands had to be escaped else Mojave would throw errors on unknown command for setaf / sgr0

c_cyan=$(tput setaf 6)
c_sgr0=$(tput sgr0)

if [ -f $(brew --prefix)/etc/bash_completion ]; then
source $(brew --prefix)/etc/bash_completion.d/git-completion.bash
source $(brew --prefix)/etc/bash_completion.d/git-prompt.sh
fi

GIT_PS1_SHOWDIRTYSTATE=true
GIT_PS1_SHOWCOLORHINTS=true

export PS1='[\033[32m\]\u@\h\[\033[00m]:[${c_sgr0}\w]${c_cyan}$(__git_ps1)\033[00m\]$ '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment