如何忽略已经提交的文件? [英] How to ignore a file which is already committed?

查看:271
本文介绍了如何忽略已经提交的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前,以下是我的 .gitignore 文件:

  ... 
config / database.yml
.DS_Store

后来我创建了一个 app_config.yml 文件放在config目录中并提交。



现在,我意识到我没有需要git仓库中的 app_config.yml 文件。
我修改了我的 .gitignore 文件:

 。 .. 
config / app_config.yml
config / database.yml
.DS_Store

现在,当我提交时, app_config.yml 文件仍在我的回购库中。
我想从我的回购中删除该文件。我怎么做到的?

正如忽略不会删除文件

(以下引用伟大的文章来自 Nick Quaranto

在他的博客中 git ready 一次学习git一次提交):



当您告诉Git忽略文件时,只是停止观看对该文件的更改,而没有其他任何内容。

这意味着历史记录仍会记住该文件并将其保存



如果您想从存储库中删除文件,但将其保存在工作目录中,只需使用:



  git rm --cached< file> 




但是,这仍然会保留历史记录中的文件。



如果您真的想将其从历史记录中删除,您确实有两种选择:


  • 重新编写仓库的提交,或者
  • 重新开始。



这两个选项真的很糟糕,这是一个很好的理由:Git尽力不丢失您的数据。

和重新绑定一样,Git迫使你考虑这些类型的选项,因为它们是破坏性的操作。



如果你真的想从历史中删除一个文件, strong> git filter-branch 是您正在寻找的钢锯。

绝对读取其 manpage 在使用之前,因为它会直接重写您的项目提交。这对于某些操作实际上是一个很好的工具,并且可以执行各种操作,比如完全删除作者的提交以移动项目根文件夹。从所有版本删除文件的命令是:



  git filter-branch --index -filter'git rm --cached< file>'HEAD 




如果您需要删除可能已放置在您的存储库中的敏感或机密信息,此操作绝对有用



Previously, the following was my .gitignore file:

...
config/database.yml
.DS_Store

Later I created an app_config.yml file in the config directory and committed it.

Now, I realized that I don't need the app_config.yml file in the git repository. And I modified my .gitignore file:

...
config/app_config.yml
config/database.yml
.DS_Store

Now, when I commit, that app_config.yml file is still in my repo. I want to remove that file from my repo. How can I do it?

解决方案

As mentionned in "ignoring doesn't remove a file "
(the following quote the great article from Nick Quaranto,
in his blog git ready "learn git one commit at a time"):

When you tell Git to ignore files, it’s going to only stop watching changes for that file, and nothing else.
This means that the history will still remember the file and have it
.

If you want to remove a file from the repository, but keep it in your working directory, simply use:

git rm --cached <file>

However, this will still keep the file in history.

If you actually want to remove it from history, you really have two options:

  • rewrite your repository’s commits, or
  • start over.

Both options really suck, and that’s for a good reason: Git tries hard not to lose your data.
Just like with rebasing, Git forces you to think about these kinds of options since they are destructive operations.

If you did actually want to remove a file from history, git filter-branch is the hacksaw you’re looking for.
Definitely read up on its manpage before using it, since it will literally rewrite your project’s commits. This actually is a great tool for some actions, and can do all sorts of stuff like totally removing an author’s commits to moving the project root folder around. The command to remove a file from all revisions is:

git filter-branch --index-filter 'git rm --cached <file>' HEAD

This action can definitely be useful when you need to blow out sensitive or confidential information that may have been placed in your repository

这篇关于如何忽略已经提交的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆