Git Tips You Probably Didn’t Know

I’ve been using `git` for quite a while now and some of it’s features continue to amaze me. Here’s a few things I learned recently.

I’ve been using git for quite a while now and some of it’s features continue to amaze me. Here’s a few things I learned recently.

Finding only your changes to master.

When you’ve made a change against a master that’s moving often, you find that simply doing git diff master doesn’t give the right diff. It shows you the difference between your branch and current master. That’s not what you want in most cases. The master branch would have changed and now the command also shows those changes. GitHub does the right thing and the right command in this case is the following:

git diff master... 

Copying your changes in one branch into another

Recently, our designer Sam Smith started working on making CKAN more responsive. His Branch was based off master and I wanted to make a new branch based off release-v2.2 with his changes on top of it. My instinct was to make a patch.

git diff master...responsive > ../responsive.patch 

Then, apply the patch onto a different branch. This would surely work, but I’m using a version control system! It should be smart about this. The good folks in #git pointed me in the right direction.

First find the last commit from the responsive branch that you don’t want to copy.

git checkout responsive git log master..responsive 

The last commit in that log is what you want to copy. In this case, it’s 7587c6e8fe49c809ef7357b6f88496bd06ac93b9, so now you want to do git log 7587c6e8fe49c809ef7357b6f88496bd06ac93b9^. The first commit is the one you don’t want to keep.

git checkout responsive git checkout -b reponsive-2.2 git rebase 7587c6e8fe49c809ef7357b6f88496bd06ac93b9 --onto release-2.2 

Thus, responsive-2.2 is a new branch with responsive changes based on top of release-v2.2!

The more you know

Git and Bash Love

I’ve recently been working with git a lot, more specifically with branches. I love git’s branching and its fairly awesome. However, its also easy to forget which branch you’re currently working on (especially awful to find that the commits you did were to master :/). Sometime back I did some extensive searching to fix that situation and finally stumbled upon a neat way to fix it. I modified the PS1 variable to show the current branch! Here’s what I added to the .bashrc file in Ubuntu 🙂

function parse_git_branch {     ref=$(git symbolic-ref HEAD 2> /dev/null) || return     echo "("${ref#refs/heads/}")" }  RED="[33[0;31m]" YELLOW="[33[0;33m]" GREEN="[33[0;32m]"  PS1="$PS1$RED$(date +{13371f13f0bf161e7595c2ac5df92e005bed3de1d132ef646d0a44f3a1a9ee62}H:{13371f13f0bf161e7595c2ac5df92e005bed3de1d132ef646d0a44f3a1a9ee62}M) w$YELLOW $(parse_git_branch)$GREEN$[33[0m] " 

Web-based viewer for git

I’ve been looking for a simple to set up viewer for git that would list out all the git repositories that I have on my git server. It took a bit of a search and I finally discovered viewgit. So far, I’ve found it pretty nice. Setting up viewgit is very simple.

Download the tarball from sourceforge. Extract it to your htdocs or www folder. Make a copy of example-localconfig.php and rename it as localconfig.php. Comment out the line$conf['projects'] and uncomment $conf['projects_glob'] = array ('/path/to/repo/);. If like me, you store all your git repos in one folder, this is the best viewer for the amount of work I’ve had to do. Maybe I should write about how to store git repos in one folder. A story for tomorrow!