如何在 svn 和/或 git 中“取消版本”文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 05:43:30  来源:igfitidea点击:

How to "unversion" a file in either svn and/or git

svngitversion-controlversioning

提问by Mo.

It happens to me all the time. I accidentally version a file, I do not want to be versioned (i.e. developer/machine specific config-files).

它总是发生在我身上。我不小心版本了一个文件,我不想被版本化(即开发人员/机器特定的配置文件)。

If I commit this file, I will mess up the paths on all the other developer machines - they will be unhappy.

如果我提交这个文件,我会弄乱所有其他开发者机器上的路径——他们会不高兴。

If I do delete the file from versioning, it will be deleted from the other developers machines - they will be unhappy.

如果我确实从版本控制中删除了该文件,它将从其他开发人员的机器中删除 - 他们会不高兴。

If I choose to never commit the file, I always have a "dirty" checkout - I am unhappy.

如果我选择从不提交文件,我总是有一个“脏”的结帐——我很不高兴。

Is a clean way to "unversion" a file from revision-control, that will result in no-one being unhappy?

是一种从修订控制中“取消版本”文件的干净方法,这会导致没有人不高兴吗?

edit: trying to clarify a bit: I have already commited the file to the repository and I want to only remove it from versioning - I specifically do not want it to be physically deleted from everyone doing a checkout. I initially wanted it to be ignored.

编辑:试图澄清一点:我已经将文件提交到存储库,我只想从版本控制中删除它 - 我特别不希望它从每个人进行结帐时物理删除。我最初希望它被忽略。

Answer: If I could accept a second answer, it would be this. It answers my question with respect to git - the accepted answer is about svn.

回答:如果我可以接受第二个答案,那就是这个。它回答了我关于 git 的问题——接受的答案是关于 svn。

采纳答案by Mo.

SVN version 1.5 supports removing/deleting a file from a repository with out losing the local file

SVN 1.5 版支持从存储库中删除/删除文件而不会丢失本地文件

taken from http://subversion.tigris.org/svn_1.5_releasenotes.html

取自http://subversion.tigris.org/svn_1.5_releasenotes.html

New --keep-local option retains path after delete..

新的 --keep-local 选项在删除后保留路径..

Delete (remove) now takes a --keep-local option to retain its targets locally, so paths will not be removed even if unmodified.

删除 (remove) 现在采用 --keep-local 选项在本地保留其目标,因此即使未修改路径也不会被删除。

回答by wxs

In Git, in order to delete it from the tree, but NOT from the working directory, which I think is what you want, you can use the --cached flag, that is:

在 Git 中,为了从树中删除它,而不是从工作目录中删除它,我认为这是你想要的,你可以使用 --cached 标志,即:

git rm --cached <filename>

回答by Glenn Slaven

If you accidentally 'add' a file in svn & you haven't committed it, you can revert that file & it will remove the add.

如果您不小心在 svn 中“添加”了一个文件并且您没有提交它,您可以还原该文件并删除添加。

回答by Kai

Without having tried it...

没有试过...

In git, if your changes haven't been propagated to another repository, you shouldbe able to git rmthe affected file(s), git rebase --interactiveto reorder the deletion commit to be just after the commit in which you accidentally added the offending files, and then squash those two commits together.

在 git 中,如果您的更改尚未传播到另一个存储库,您应该能够对git rm受影响的文件git rebase --interactive重新排序删除提交,使其紧跟在您不小心添加了违规文件的提交之后,然后压缩这两个一起提交。

Of course, this won't help if someone else has pulled your changes.

当然,如果其他人撤消了您的更改,这将无济于事。

回答by Trumpi

It sounds like you have already added and committed the file to subversion (I assume that you are using Subversion). If that is the case, then there are only two ways to remove that file:

听起来您已经将文件添加并提交到 subversion(我假设您正在使用 Subversion)。如果是这种情况,那么只有两种方法可以删除该文件:

  1. Mark the file as deleted and commit.
  2. Perform an svnadmin dump, filter out the revision where you accidentally committed the file and perform an svnadmin load.
  1. 将文件标记为已删除并提交。
  2. 执行一个svnadmin dump,过滤掉你不小心提交文件的修订版,然后执行一个svnadmin load.

Trust me, you don't really want to do number 2. It will invalidate all working copies of the repository. The best is to do number 1, mark the file as ignored and apologise.

相信我,您不会真的想做第 2 项。它会使存储库的所有工作副本无效。最好的做法是做第 1 步,将文件标记为已忽略并道歉。

回答by dbr

To remove a file entirely from a git repository (Say you commited a file with a password in it, or accidently commited temporary files)

从 git 存储库中完全删除文件(假设您提交了一个带有密码的文件,或者不小心提交了临时文件)

git filter-branch --index-filter 'git update-index --remove filename' HEAD

Then I think you have to commit, and push -f if it's in remote branches (remember it might annoy people if you start changing the repository's history.. and if they have pulled from you before, they could still have the file)

然后我认为你必须提交,如果它在远程分支中,则 push -f (请记住,如果你开始更改存储库的历史记录,它可能会惹恼人们......如果他们之前从你那里拉过,他们仍然可以拥有该文件)

回答by Greg Hewgill

Look up svn:ignore and .gitignore - these features allow you to have extra files in your checkout that are ignored by your RCS (when doing a "status" operation or whatever).

查找 svn:ignore 和 .gitignore - 这些功能允许您在结帐中拥有被 RCS 忽略的额外文件(在执行“状态”操作或其他操作时)。

For machine-specific config files, a good option is to check in a file named with an extra ".sample" extension, ie. config.xml.sample. Individual developers would make a copy of this file in config.xmland tweak it for their system. With svn:ignore or .gitignore you can ensure that the unversioned config.xmlfile doesn't show up as dirty all the time.

对于特定于机器的配置文件,一个不错的选择是签入一个以额外的“.sample”扩展名命名的文件,即。config.xml.sample. 个别开发人员会制作此文件的副本config.xml并针对他们的系统进行调整。使用 svn:ignore 或 .gitignore 您可以确保未版本化的config.xml文件不会一直显示为脏文件。

In response to your edit: If you remove the file from the repository now, then your developers will get a conflict next time they do an update (assuming they have all changed the file for their system). They won't lose their local changes, they will be recoverable from somewhere. If they happen not to have made any local changes, then their config file will vanish but they can just re-get the previous one out of source control and use that.

回应您的编辑:如果您现在从存储库中删除该文件,那么您的开发人员下次进行更新时将遇到冲突(假设他们都已更改了其系统的文件)。它们不会丢失本地更改,它们可以从某个地方恢复。如果他们碰巧没有进行任何本地更改,那么他们的配置文件将消失,但他们可以从源代码控制中重新获取前一个并使用它。

回答by Sergio Acosta

To remove a file already in source control:

要删除已在源代码管理中的文件:

git rm <filename>

and then

进而

git commit -m ...

You should add every file you want to ignore to the .gitignore file. I additionally always check the .gitignore file to my repository, so if someone checks out the code on his machine, and the file gets generated again, he won't 'see' it as 'dirty'.

您应该将要忽略的每个文件添加到 .gitignore 文件中。另外,我总是将 .gitignore 文件检查到我的存储库,所以如果有人在他的机器上检查代码,并且文件再次生成,他不会“看到”它为“脏”。

Of course if you already committed the file and someone else got your changes on another machine, you would have to alter every local repository to modify the history. At least that's a possible solution with git. I don't think svn would let you do that.

当然,如果您已经提交了文件并且其他人在另一台机器上获得了您的更改,则您必须更改每个本地存储库以修改历史记录。至少这是一个可能的 git 解决方案。我不认为 svn 会让你这样做。

If the file is already on the master repository (git) or in the server (svn), I don't think there is a better solution than just deleting the file in another commit.

如果文件已经在主存储库 (git) 或服务器 (svn) 中,我认为没有比在另一个提交中删除文件更好的解决方案。

回答by Magnus Westin

For SVN you can revert files you haven't committed yet. In TortoiseSVN you just right click the file in the commit window and choose Revert...

对于 SVN,您可以还原尚未提交的文件。在 TortoiseSVN 中,您只需右键单击提交窗口中的文件并选择 Revert...

On command line use svn revert [file]

在命令行上使用 svn revert [file]

Don't know about GIT since I've never used it.

不知道 GIT,因为我从未使用过它。

回答by Manu Manjunath

Two simple steps in SVN:
1. Add this directory in parent directory's svn:ignore property:

SVN 中的两个简单步骤:
1. 在父目录的 svn:ignore 属性中添加此目录:

svn propedit svn:ignore .  

2. Remove directory:

2. 删除目录:

svn rm mydir

3. Commit

3. 提交

Please note that when other developers do a svn update, that directory will notget deleted. SVN just unversions it instead.

请注意,当其他开发人员执行svn update 时,该目录不会被删除。SVN 只是将其取消版本化。