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

@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