More git goodies

Posted by craig

If you are in a subdirectory of your project and want to know where the .git directory lives above you:

git rev-parse --git-dir

My coworker Guillermo Castro (aka the JavaGeek) found this one by pure necessity. He was told recently that a bunch of related changes which had been checked in over a series of separate commits needed to be merged into an emergency patch branch for production. No problem he thought, git cherry-pick to the rescue! Not so fast. For 90% of the cases, yes he could just cherry-pick an entire commit into the patch branch. But there was that 10% of the cases where few files that were supposed to be part of the patch which were checked in to commits that contained changes to files which were not to be included in the branch. Here is how he solved this problem:

* he knew that the files he was interested resided under two directories * he knew the sha1 commit hash for the commit that contained changes to the files he was interested in (but also had commits to other files he wasn’t) * he wanted only the changes to files under the two directories to be part of his commit.
git log -1 -p <commit hash> <paths or file names>

The “-1” tells git to only prepare a patch file for the files under the paths specified based on the first commit (which is the one you specified in the param.

Along with the above, he also found a way to cherry-pick commits without actually committing them locally (i.e. just keep the changes staged)
git cherry-pick --no-commit <commits>

Also, if you are on a Mac, there is a nice version of gitk called “gitx” I highly recommend you grab. It’s “pretty” and has some neat features which you can find in the screencasts section of the site.


git gotcha

Posted by craig

Just thought I’d post about a problem I was having with git concerning the setup and cloning of a remote repository. Basically, if you’ve setup a repository on a remote machine, and want to clone from that machine onto a local one using ssh, you may run into problems like I did.

The error I received was:

ThaDonMBP:workspace craiger$ git clone craiger@192.168.2.10:/git/myproj
Initialized empty Git repository in /private/workspace/myproj/.git
craiger@192.168.2.10's password: 
bash: git-upload-pack: command not found
fatal: The remote end hung up unexpectedly

The problem is that git is expecting git-upload-pack in a specific directory on the remote machine, and that is /usr/bin or /usr/local/bin. That’s not where I had git installed, I had it installed in /opt/git/bin. So all you need to do is symlink your git executables to /usr/bin like so:

craiger@192.168.2.10:~$ cd /usr/bin
craiger@192.168.2.10:/usr/bin$ sudo ln -s /opt/git/bin/* .

After that, things should work!