Is there a Subversion command to reset the working copy?

前端 未结 9 1530
时光说笑
时光说笑 2020-12-23 02:25

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

相关标签:
9条回答
  • 2020-12-23 03:03

    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) ;)'
    
    0 讨论(0)
  • 2020-12-23 03:06

    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
    
    0 讨论(0)
  • 2020-12-23 03:08

    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)

    0 讨论(0)
  • 2020-12-23 03:14

    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.

    0 讨论(0)
  • 2020-12-23 03:17

    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.

    0 讨论(0)
  • 2020-12-23 03:19

    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
    
    0 讨论(0)
提交回复
热议问题