【问题标题】:How to discard local changes in an SVN checkout?如何丢弃 SVN 结帐中的本地更改?
【发布时间】:2012-03-16 20:20:52
【问题描述】:

我想为一个开源项目提交 diff 以供审核。

我使用 SVN(来自终端,Ubuntu)获得了代码。我在几个文件中做了一些小的编辑。现在我只想提交一个更改。我所做的其余更改用于调试,不再需要。

我使用svn di > ~/os/firstdiff.diff 生成了差异

所以我的问题是,如何丢弃我的本地更改?

有 SVN 方法吗?如果没有,我将不得不去每个文件并删除我所有的编辑。 然后我会生成一个新的差异,并提交它。

【问题讨论】:

    标签: svn


    【解决方案1】:

    只需使用svn revert 命令,例如:

    svn revert some_file.php
    

    它(与所有其他 svn 命令一样)在 svnbook resource 或手册页中,甚至在 svn help 命令中都有详细记录。

    【讨论】:

    • 除了 Vinayak 可能想为自己保留本地无趣的更改。
    • @BasileStarynkevitch :我再次阅读了这个问题,看起来不需要其他更改:“”“我所做的其余更改基本上用于调试,不再需要.""" :)
    • 是的……但他可能想为自己进一步保留它们……(他的问题可能被理解为双向)。
    【解决方案2】:

    您需要使用svn revert 命令恢复所有更改:

    • 恢复对文件的更改:svn revert foo.c
    • 还原整个目录的文件:svn revert --recursive .

    【讨论】:

    • 对 svn 1.1 的引用有点老了,尽管它是第一个链接 given by google ;)
    • 显然您文件名中的某些字符可能会被忽略。例如图标@2x.png。 svn 还原图标@2x.png。 svn 说跳过“图标”。
    • 这是最好的答案。应该检查一下。
    • svn revert --recursive . 帮助了我很多
    • 这应该是公认的答案,因为--recursive 参数会还原任何本地更改,这就是问题所在。
    【解决方案3】:

    放弃一个特定文件中的本地更改:

    $ svn revert example_directory/example_file.txt
    

    放弃一个特定文件夹中的本地更改:

    $ svn revert -R example_directory/
    

    【讨论】:

      【解决方案4】:

      简单的svn revert 对原始海报来说已经足够了。然而,一个简单的svn revert 不会在你的更一般的情况下做

      1. 同一个文件中拥有您想要共享的编辑和您不想共享的编辑
      2. 您仍然有兴趣为自己的私人利益保留的本地更改

      @ErichBSchulz 建议使用git add -p 是非常合理的,并且在这种情况下更普遍适用。答案只是缺少一些细节。假设您的当前目录是您要制作可共享补丁的目录,您可以执行以下操作:

      1. 将原始版本从 subversion 检出到不同的目录(选择一个不存在的目录名,这里使用子目录TMP/)。

        $ url=$(svn info . | awk '/^URL/ {print $2}')
        $ svn checkout "$url" TMP
        
      2. 使用原始的 svn checkout 作为基础,初始化一个 git 存储库,忽略 .svn 目录;将 svn head 中的所有内容提交到您的临时 git 存储库

        $ cd TMP                                                     
        $ git init && echo ".svn/" > .gitignore
        $ git add -A && git commit
        $ cd ..
        
      3. 将新准备的git仓库元数据复制到你原来的工作目录;由于不需要原始的 subversion checkout 目录,您可以摆脱它。你的工作目录现在是 git 和 subversion 存储库:

        $ mv TMP/.git .
        $ rm -rf TMP/
        
      4. 您现在可以使用功能强大且方便的git add -p 以交互方式准确地选择您想要共享的内容,并将它们提交到您的 git 存储库。如果您需要将文件添加到提交中,请在 git commit 之前执行 git add <file-to-add>

        $ git add -p                            
        <interactively select (and edit) the chunks you want to share>
        $ git add ${the_list_of_files_not_in_yet_in_svn_you_want_to_add}
        $ git commit
        
      5. 使用提交,准备一个您想要共享的补丁。为此,您还可以使用git diff HEAD^..HEAD,或git format-patch(后者可用于直接准备要发送的包含补丁或多个补丁的电子邮件):

        $ git show -p HEAD > my-mighty-patch.patch
        

      要摆脱 git 元数据,只需执行 rm -rf .git/。如果您打算继续使用原始工作目录进行黑客攻击,您可以继续使用git 来管理您的本地更改。在这种情况下,您可能会从学习如何使用git svn 的投资中受益。

      注意:如果您熟悉git,那么即兴创作是相当微不足道的事情。否则这看起来可能有点乱。您可以通过从这些步骤中编写脚本来为 svn 实现“交互式提交”或“交互式补丁创建”来概括该方法,无需了解 git 即可使用。

      【讨论】:

      • 非常感谢匿名 -1 没有任何解释。无论如何,我花时间通过澄清这种方法何时有意义来改进答案。
      • 非常感谢您的扩展。 +1 来自我!别担心。艰难的人群。
      • 感谢您的赞许,@ErichBSchulz!尽管有对手,我们正在慢慢地让世界变得更美好!!! ;-)
      【解决方案5】:

      你可以使用

       svn diff important/file1.c important/file2.c > $HOME/os/firstdiff.diff
      

      在发布你的差异时,不要忘记告诉你正在比较的修订版本。

      正如其他人所说,您也可以谨慎使用svn revert。这取决于您是否要为将来的工作保留本地更改...

      【讨论】:

      • +1 这是解决问题的最佳方案,但在这种情况下,我真的很想摆脱我的所有更改。当然,我不想一直 revert 我的更改。所以我以后会用你的方式。
      【解决方案6】:

      转到该存储库的根目录并运行以下命令

      svn revert --depth=infinity .
      

      【讨论】:

      • svn revert -R . 可能会更短
      【解决方案7】:

      我意识到这是一个旧查询,但是..。现在可以轻松完成:

      svn revert -R --remove-added /path/to/dir && svn cleanup --remove-unversioned /path/to/dir
      

      第一部分递归恢复更改,并删除添加但未提交的文件。

      第二部分删除路径中所有未版本化或忽略的项目。

      上述信息的最佳来源。我知道的是 svn 客户端本身:

      ❯ svn help revert
      revert: Restore pristine working copy state (undo local changes).
      usage: revert PATH...
      
        Revert changes in the working copy at or within PATH, and remove
        conflict markers as well, if any.
      
        This subcommand does not revert already committed changes.
        For information about undoing already committed changes, search
        the output of 'svn help merge' for 'undo'.
      
      Valid options:
        --targets ARG            : pass contents of file ARG as additional args
        -R [--recursive]         : descend recursively, same as --depth=infinity
        --depth ARG              : limit operation by depth ARG ('empty', 'files',
                                   'immediates', or 'infinity')
        -q [--quiet]             : print nothing, or only summary information
        --changelist [--cl] ARG  : operate only on members of changelist ARG
        --remove-added           : reverting an added item will remove it from disk
      
      ❯ svn help cleanup
      cleanup: Either recover from an interrupted operation that left the working copy locked,
      or remove unwanted files.
      usage: 1. cleanup [WCPATH...]
             2. cleanup --remove-unversioned [WCPATH...]
                cleanup --remove-ignored [WCPATH...]
             3. cleanup --vacuum-pristines [WCPATH...]
      
        1. When none of the options --remove-unversioned, --remove-ignored, and
          --vacuum-pristines is specified, remove all write locks (shown as 'L' by
          the 'svn status' command) from the working copy.  Usually, this is only
          necessary if a Subversion client has crashed while using the working copy,
          leaving it in an unusable state.
      
          WARNING: There is no mechanism that will protect write locks still
                   being used by other Subversion clients. Running this command
                   without any options while another client is using the working
                   copy can corrupt the working copy beyond repair!
      
        2. If the --remove-unversioned option or the --remove-ignored option
          is given, remove any unversioned or ignored items within WCPATH.
          Note that the 'svn status' command shows unversioned items as '?',
          and ignored items as 'I' if the --no-ignore option is given to it.
      
        3. If the --vacuum-pristines option is given, remove pristine copies of
          files which are stored inside the .svn directory and which are no longer
          referenced by any file in the working copy.
      

      【讨论】:

        【解决方案8】:

        你可以对你想放的文件使用commit命令,使用svn revert命令丢弃剩余的本地修改

        【讨论】:

          猜你喜欢
          • 2017-04-22
          • 2022-09-29
          • 2015-09-24
          • 2021-09-20
          • 1970-01-01
          • 2015-03-08
          • 1970-01-01
          • 2014-03-31
          • 1970-01-01
          相关资源
          最近更新 更多