0

我有以下目录结构

在此处输入图像描述

这是 .gitignore 的内容

conf.php

问题是,当我更改内容 conf.php 并尝试提交某些内容时,GITconf.php再次检测为更改的文件。事实上,它必须被忽略。
我犯过一次。我想从现在开始忽略它。我错过了什么?

更新

我试过以下:

  1. 将 conf.php 添加到 gitignore

  2. 从缓存中删除它git rm --cached conf.php.

    但是现在,当我重新扫描项目时,它想暂存conf.php为已删除。

在此处输入图像描述

它不是我想要的。

我想将其保留在远程仓库中并忽略本地仓库中未来(从现在开始)的更改。

4

3 回答 3

6

Git 不会忽略跟踪的文件

Git 不会忽略对已提交文件的更改。

Git忽略的目的是什么

Git 忽略,忽略存储库中的噪音,例如:

$ git init
$ mkdir tmp
$ touch tmp/file1
$ touch tmp/file2
$ git status
# On branch master
#   
# Initial commit
#   
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#   
#   tmp/
nothing added to commit but untracked files present (use "git add" to track)

如果 .gitignore 文件被修改为忽略 tmp 目录:

$ echo "tmp" > .gitignore
$ git status
# On branch master
#   
# Initial commit
#   
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#   
#   .gitignore
nothing added to commit but untracked files present (use "git add" to track)

tmp 目录内容不再列出 - 但 .gitignore 文件已列出(因为我刚刚创建了它并且文件本身没有被忽略)。

承诺,没有列出任何更改:

$ git add .gitignore
$ git commit -v -m "addding git ignore file"
[master (root-commit) d7af571] addding git ignore file
1 file changed, 1 insertion(+)
    create mode 100644 .gitignore
$ git status
# On branch master
    nothing to commit (working directory clean)

现在对 .gitignore 文件的任何更改都将显示为挂起的更改,tmp 目录之外的任何新文件都将显示为未跟踪的文件,并且 tmp 目录内的任何更改都将被忽略。

不要提交您不想跟踪的文件

如果您已经将 conf.php 添加到您的 git 存储库,则 git 正在跟踪该文件。当它更改时,git说它有待处理的更改。

解决此类问题的方法是不要提交您不想跟踪的文件。相反,您可以做的是提交默认文件,例如:

$ mv conf.php conf.php.default
$ # Edit conf.php.default to be in an appropriate state if necessary
$ git commit -v -m "moving conf.php file"
$ cp conf.php.default conf.php

现在,您对 conf.php 所做的任何更改都不会显示为未分级更改。

于 2012-10-25T14:21:57.410 回答
4

如果您想向项目添加一个空配置文件,然后不跟踪任何附加更改,那么您可以这样做:

  1. 编辑 conf.php 使其处于您希望它保持的状态
  2. 添加并提交 config.php

运行以下 git 命令:

git update-index --assume-unchanged conf.php

您将不再看到 conf.php 有待处理的更改

要再次开始跟踪更改,请运行以下命令:

git update-index --no-assume-unchanged conf.php
于 2013-12-31T01:55:41.570 回答
3

赶紧跑git rm --cached conf.php

如果您有更多提交的文件要忽略:

git rm -r --cached .

git add .


然后提交并推送您的更改。

于 2012-10-25T14:21:59.567 回答