ChatGPT解决这个技术问题 Extra ChatGPT

Is there a Subversion command to reset the working copy?

svn

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.)

reverting all changes in the working copy?
You'd use git clean -xdf to remove things that aren't under source control.
@vcsjones: Thanks, the man page for git clean quotes exactly what I am looking for: “This can be used (possibly in conjunction with git reset) to create a pristine working directory to test a clean build.” Pity that Subversion does not seem to have anything like this built-in.

C
Community

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: 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.


You might want to run svn cleanup on the wc first as well. This is my standard practice for cleaning up after aborted merges and whatnot.
tortoise also has an option "remove unversioned files" in the cleanup dialog.
It would be nice if the "remove unversioned files" was available via command line. Especially for automated builds.
See my answer... --remove-unversioned exists for svn cleanup
P
Peter Mortensen

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:

Only wholly-untracked directories will match the pattern passed to rm 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) ;)'

the 2nd snippet does not seem to work for me: awk: fatal: error reading input file '-': Input/output error
I had to escape the ? in the awk command, otherwise it matched all lines in the output of svn status
P
Peter Mortensen

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.


much faster is svn cleanup --remove-unversioned .
D
Denny Weinberg

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

this is the best answer, without additional scripting. this is universal and compatible with many svn versions.
I needed/wanted svn cleanup --remove-ignored too. Still - best answer here.
A
Abe Voelker
svn revert . -R

to reset everything.

svn revert path/to/file

for a single file


That does not delete extra unversioned files in the working copy, does it?
@zoul I don't think so, but don't quote me on that since it's been awhile since I used SVN
unfortunately it does not touch not tracked files. try svn revert -R your_local_svn_dir_path
P
Peter Mortensen

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

佚名

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


Painfully slow. That’s why I was looking for a way around this.
u
user1299374

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


This is so awful. What about Windows users? Subversion obviously needs a bit of work before it can be taken seriously as a version control tool.
C
Community

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)