git - 10 tips and tricks

A while ago I gave a short talk at our office in Frankfurt. I talked about 10 git tips that come in handy when doing version control with git.

1. Command autocompletion

To enable autocompletion for git commands, all you have to do is download this shell script from github and activate it in your .bash_profile or .bashrc file.

$ curl -O https://raw.github.com/git/git/master/contrib/completion/git-completion.bash
$ mv git-completion.bash .git-completion.bash
if [ -f ~/.git-completion.bash ]; then
. ~/.git-completion.bash
fi

After logging out and in again you should be able to autocomplete git commands by hitting the tab key.

2. Pimp my git log

The git log command offers some neat options to customize the output.

--autor="Sebastian"
Only show commits made by a certain author
--name-only
Only show names of files that changed
--oneline
Show commit data compressed to one line
--graph
Show dependency tree for all commits
--reverse
Show commits in reverse order (Oldest commit first)
--after
Show all commits that happened after certain date
--before
Show all commits that happened before certain data

Example:

$ git log --author="Sebastian" --after="1 week ago" --oneline

You can use the regular less commands to search the log output. Use lower case n to navigate to the next occurrence and upper case N to the previous one.

/{{your-search-here}}

3. Reset files

Return to a previous version and discard all changed

$ git reset --hard {{some-commit}}

Return to a previous version, changes are unstaged

$ git reset {{some-commit}}

Return to a previous version, changes are staged

$ git reset --soft {{some-commit}}
reset --hard HEAD
I want to forget all the changes I’ve made, clean start
reset
I want to edit, re-stage and re-commit files in some different order
reset --soft
I just want to re commit past 3 commits, as one big commit

4. Ignore whitespace

You can easily ignore whitespace for diff and blame.

$ git diff -w
$ git blame -w

5. Doing partial adds

If you made multiple changes to a file and you want to commit them separately, you can do so with the -p flag.

$ git add -p

6. Use OS aliases

You can add some useful aliases to your .bash_profile or .bashrc file.

# I’m lazy as hell
alias g='git'

# git status in a flash
alias gs='git status’
alias gss='git status -s’

# go to repository root directory
alias gr='[! -z `git rev-parse --show-cdup`] && cd `git rev-parse --show-cdup || pwd`'

The last one is particularly useful, from anywhere in your repository it brings you right back to the root directory of your repository simply by typing gr.

7. Use git aliases

In addition to system aliases git also supports aliases for git commands. You can add those by editing your git configuration file ~/.gitconfig or by running a git command.

$ git config --global alias.unstage "reset HEAD"
$ git config --global alias.wtf "log -p"
$ git config --global alias.l "log --oneline --graph"
$ git config --global alias.b "branch"
$ git config --global alias.c "commit"
$ git config --global alias.p "pull —rebase"

8. Using rebase

Git rebase is a great tool to keep your git history clean when working on shared branches. If you merge a shared branch you end up with merge commits and log entries like:

Merge remote-tracking branch ‘origin/master’

This is no big deal and completely safe, but it still messes up the log history a bit.

$ git pull --rebase

Using rebase will basically put your commits on a shelve, merge the remote changes and then reapply your commits. This way your git history won’t show any merge commits. Rebasing is very powerful but should be used with caution. The git book states it like this.

Ahh, but the bliss of rebasing isn’t without its drawbacks, which can be summed up in a single line:
Do not rebase commits that exist outside your repository.
If you follow that guideline, you’ll be fine. If you don’t, people will hate you, and you’ll be scorned by friends and family.

-- git book

9. Using amend

Let’s say you made a commit and realize you made a typo. If you fix the typo and run the following commands:

$ git add path/toFile/withFixedTypo.php
$ git commit --amend

Git will undo and redo your last commit including the added changes.

10. Using git bisect

Let’s say someone broke your code but there where a lot off commits and a lot of changes. Using git bisect finds commits that break your code fast and easy.

$ git bisect start
$ git bisect good {{some-commit}}
$ git bisect bad HEAD

This commands will checkout a commit in the middle of {{some-commit}} and HEAD. You then have to mark this commit as good or bad.

$ git bisect good|bad

This will continue the search by checking out the next commit depending on your answer. If you identified the commit that broke your code run.

$ git bisect reset
$ git bisect log

Running the commands above will get you back to your starting point and print out a summary report for your executed git bisect run.

This is my top 10 of git tips and tricks and I hope there was something useful for you. You can download the slides for this talk here.

What are your git secrets? You can share them in the comments below.

Leave a Reply

Your email address will not be published. Required fields are marked *