Skip to content

Instantly share code, notes, and snippets.

@rpullinger
Last active December 17, 2015 19:39
Show Gist options
  • Save rpullinger/5661631 to your computer and use it in GitHub Desktop.
Save rpullinger/5661631 to your computer and use it in GitHub Desktop.
Some helpful stuff for Git

Initial Setup

Theses are things that you run once. The log format and PS1 are my personal preferences so may need changing up for your needs.

Colors in git ui

git config --global color.ui true

Make git log prettier

git config --global format.pretty "%C(white dim) %h %C(yellow) %ar %C(cyan) %an %Creset %s"

Add Branch to your command prompt

Add this function to your ~/.bash_profile

__git_ps1 ()
{
    local b="$(git symbolic-ref HEAD 2>/dev/null)";
    if [ -n "$b" ]; then
        printf " (%s)" "${b##refs/heads/}";
    fi
}

You can then use this when you set your PS1 prompt value.

export PS1='\[\033[0;30m\]\u@$REALNAME\[\033[00m\]:\[\033[01;37m\]\w \[\033[0;32m\]$(__git_ps1)\[\033[00m\]\$ '

Set up autocomplete within git

curl https://raw.github.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash

Then add this to your ~/.bash_profile

if [ -f ~/.git-completion.bash ]; then
  . ~/.git-completion.bash
fi

Global Git ignore

This allows to to automaticaly never track any of these files. Create a file ~/.gitignore_global and add the following as a basic starter. This should stop most of the files that os's add, SASS cache and rogue SVN files.

.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
Icon?
ehthumbs.db
Thumbs.db
.sass-cache
.svn

Save then run this command

git config --global core.excludesfile ~/.gitignore_global

Autocomplete typing mistakes

git config --global help.autocorrect 1

Useful Commands

Pretty self explanatory but these are some useful commands I use everyday.

Checkout a new branch - shorthand

git checkout -b BRANCHNAME

Rename a branch

git branch -m OLDBRANCH NEWBRANCH

Amend the last commit

Don't if the commit is public

git commit --amend

Search the logs

git log | grep "NEEDLE"

Simpler status messages

git status -sb

Show commit that removed a file

git log -1 -- PATH/TO/FILE

Revert a file to the last commit

git checkout -- PATH/TO/FILE

Resources

Hat Tips

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