Skip to content

Instantly share code, notes, and snippets.

@trey
Last active September 19, 2024 16:23
Show Gist options
  • Save trey/2722934 to your computer and use it in GitHub Desktop.
Save trey/2722934 to your computer and use it in GitHub Desktop.
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

@ivana4matic
Copy link

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

@nicregez
Copy link

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
Copy link

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

@megahall
Copy link

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
Copy link

leobeal commented Oct 5, 2017

@megahall nice colours! thanks!

@ferantivero
Copy link

@megahall it looks good! Thanks,

@criticalbh
Copy link

Thanks!

@criticalbh
Copy link

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
Copy link

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

@agarny
Copy link

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
Copy link

gitfilix commented Jan 3, 2018

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

@davejtoews
Copy link

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
Copy link

Thank you @nicregez, your answer worked for me

@sayfulloev
Copy link

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
Copy link

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
Copy link

Jujenji commented Sep 3, 2018

@mastbaum this worked for me, thx

@Sauraus
Copy link

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\]$ '

@chrisbergeron
Copy link

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

Those extra brackets are there as place holders to keep the line intact when you up/down arrow through command history. I wouldn't remove them, they will foul up your prompt alignment.

@wodge73
Copy link

wodge73 commented Apr 4, 2019

@dbadilla-aeropost This worked for me also. Thank you very much, I have wanted to get this working for a long time!

@hstarikov-coursera
Copy link

Great guide!
I'd like to share another git must-have for me: diff-so-fancy

Good-lookin' diffs. Actually… nah… The best-lookin' diffs. 🎉

image

@mwtzzz-zz
Copy link

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

same here

@mwtzzz-zz
Copy link

GIT_PS1_SHOWDIRTYSTATE=true
GIT_PS1_SHOWCOLORHINTS=true

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

These also were needed in my case for the colors to work. Thanks!

@charsi
Copy link

charsi commented Dec 30, 2022

I also had to also add this to ~/.bash_profile

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

This is needed because git now splits the script in two.
Ref :

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment