You might have seen it â @kkuchta christmas tree gif making the rounds these past holidays:
For a short story, that sums up my early experience with git with surprisingly accuracy!
For anyone else whoâs struggled with git over the years, hereâs a bit of my process to help me stay sane with git:
1. Where I am in the tree?
I use the command line to make new commits, pull, merge, etc, and when I first started using git I had a lot of trouble understanding just where I was in the repo at any given time. After branching a few times, Iâd get lost pretty easily and forget which branch I was currently on. A simple fix â I added the following line to my .bash_profile:
# Put Git branch name it shell prompt
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
PS1='\W$(parse_git_branch)\$ '
`
This simply printed out my current location in my consoleâs prompt:
loose-leaf(master)$
Now I felt comfortable git add branch-nameing and git checkouting as often as I needed without getting lost.
2. Where I can go in the tree?
Now that I knew my location in the tree, I still wasnât sure where I was in relation to all the other branches. For that, I use a GUI app called GitX-dev: http://rowanj.github.io/gitx/. Itâs a fantasic and incredibly simple app that shows the history, branches, and tags of your git repo.
Itâs incredibly handy for easily seeing at a glance where I am in relation to all of the other branches, both local and remote.
3. Where is the remote?
Speaking of remote â thereâs been more than once where Iâve worked on a fork of a primary repo, so I needed to keep track of both the upstream original repo and the fork repo.
For that, two simple commands help me know whatâs what:
# lists all of the remote repositories and their names
get remote -v
and
# fetches all of the branches and tags from the remote repo
git fetch remote-name
These two commands let me know if my local branch was still simply an ancestor of the remote branch â if so, I could git pull without fear of an ugly surprise merge.
If my local branch had diverged from the remote, then I could look through the recent commits for that remote branch in GitX, and then decide if I wanted to a) git rebase my local changes onto the end of the remote branch with:
git rebase branch-name remote-name/branch-name
or b) have more context going into the merge with git pull.
4. Where is my bug?!
Hereâs the real gem that Iâve only recently started using to great effect:Â git bisect.
Git bisect is a magical tool that will help you find the exact commit that caused the bug youâre trying to track down. Simply put, it does a binary search through your commit history until it finds the exact commit where the bug appeared.
Assuming your repo is currently showing your bug, you can start the process with a simple:
$ git bisect bad
You need to start by "git bisect start"
Do you want me to do it for you [Y/n]? Y
Confirm the prompt with a Y, and then checkout a commit that you know doesnât show the bug:
$ git checkout old-branch-that-worked
And then mark that bugless commit as the 'good' commit.
$ git bisect good
And now the magic has started! Youâll see something like the following show up soon:
Bisecting: 4 revisions left to test after this (roughly 2 steps)
[uglycommithash] an old commit message
Git has started its binary search through your tree, and all you have to do now is tell it whether the bug shows up in this commit with git bisect bad or if itâs bug free with git bisect good. Just keep going until you finally see:
ugly-commit-hash is the first bad commit
commit ugly-commit-hash
Author: Definitely Not You
And thatâs it! Now you know precisely where the bug is and have a much better chance of tracking down its root cause. At this point I usually git branch bug-name just so I have a temporary flag in GitX for me to look for.
Note: At the end of your git bisect, even though itâs shown you the offending commit, youâll technically still be in bisect mode.
# at this point after a bisect, your git status will be:
$ git status
HEAD detached at 4751a0a
You are currently bisecting, started from branch 'master'.
(use "git bisect reset" to get back to the original branch)
To return to git business as usual, simply:
$ git bisect reset
And Tada! Youâre back at your original branch as if nothing had happened. Itâs an absolutely invaluable way to quickly narrow down a tricky to find bug.
A little goes a long way
This writeup barely scratches the surface of whatâs possible in git â thereâs huge swaths of functionality I havenât even touched on â but if I had to choose only a handful of features, this would be it. This is 99% of what I use when Iâm interacting with a repo, and I wish Iâd seen it when I first made the switch to git many years ago.

