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.
- 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. - Then
git reset --hard {hash}
to restore the repo to where it was before trying todcommit
. - 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. - If the
dcommit
failure was caused by a problem with any of your commits, you can fix it now. - Once you’re ready, just run
git svn dcommit
again.
Thanks so much for this! This helped me clean-up my git repo’s commit history and a returning error that I couldn’t figure out where or how it existed.