Restoring Lost Commits After a Failed `git svn dcommit`

If you run git svn dcommit to push local Git commits to a Subversion repository, and anything goes wrong (like a pre-commit hook on the server rejecting one of the commits), Git will give up and exit. You’ll be back at your shell prompt, and if you do a git log, you’ll see that any commits before the error was encountered were successfully pushed to the server.

However, the commit that failed, and any after it, will no longer be in your local git log. Instead, their changes will just be sitting there unstaged. If this happens towards the end of a long session, the prospect of re-doing all those commits one-by-one can be daunting, but luckily there’s a way to avoid that.

A quick search turned up an old post from a GNOME contributor with a solution.

  1. Find the hash of the last commit you made before running dcommit. There are several ways to do this, but the most reliable is by opening .git/logs/HEAD and looking for the last commit message towards the bottom. The line with the commit message will have two hashes at the begining. The first hash it the ref for the previous commit, and the second hash is the one that corresponds to the commit message on that line.
  2. Then git reset --hard {hash} to restore the repo to where it was before trying to dcommit.
  3. Then git svn rebase so that git-svn will recognize if any of the commits were successfully pushed to the server and not try to re-push them.
  4. If the dcommit failure was caused by a problem with any of your commits, you can fix it now.
  5. Once you’re ready, just run git svn dcommit again.

One thought on “Restoring Lost Commits After a Failed `git svn dcommit`

Leave a Reply to Daan van den Bergh Cancel reply

Your email address will not be published. Required fields are marked *