171

当你使用

git rm --cached myfile

它不会从本地文件系统中删除,这是目标。但是,如果您已经对文件进行了版本控制并提交,将其推送到中央存储库,并在使用该命令之前将其拉入另一个存储库,它将从该系统中删除该文件。

有没有办法从版本控制中删除文件而不从任何文件系统中删除它?

编辑:澄清,我希望。

4

7 回答 7

123
于 2010-04-10T07:52:26.630 回答
88

Had the very same issue this week when I accidentally committed, then tried to remove a build file from a shared repository, and this:

http://gitready.com/intermediate/2009/02/18/temporarily-ignoring-files.html

has worked fine for me and not mentioned so far.

git update-index --assume-unchanged <file>

To remove the file you're interested in from version control, then use all your other commands as normal.

git update-index --no-assume-unchanged <file>

If you ever wanted to put it back in.

Edit: please see comments from Chris Johnsen and KPM, this only works locally and the file remains under version control for other users if they don't also do it. The accepted answer gives more complete/correct methods for dealing with this. Also some notes from the link if using this method:

Obviously there’s quite a few caveats that come into play with this. If you git add the file directly, it will be added to the index. Merging a commit with this flag on will cause the merge to fail gracefully so you can handle it manually.

于 2012-05-27T11:08:52.553 回答
29

要从索引中删除文件,请使用:

git reset mylife

这不应影响您的本地副本或其他任何人的副本。

于 2010-04-09T12:51:48.903 回答
25
  1. git rm --cached remove_file
  2. add file to gitignore
  3. git add .gitignore
  4. git commit -m "Excluding"
  5. Have fun ;)
于 2017-02-09T16:16:21.650 回答
15

执行git rm --cached命令后,尝试添加myfile.gitignore文件(如果不存在则创建一个)。这应该告诉 git 忽略myfile

.gitignore文件是版本化的,因此您需要提交它并将其推送到远程存储库。

于 2010-04-09T01:58:35.937 回答
1

My solution is to pull on the other working copy and then do:

git log --pretty="format:" --name-only -n1 | xargs git checkout HEAD^1

which says get all the file paths in the latest comment, and check them out from the parent of HEAD. Job done.

于 2012-01-05T16:24:22.167 回答
0

The above solutions work fine for most cases. However, if you also need to remove all traces of that file (ie sensitive data such as passwords), you will also want to remove it from your entire commit history, as the file could still be retrieved from there.

Here is a solution that removes all traces of the file from your entire commit history, as though it never existed, yet keeps the file in place on your system.

https://help.github.com/articles/remove-sensitive-data/

You can actually skip to step 3 if you are in your local git repository, and don't need to perform a dry run. In my case, I only needed steps 3 and 6, as I had already created my .gitignore file, and was in the repository I wanted to work on.

To see your changes, you may need to go to the GitHub root of your repository and refresh the page. Then navigate through the links to get to an old commit that once had the file, to see that it has now been removed. For me, simply refreshing the old commit page did not show the change.

It looked intimidating at first, but really, was easy and worked like a charm ! :-)

于 2015-12-11T20:00:03.937 回答