当对存储在Subversion repo中的软件进行编程时,我经常修改一些文件,然后注意到我想对我的主要工作做一些准备性更改。例如,在实现新功能时,我注意到一些重构可能会对我有所帮助。

为了不将两个不相关的更改混合在一起,在这种情况下,我想“收起”我的更改,即恢复到存储库版本,做一些其他更改,提交这些更改,然后“取回”我的更改。

Git-stash允许这样做。有没有办法在Subversion中直接或通过一些插件或脚本来做到这一点?Eclipse插件也可以。


当前回答

另一种选择是将当前签出复制到一个新目录并恢复所有更改。这样就省去了在服务器上创建临时分支的麻烦——毕竟存储是一个本地操作,不是每个人都应该看到,而且可以经常这样做。

提交修复程序后,您可以更新主工作副本并删除“存储区域”

其他回答

Use:

svn cp --parents . ^/trash-stash/my-stash

它将从当前位置和当前修订创建一个分支,然后将工作副本中的更改提交给该分支,而不切换到该分支。

复制SRC[@REV]…DST SRC和DST都可以是工作副本(WC)路径或URL: WC -> URL:立即将WC副本提交到URL

注意,工作副本中的更改不会自动恢复(cp只是将更改复制到一个新的分支),您必须手动恢复它们。

要恢复更改,只需将新创建的分支的更改合并到工作副本。

svn merge --ignore-ancestry ^/trash-stash/my-stash -c <commited revision>

——ignore-ancestry用于不更新工作副本中的合并信息。

Use:

svn ls -v ^/trash-stash/

看看你藏东西的地方有什么。提交的修订版也被印刷。

如果你不需要了,就运行:

svn rm ^/trash-stash/my-stash

这种解决方案比使用patch更好,因为如果工作副本或当前分支的新更改与stash中的更改发生冲突,可以使用svn手段解决冲突,而patch在某些情况下会失败甚至不正确地应用patch。

这篇博文建议使用diff和patch。

Git stash大约变成SVN diff > patch_name.patch;svn revert -R。 Git stash应用变成补丁-p0 < patch_name.patch

注意,这不会隐藏元数据更改或(我认为)目录创建/删除。(是的,svn与目录内容分开跟踪,不像git。)

您可以使用svn diff将当前更改存储到补丁文件中,然后恢复您的工作副本:

svn diff > stash.patch
svn revert -R .

在你实现了你的准备功能后,你可以用补丁工具应用你的补丁:

patch < stash.patch

正如其他人所注意到的,这将不适用于svn:properties和树形操作(添加、删除、重命名文件和目录)。

二进制文件也可能会出现问题,我不知道补丁(或TortoiseSVN在这种情况下如何处理它们)。

从1.10.0(2018-04-13)开始,您有实验性的svn shelve命令。(TortoiseSVN支持该命令)它只是一个保存补丁并应用回来的助手,因此它与svn diff + patch有相同的限制(即不能处理二进制文件和重命名)。(编辑:看起来下一个版本1.11.0将支持二进制文件)

编辑^2:1.11.0(发布2018-10-30)支持二进制文件。暂不支持搁置重命名的文件。1.11中的置物架与1.10中创建的置物架不兼容。

编辑^3:1.12.0(发布2019-04-24),支持复制和重命名。1.12中的货架与早期版本创建的货架不兼容。

编辑^4:1.13.0(2019年10月)和1.14.0(2020年5月)的搁置没有变化。命令仍被标记为实验性,需要定义SVN_EXPERIMENTAL_COMMANDS=shelf3才能启用该特性。看起来这个功能目前还没有分类。

设计说明可以在开发者的Wiki上找到。

$ svn x-shelve --help
x-shelve: Move local changes onto a shelf.
usage: x-shelve [--keep-local] SHELF [PATH...]

  Save the local changes in the given PATHs to a new or existing SHELF.
  Revert those changes from the WC unless '--keep-local' is given.
  The shelf's log message can be set with -m, -F, etc.

  'svn shelve --keep-local' is the same as 'svn shelf-save'.

  The kinds of change you can shelve are committable changes to files and
  properties, except the following kinds which are not yet supported:
     * copies and moves
     * mkdir and rmdir
  Uncommittable states such as conflicts, unversioned and missing cannot
  be shelved.

  To bring back shelved changes, use 'svn unshelve SHELF'.

  Shelves are currently stored under <WC>/.svn/experimental/shelves/ .
  (In Subversion 1.10, shelves were stored under <WC>/.svn/shelves/ as
  patch files. To recover a shelf created by 1.10, either use a 1.10
  client to find and unshelve it, or find the patch file and use any
  1.10 or later 'svn patch' to apply it.)

  The shelving feature is EXPERIMENTAL. This command is likely to change
  in the next release, and there is no promise of backward compatibility.

Valid options:
  -q [--quiet]             : print nothing, or only summary information
  --dry-run                : try operation but make no changes
  --keep-local             : keep path in working copy

(...)

$ svn x-unshelve --help
x-unshelve: Copy shelved changes back into the WC.
usage: x-unshelve [--drop] [SHELF [VERSION]]

  Apply the changes stored in SHELF to the working copy.
  SHELF defaults to the newest shelf.

  Apply the newest version of the shelf, by default. If VERSION is
  specified, apply that version and discard all versions newer than that.
  In any case, retain the unshelved version and versions older than that
  (unless --drop is specified).

  With --drop, delete the entire shelf (like 'svn shelf-drop') after
  successfully unshelving with no conflicts.

  The working files involved should be in a clean, unmodified state
  before using this command. To roll back to an older version of the
  shelf, first ensure any current working changes are removed, such as
  by shelving or reverting them, and then unshelve the desired version.

  Unshelve normally refuses to apply any changes if any path involved is
  already modified (or has any other abnormal status) in the WC. With
  --force, it does not check and may error out and/or produce partial or
  unexpected results.

  The shelving feature is EXPERIMENTAL. This command is likely to change
  in the next release, and there is no promise of backward compatibility.

Valid options:
  --drop                   : drop shelf after successful unshelve
(...)

$ svn help | grep x-
 x-shelf-diff
 x-shelf-drop
 x-shelf-list (x-shelves)
 x-shelf-list-by-paths
 x-shelf-log
 x-shelf-save
 x-shelve
 x-unshelve

当我的工作副本中的一个任务有未提交的更改时,我需要切换到另一个任务,我会做以下两件事之一:

为第二个任务签出一个新的工作副本。 或 启动分支: workingcopy$ svn copy CURRENT_URL_OF_WORKING_COPY SOME_BRANCH workingcopy$ svn switch SOME_BRANCH Workingcopy $ SVN commit -m“正在进行的工作” workingcoyp$ svn switch WHATEVER_I_WAS_WORKING_ON_BEFORE

我有一些脚本可以帮助实现自动化。