git .gitignore 不会忽略文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2965060/
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 08:30:35  来源:igfitidea点击:

.gitignore doesn't ignore files

gitversion-controlatlassian-sourcetree

提问by Fu86

My .gitignorefile gets ignored and the files which should be ignored are still visible.

我的.gitignore文件被忽略,应该忽略的文件仍然可见。

user@host ~/workdir % git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   .htaccess
#       modified:   application/controllers/statistics.php
#
no changes added to commit (use "git add" and/or "git commit -a")
user@host ~/workdir % cat .gitignore
.htaccess
application/config/config.php
application/config/database.php
user@host ~/workdir %

The files are in version control, but with sensible changes I dont want to push. git rm ...is not an option because the files should be in version controll (with other settings in the files). I just dont want to push my changes on this files.

这些文件处于版本控制中,但我不想推送一些合理的更改。git rm ...不是一个选项,因为文件应该在版本控制中(文件中有其他设置)。我只是不想在这个文件上推送我的更改。

采纳答案by Noufal Ibrahim

Ignoring files in git doesn't mean that you won't pushthem. It means that you won't version controlthem. If you don't want to push then, I suggest you create a local branch which will contain stuff for the local machine and then merge necessary changes to a branch which you'll push to remote.

忽略 git 中的文件并不意味着你不会推送它们。这意味着您不会它们进行版本控制。如果您不想推送,我建议您创建一个本地分支,其中包含本地机器的内容,然后将必要的更改合并到您将推送到远程的分支。

I think you've already added your files and are now trying to ignore them.

我认为您已经add编辑了您的文件,现在正试图忽略它们。

回答by anybug

i had same problem: i wanted different .htaccess file per repository then i couldn't because htaccess file had already been added to repository. It's working now, i can change any of the .htaccess file from any version without getting git pushing them. Marc is right when he says "You need to remove it first, before it can be ignored. This can be done using git rm".

我有同样的问题:我想要每个存储库不同的 .htaccess 文件,然后我不能,因为 htaccess 文件已经添加到存储库中。它现在可以工作了,我可以更改任何版本的任何 .htaccess 文件,而无需 git 推送它们。Marc 说的是“你需要先删除它,然后才能忽略它。这可以使用 git rm 完成”。

i proceeded in this order:

我按以下顺序进行:

  • backup your .htaccess file somewhere
  • remove .htaccess file using git rm .htaccess
  • commit this change (.htaccess deletion)
  • push it (git push)
  • 在某处备份您的 .htaccess 文件
  • 使用 git rm .htaccess 删除 .htaccess 文件
  • 提交此更改(.htaccess 删除)
  • 推它(git push)

this will obviously remove your .htaccess file

这显然会删除您的 .htaccess 文件

  • then edit .gitignore and add /.htaccess
  • commit then push it
  • recreate your .htaccess file by restoring your previous backup
  • 然后编辑 .gitignore 并添加 /.htaccess
  • 提交然后推送它
  • 通过恢复以前的备份重新创建 .htaccess 文件

then you're done: whatever changes you make into .htaccess won't be pushed anymore. The hard part is following steps order...

然后你就完成了:你对 .htaccess 所做的任何更改都不会再被推送。困难的部分是按照步骤顺序...

hope this is clear

希望这很清楚

回答by tabacitu

after anybug's steps noted heremy git still kept track of .htaccess and this helped:

此处指出 anybug 的步骤我的 git 仍然跟踪 .htaccess,这有帮助:

git update-index --assume-unchanged .htaccess

To revert back:

要恢复:

git update-index --no-assume-unchanged .htaccess

回答by VonC

Since you want to:

既然你想:

  • keep .htaccessin the Git repo (no git rm)
  • have localsensitive data in it
  • 保留.htaccess在 Git 存储库中(否git rm
  • 本地敏感数据

use a filter driverwhich will:

使用过滤器驱动程序,它将:

  • during the checkout phase, complete the .htaccesswith private data
  • during the commit phase clean the .htaccessfrom all sensitive data
  • 在结账阶段,.htaccess用私人数据完成
  • 在提交阶段清除.htaccess所有敏感数据

content filter driver

内容过滤驱动

you can then version an encrypted file that your smudge script will:

然后,您可以对一个加密文件进行版本控制,您的涂抹脚本将:

  • be the only one to decrypt
  • use in order to complete the classic (and pushed/pulled) .htaccess file.
  • 成为唯一解密的人
  • 使用以完成经典(和推/拉).htaccess 文件。

You can version that encrypted sensible data in a special branch you won't push.
Even if you did push that file somehow, the damage would be limited, since only your workstation has the private key needed to decrypt said file.

您可以在不会推送的特殊分支中对加密的敏感数据进行版本控制。
即使您确实以某种方式推送了该文件,损害也将是有限的,因为只有您的工作站具有解密所述文件所需的私钥。

回答by Bade Lal

Suppose the below is the content of .gitignorefile. Here, .htaccessis in the project's root directory.

假设下面是.gitignore文件的内容。这里,.htaccess是在项目的根目录下。

/.htaccess
application/config/config.php
application/config/database.php

Let's run the below command to ignore the .htaccess, config.phpand database.phpfiles.

让我们运行以下命令来忽略.htaccess,config.phpdatabase.php文件。

[email protected]:work-dir$ git update-index --assume-unchanged .htaccess
[email protected]:work-dir$ git update-index --assume-unchanged application/config/config.php
[email protected]:work-dir$ git update-index --assume-unchanged application/config/database.php

If you want to start tracking it again:

如果您想再次开始跟踪它:

[email protected]:work-dir$ git update-index --no-assume-unchanged path/to/file

e.g.

例如

[email protected]:work-dir$ git update-index --no-assume-unchanged .htaccess

回答by Marc

It seems that the .htaccessfile has already been added to the version control. You need to remove it first, before it can be ignored. This can be done using git rm

.htaccess文件似乎已经添加到版本控制中。您需要先将其删除,然后才能忽略它。这可以使用git rm来完成

回答by Jason Yarrington

I do this all the time. I start with configuration files in my repo then decide that I don't want them in there.

我一直这样做。我从我的 repo 中的配置文件开始,然后决定我不希望它们在那里。

You can remove something from your repo by using

您可以使用

git rm --cached <<filename>>

回答by Marcelo Cantos

This one bugged me for days. The .htaccessfile is already under version control and has been modified.

这个问题困扰了我好几天。该.htaccess文件已在版本控制之下并已被修改。