git - 如何忽略已经提交的文件?

标签 git gitignore

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

...
config/database.yml
.DS_Store

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

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

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

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

最佳答案

如“ignoring doesn't remove a file ”中所述
(以下引用Nick Quaranto的精彩文章,
在他的博客中git ready “一次学习 git”):

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

关于git - 如何忽略已经提交的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/625919/

相关文章:

git - .gitignore 文件中的/media 和 media/有什么区别?

git - 一个项目可以有多个来源吗?

git - macOS Git 客户端可以使用存储在用户钥匙串(keychain)中的证书吗?

git - 如何安装 Symfony 2.7?

git - 忽略根目录下的所有文件,除了一些特定的文件、目录

git - 如何让.gitignore忽略文件?

Git与单个破折号 merge

混帐智能 "Authentication Failed”

java - 如果 gradlew 的版本明确在 build.gradle 上,为什么每个人都说我应该在我的源代码管理中包含 gradlew?

git - 如何让 git 忽略 vendor/下的 .git 文件夹?