Categorized: Apple, Git

Creating a Happy Git Environment on OS X Leopard

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).

Set up Global Git Config on your GitHub account page (the same place you pasted your SSH key). You’ll type in some stuff that looks like this:

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

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

Comments

Trevor → April 12th, 2009 at 5:48 pm

Thank you! This is exactly the information I have been looking for!

Andreas → September 3rd, 2009 at 8:15 am

Hi.

Thanks! This information saved my day!!!

Dave → January 18th, 2010 at 12:42 pm

Thanks Trey – this is nice and compact! Just one thing I found with git version 1.6.6 (installed into /usr/local): I had to add the full / path for my global .gitexcludes file to .gitconfig, whereas 1.6.4 worked with a quoted string of the filename alone.

Chris → March 21st, 2010 at 11:53 pm

Man, you have no idea the troubles that just got solved via your last tip, about leaving out the –global on the email config. I’ve fought the good fight on trying to reconcile “I want gitorious (work address) to show my commits etc.. but I want github updates via my non work address etc….)”. It seems so obvious now :) I know other guys at work who gripe about this same thing and all we had to do was look at that flag! hahahahaahahah that is awesome thanks for posting this here! I have a file full of stuff that I use but I don’t really put anywhere for fear of someone thinking “oh wow you don’t know that” but I bet no matter what it is and how much of an expert you are there are always bash tricks, textmate shortcuts etc that you miss, and stuff like you have here is awesome. I’m going to clean mine out and put it on github to. Thanks Trey!!!

Trey → March 22nd, 2010 at 9:59 am

Chris,

Glad you got some use out of that tip. Yeah, I have given up trying to not look stupid online. :) I like to just throw things out there, and if people think I’m stupid for not knowing it already, c’est la vie.

What do you think about that?

Elsewhere in the empire: Home, Blog, APOD