Useful Git commands – Part 4

Continuing my series of useful Git stuff, here are a few useful one-liners that can make life easier.

Recovering from bad reset

Imagine for instance that you just pulled master into your own branch but realized you should have rebased instead to avoid those pesky merge commits. Imagine also that you wanted to recover by doing a git reset --hard, but you accidentally killed some of your own commits too. It may look like you are screwed now, but all is not lost – git still knows about your commits. Use git reflog to see the commits again and merge them back as needed. If you have just killed a single commit, the easiest way to get it back is with git cherry-pick ORIG_HEAD. Handy.

Setting up proper remote tracking

You are using Arcanist in your project, and created a feature branch with arc branch, then it looks like remote tracking has been set up, but your feature branch is actually tracking master. Probably not what you want. To fix this, run git branch --set-upstream

Subversion to Git

The whole world has not quite converted to Git yet, but it is well on the way. To actually work with a Subversion backend, a standard Git installation includes git-svn which implements an excellent two-way support. If you just want to track a Subversion repository without pushing to it, svn2git gives a better conversion in that it properly converts Subversion tags to real Git tags as well as some other house keeping.

I have used (and contributed to) svn2git before, but not for a while. Now I needed to use it again and realised that it is pretty sensitive to what Git version you have installed. I first tried with the latest Homebrew version (1.8.4) and it failed. I then uninstalled the Homebrew version and tried the Apple supplied one (1.7.10.2) and that failed too. Some Googling told me that version 1.8.3.1 seemed to work all right. So how to get that one? Turns out that is pretty simple nowadays.

  1. cd into the Homebrew folder (normally /usr/local)
  2. Run brew versions git to see all available versions
  3. Copy the checkout command from the list and run it.
  4. Run brew unlink git && brew install git.
  5. Run git checkout with the file path from 2 above.

Voilà! You now have the correct version installed. But wait, there’s more! You can now install several versions of the same program by following the steps above and then switch between them with brew switch <formula> <version>. Very convenient.

Useful git commands – part 2

Sometimes you have a project with lots of submodules that you never change yourself, but want to update occasionally. One example would be if you use Pathogen for your VIM plugins. You’d think you could use git submodule update here, and sometimes you can. But note that one of the main features of submodules is that they are locked to a particular commit. In case you want to pull in the latest changes from the remote, you need something else.

I put this alias in my .gitconfig file to solve the problem (note that this needs to be all on one line):

subup = submodule foreach 'git pull -s recursive 
-X theirs origin master'

The command will cd into each submodule, and do a forced pull overwriting any local changes you may have. Nice, right?

Useful git commands – part 1

If you are like me, you have a whole bunch of projects on your hard disk that you want to update occasionally, but don’t really work actively with. Previously I usually put an update-all.sh file in the root folder of those projects. But in most cases it is possible to use a general approach. I now do it like this:

If you pass a path to the script, it updates all subdirectories under that path, else it uses the current directory. And to make it even more convenient, you can add an alias to your .gitconfig file, like so:

pullall = !~/bin/git-pullall.sh

Now you can just use git pullall to update everything in one go. Convenient!