不理解gitignore提示消息

xesrikrc  于 2023-09-29  发布在  Git
关注(0)|答案(2)|浏览(114)

我试图理解以下信息:

cap@MacPro-2 wiki-dir % git add -A -- /Users/cap/DOCKER/dante-wiki/volumes/full/content/wiki-dir/extensions/DanteBread/breadCrumbs.js
The following paths are ignored by one of your .gitignore files:
extensions
hint: Use -f if you really want to add them.
hint: Turn this message off by running
hint: "git config advice.addIgnoredFile false"

我觉得令人困惑的问题如下:
在发出git add命令之前,我得到了

cap@MacPro-2 wiki-dir % git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    modified:   extensions/DanteBread/COPYING

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   extensions/DanteBread/breadCrumbs.js

之后我得到了

cap@MacPro-2 wiki-dir % git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    modified:   extensions/DanteBread/COPYING
    modified:   extensions/DanteBread/breadCrumbs.js

所以Git实际上 * 确实 * 将添加的文件添加到了暂存区,尽管我没有使用-force
另外,一个单独的git check-ignore表示该文件 * 不 * 被忽略。
为什么我得到这个警告?为什么git add声明路径extensions被忽略,而git check-ignore没有找到它?

c9qzyr3d

c9qzyr3d1#

我可以在我的Linux系统上重现同样的问题,使用git版本2.42.0。它看起来像是git中的一个bug(或缺点)--至少在当前版本中是这样。
设置:

mkdir repo
cd repo
git init
mkdir -p dir1
echo foobarfoobar > dir1/versioned.txt
git add dir1/versioned.txt && git commit -m "created dir1/versioned.txt"
echo dir1 > .gitignore
git add .gitignore && git commit -m "ignoring dir1"

现在:

  • 运行git check-ignore dir1/versioned.txt将产生空输出,但运行git check-ignore dir1/ignored.txt甚至git check-ignore dir1/doesnt_exist.txt将显示某些内容被忽略:
# check-ignore does not mention versioned.txt: (coherent: that file is tracked)
$ git check-ignore dir1/versioned.txt

# it does catch ignored files, or even path that don't exist yet and are below dir1/:
# (coherent: these files would be ignored)
$ touch dir1/ignored.txt
$ git check-ignore dir1/ignored.txt
dir1/ignored.txt
$ git check-ignore dir1/doesnt_exist.txt dir1/no_dir/no_file.txt
dir1/doesnt_exist.txt
dir1/no_dir/no_file.txt

# running git check-ignore on the directory dir1 alone does not mention anything
# (shortcoming ? that path is explicitly mentioned as an ignored path,
#  it does have some of its content tracked though)
$ git check-ignore dir1/
  • 修改版本化文件会产生与您相同的行为:该文件(正确地)列在git status中,尽管它 * 确实 * 被添加,但在运行git add时我得到警告:
$ echo "barfoobarfoo" >> dir1/versioned.txt

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   dir1/versioned.txt

no changes added to commit (use "git add" and/or "git commit -a")

# (shortcoming: that file is known to be versioned, the warning
#  shouldn't be there)
$ git add dir1/versioned.txt
The following paths are ignored by one of your .gitignore files:
dir1
hint: Use -f if you really want to add them.
hint: Turn this message off by running
hint: "git config advice.addIgnoredFile false"

$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    modified:   dir1/versioned.txt
a0x5cqrl

a0x5cqrl2#

我做了大量的测试,得出以下结论:
1.看起来我的gitignore / index / tracking的心理模型是不正确的。
1.看起来对gitignore如何工作的描述很糟糕,但有一点虚幻,看起来行为是正确的。
1.暗示是错误的。“如果你真的想把它们加进去”的语言,至少在我看来,是指错了方向。
1.我现在的解决办法是关闭提示。
1.这些文件已在索引中,不受添加的影响。

相关问题