Open all files from a git diff or git show with this handy command utility

December 25, 2011 in Web Development

We use git as our versioning tool at work and I’ve gradually been learning a few tricks on how to speed up my development time and time spent managing my repo.

When jumping between branches, continuing your work from where you stopped last time, etc., you very often open the same files as you were editing in a previous commit. This may not be a problem if you use something like Command-T for vim or rely on the file browsing in TextMate, but often it might just be quicker to open all the files from a particular ref in git or opening all files from your branch’s diff from master/dev or something.

I made this little snippet of a shell script that allows you to open all files from a git diff or show command in one fell swoop. Usage goes something like:

gitopen diff master

or

gitopen show 3663c033

or simply

gitopen

The command takes two arguments:

gitopen [diff <ref>] [show <ref>]

Where ref could be a hash or a branch name etc.

If you want to try it, you can download it here: https://gist.github.com/gists/1517095/download

Paul Rosnia also made a change which makes the script try to use the $EDITOR environment variable if it exists. You can override that if you like to use a different editor than is set by your environment.

If you find this utility useful, please let me know in the comments.

Update: Jarin Udom pointed out to me on Hacker News the following:

If you add a git alias, like so:

git config --global alias.open '!sh ~/gitopen.sh'

It will allow you to use the command like other git commands:

git open diff master

Apparently you should also be able to name the file git-open somewhere in your path, and that should have the same effect. However, it did not work for me and I have no idea why, but the git global config variable worked great.

  • http://www.mostlymaths.net/ Ruben Berenguel

    It’s nice that it uses $Editor! This means I could use emacs ;) Anyway, I’ve never gone too far with git: there are too many source control systems out there! I was using RCS for a long time for my LaTeX work (until I grew tired). Then tried to use CVS and SVN for a change… Currently I only use SVN for my Google Code small projects… Tried Darcs, too. Didn’t like it that much. And have only used git and mercurial for downloading things. Ah… So many programs!

    Ruben

  • http://minhajuddin.com Khaja Minhajuddin

    Renaming this script to git-open and putting it in your path would allow you to run it as “git open” (which looks much cleaner :) )

  • http://minhajuddin.com Khaja Minhajuddin

    If you are already using vim, Fugitive (https://github.com/tpope/vim-fugitive) is an awesome plugin which allows you to drill into all the git information including diffs.

  • Anonymous

    Arnor — YES found it useful. Been wanting this very command for a long time. Thanks for sharing.