Is there a Subversion command to reset the working copy?

Svn

Svn Problem Overview


Is there a single Subversion command that would “reset” a working copy exactly to the state that’s stored in the repository? Something like git reset --hard or (ha, hard Git reset does not remove unversioned files either!) rm -rf wc && svn co <url> wc.

Update: I’m not after a simple revert, as that does not delete extra files in the working copy. I really want something that would be the same as deleting the working copy and checking it out again, only without having to download the data again. (Obviously I don’t mind losing all the uncommitted changes.)

Svn Solutions


Solution 1 - Svn

You can recursively revert like this:

svn revert --recursive .

There is no way (without writing a creative script) to remove things that aren't under source control. I think the closest you could do is to iterate over all of the files, use then grep the result of svn list, and if the grep fails, then delete it.

EDIT: The solution for the creative script is here: https://stackoverflow.com/questions/239340/automatically-remove-subversion-unversioned-files

So you could create a script that combines a revert with whichever answer in the linked question suits you best.

Solution 2 - Svn

To revert tracked files

svn revert . -R

To clean untracked files

svn status | rm -rf $(awk '/^?/{$1 = ""; print $0}')

The -rf may/should look scary at first, but once understood it will not be for these reasons:

  1. Only wholly-untracked directories will match the pattern passed to rm
  2. The -rf is required, else these directories will not be removed

To revert then clean (the OP question)

svn revert . -R && svn status | rm -rf $(awk '/^?/{$1 = ""; print $0}')

For consistent ease of use

Add permanent alias to your .bash_aliases

alias svn.HardReset='read -p "destroy all local changes?[y/N]" && [[ $REPLY =~ ^[yY] ]] && svn revert . -R && rm -rf $(awk -f <(echo "/^?/{print \$2}") <(svn status) ;)'

Solution 3 - Svn

Delete everything inside your local copy using:

rm -r your_local_svn_dir_path/*

And the revert everything recursively using the below command.

svn revert -R your_local_svn_dir_path

This is way faster than deleting the entire directory and then taking a fresh checkout, because the files are being restored from you local SVN meta data. It doesn't even need a network connection.

Solution 4 - Svn

Delete unversioned files and revert any changes:

svn revert D:\tmp\sql -R
svn cleanup D:\tmp\sql --remove-unversioned

Out:

D         D:\tmp\sql\update\abc.txt

Solution 5 - Svn

svn revert . -R

to reset everything.

svn revert path/to/file

for a single file

Solution 6 - Svn

Pure Windows cmd/bat solution:

svn cleanup .
svn revert -R .
For /f "tokens=1,2" %%A in ('svn status --no-ignore') Do (
     If [%%A]==[?] ( Call :UniDelete %%B
     ) Else If [%%A]==[I] Call :UniDelete %%B
   )
svn update .
goto :eof

:UniDelete delete file/dir
IF EXIST "%1\*" (
    RD /S /Q "%1"
) Else (
    If EXIST "%1" DEL /S /F /Q "%1"
)
goto :eof

Solution 7 - Svn

Delete the working copy from the OS and check it out again is simplest, but obviously not a single command.

Solution 8 - Svn

Very quick and simple and does exactly what you want

svn status | awk '{if($2 !~ /(config|\.ini)/ && !system("test -e \"" $2 "\"")) {print $2; system("rm -Rf \"" $2 "\"");}}'

The /(config|.ini)/ is for my own purposes.

And might be a good idea to add --no-ignore to the svn command

Solution 9 - Svn

To remove untracked files

I was able to list all untracked files reported by svn st in bash by doing:

echo $(svn st | grep -P "^\?" | cut -c 9-)

If you are feeling lucky, you could replace echo with rm to delete untracked files. Or copy the files you want to delete by hand, if you are feeling a less lucky.


(I used @abe-voelker 's answer to revert the remaining files: https://stackoverflow.com/a/6204601/1695680)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionzoulView Question on Stackoverflow
Solution 1 - SvnvcsjonesView Answer on Stackoverflow
Solution 2 - SvnniklauzgView Answer on Stackoverflow
Solution 3 - SvnVivek YadavView Answer on Stackoverflow
Solution 4 - SvnDenny WeinbergView Answer on Stackoverflow
Solution 5 - SvnAbe VoelkerView Answer on Stackoverflow
Solution 6 - SvnAndriy F.View Answer on Stackoverflow
Solution 7 - Svnuser2100815View Answer on Stackoverflow
Solution 8 - Svnuser1299374View Answer on Stackoverflow
Solution 9 - SvnThorSummonerView Answer on Stackoverflow