删除和重命名文件
在处理 SVN 或 Git 项目的签出副本时,我经常删除或重命名文件,而不使用适当的 git mv、svn rename、git rm 或 svn delete 命令,因此当我将版本提交回存储库时,这些更改不会正确反映。除了我自己更加小心之外,是否有解决方法可以在事后解决此问题?
When working with a checked-out copy of an SVN or Git project, I often delete or rename files without using the appropriate git mv
, svn rename
, git rm
, or svn delete
commands, so these changes are not appropriately reflected when I commit my version back to the repository. Is there a workaround to fix this after the fact, besides being more careful on my end?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于 Git,请查看
git添加-A
。这将添加所有新的或更改的内容,并删除不再存在的文件。不过,它也可能会添加您并不真正想要添加的文件,因此请务必小心。由于 git 不会显式跟踪重命名,因此这应该足够了(与
git commit
一起)。对于 Subversion,您可以执行类似
的操作svn add --force .
后跟svn commit
执行等效操作,但这不会正确跟踪重命名/移动。我认为找出哪些文件被重命名(也可能被更改)以及哪些文件被简单地添加/删除并不是一件容易的事。For Git, have a look at
git add -A
. This will add everything which is new or changed, as well as removing files which are not there anymore. It might also add files you don't really want to add, though, so be careful here.As git does not track renames explicitly, this should be enough (together with a
git commit
).For Subversion, you could do something like
svn add --force .
followed by asvn commit
to do the equivalent, but this will not track renames/moves properly. I think it is not trivial to find out which files were renamed (and maybe changed as well) and which were simply added/removed.对于 git,
git mv
只是一个方便的别名,相当于移动文件并对旧文件执行git rm
。如果你省略使用这个,结果是你删除了原始文件,git 会注意到这一点,并添加了一个新文件,git 会忽略它,因为你没有调用 git add 。 git rm 类似。这是 git 的缺点之一,因为它不会像其他版本控制系统那样强制您提交存储库中的每个更改。但是,可以使用 git status 命令并仔细使用 .gitignore 文件轻松管理它。只要您保持树干净,或者适当地忽略,您就可以快速浏览一下是否有任何您错过的未跟踪文件。
至于事后修复此问题,只需暂存并提交适当的更改,在这种情况下将是未跟踪的文件。
With git,
git mv
is just a convenient alias and is equivalent to moving a file and doing agit rm
on the old file. If you omit using this, the result is you deleted the original file, which git will notice, and added a new file, which git will ignore because you haven't calledgit add
.git rm
is similar.This is one of the downsides of git since it does not force you to commit every change in your repository as other version control systems do. However, it can be easily managed with the
git status
command and careful use of .gitignore files. So long as you keep your tree clean, or appropriately ignored, you can tell at a quick glance whether you have any untracked files that you missed.As far as fixing this after the fact, just stage and commit the appropriate changes, which in this case will be untracked files.
在 git 中,如果你删除了文件,你可以使用 git commit -a ,提交将反映删除,而无需执行 git rm
诸如 TortoiseSVN 和 TortoiseGit 这样的 Gui 工具对这些事情很聪明,并且在使用此类工具时通常不必进行显式删除和重命名。
In git if you have deleted files, you can use
git commit -a
and the commit will reflect the deletes without having to do agit rm
Gui tools like TortoiseSVN and TortoiseGit are smart about such things and you generally don't have to do explicit delete and renames when using such tools.
对于 git,删除文件后,请使用:
For git, once you removed a file, use: