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

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

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


当前回答

我不知道只有svn有什么简单的方法。老实说,我建议使用git-svn来创建一个git repo,作为svn的工作副本,并使用git stash。只需将git pull替换为git svn rebase,将git push替换为git svn dcommit,您就可以保留90%的git工作流,并且仍然与svn服务器通信。

其他回答

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

为第二个任务签出一个新的工作副本。 或 启动分支: 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

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

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

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

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

最简单的方法是使用一个临时分支,就像这样:

$ svn copy ^/trunk ^/branches/tempbranch
$ svn switch ^/branches/tempbranch
$ svn commit -m "Stashed"
$ svn switch ^/trunk
$ ... hack away in trunk ...
$ svn commit -m "..."
$ svn merge ^/branches/tempbranch .
$ svn rm ^/branches/tempbranch
$ ... continue hacking

这可以(而且可能应该)放在一个脚本中,如果更有规律的话。

在我的实践中,我使用git init在Subversion存储库的trunk目录中创建一个git存储库,然后添加*。git到吸盘忽略模式。

在修改了一些文件后,如果我想继续使用Subversion主线进行工作,我只需使用git stash来隐藏我的工作。在提交到Subversion存储库之后,我使用git stash pop来恢复我的修改。

从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