5 January 2016 by Lars
How to revert an accidental Subversion commit
Occasionally, you may have accidentally committed something you don’t want to a Subversion repository (possibly with a Trac or Redmine frontend). Here’s some notes on how to back those changes out. Note that in Git, you may be able to amend a commit. This is generally not feasible in Subversion, thus this post.
This assumes that only the latest revisions need to be removed (you are reverting to an earlier state of the SVN database). Assumes use of the ‘fsfs’ type of repository (which is the default). $REV
is the bad revision number, $REVPATH
is the affected path (i.e., repos/branches/ticketXYZ/programs/
)
- (optional: avoid confusion of any working copies)svn revert -r $((REV-1))
- Shut down the Trac and Apache serverOn SUSE: /etc/init.d/apache2 stop
- Make a backup copy of your repository:rsync -auvz /path/to/repos/ /temporary/repos.bak
- Identify the revisions you want to removeREVS=”9098 9099”
- Remove the revision diffs and revision properties:for arg in $REVS
do
rm /path/to/repos/db/revs/$arg
rm /path/to/repos/db/revprops/$arg
done - Remove the “current” copy of the DBrm /path/to/repos/db/current
- Rebuild “current” copysvnadmin recover /path/to/repos
This completes the SVN related steps. In order to not confuse Trac or Redmine, the following steps are needed. They add back the correct number of commits, but with different content. This is best done on the repository host itself, with Apache still turned off
- Turn off the SVN hooks (so no additional commits get sent to Trac)mv /path/to/repos/hooks/post-commit \
/path/to/repos/hooks/post-commit.inactive - Check out the most recent commit in the affected branch.svn co file:///path/to/repos/$REVPATH
- Make a dummy change (this will be r9098)echo “/* change 1 */” >> $REVPATH/correction
- Commit the dummy changesvn add $REVPATH/correction
svn ci -m ‘Dummy correction’ - Add as many changes as needed to make up the number of affected revisions. You most likely want these to be neutral (i.e., at the end of all the dummy changes, the net changes are zero)svn rm $REVPATH/correction
svn ci -m ‘Dummy correction’ - (Optional) you can rebuild (resync) the Trac-to-SVN link. This will purge any commit messages from the old revisions, and insert the new ones. This is not strictly necessary (unless the commit message itself contains confidential information).
- Put the hook back into placemv /path/to/repos/hooks/post-commit.inactive /path/to/repos/hooks/post-commit
- Restart ApacheOn SUSE: /etc/init.d/apache2 start