【问题标题】:How do I get git to track and commit files according to .gitignore (from the beginning)?如何让 git 根据 .gitignore 跟踪和提交文件(从头开始)?
【发布时间】:2017-07-03 17:44:02
【问题描述】:

编辑
我的问题中的细节隐藏了问题的真实性质。但是,由于这是问题可能会向其他人显示的方式,因此我决定保持原样并仅添加此编辑以及下面的答案。


我对 git 完全陌生。

我正在开发一个基于 PHP 的网站,并且只想跟踪我的文件:
即仅/plugins/my-plugins//themes/my-theme/ 中的文件。

由于未能使其正常工作,我决定创建并使用一个最低限度的工作示例。

根据https://git-scm.com/docs/gitignore

排除除特定目录 foo/bar 之外的所有内容的示例(注意 /* - 没有斜线,通配符也将排除 foo/bar 中的所有内容):
$ cat .gitignore
# exclude everything except directory foo/bar
/*
!/foo
/foo/*
!/foo/bar

所以,我创建了文件/文件夹来匹配:

alan@lamp ~/GitPlay$ tree -a
.
|-- .gitignore
|-- README.md
|-- foo
|   |-- bar
|   |   -- shouldNOTbeExcluded_bar.txt
|   -- shouldBeExcluded_foo.txt
-- shouldBeExcluded.txt

2 directories, 4 files
alan@lamp ~/GitPlay$ 

根据https://dzone.com/refcardz/getting-started-git

...通过键入以下命令将目录初始化为 Git 存储库:
git init
git add .
git commit –m 'The first commit'

git add.之前:

alan@lamp ~/GitPlay$ git status
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .gitignore
    README.md
    foo/
    shouldBeExcluded.txt

nothing added to commit but untracked files present (use "git add" to track)
alan@lamp ~/GitPlay$ 

因此,它不会跟踪不应跟踪的文件。这表明它正在跟踪它应该跟踪的文件。
所以也许我可以提交显然被跟踪的内容:

alan@lamp ~/GitPlay$ git commit -m 'Very first commit after git init'
On branch master

Initial commit

Untracked files:
    .gitignore
    README.md
    foo/
    shouldBeExcluded.txt

nothing added to commit but untracked files present
alan@lamp ~/GitPlay$ 

“没有添加到提交”。

好的,让我们添加一些东西:

alan@lamp ~/GitPlay$ git add .

现在看看发生了什么:

alan@lamp ~/GitPlay$ git status
On branch master

Initial commit

    Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   .gitignore
    new file:   README.md
    new file:   foo/bar/shouldNOTbeExcluded_bar.txt
    new file:   foo/shouldBeExcluded_foo.txt
    new file:   shouldBeExcluded.txt

alan@lamp ~/GitPlay$ 

现在它计划在下一次提交中添加所有内容。这不是我想要的。

让我们看看实际发生了什么:

alan@lamp ~/GitPlay$ git commit -m 'Very first commit after git init'
[master (root-commit) dca0c49] Very first commit after git init
 5 files changed, 37 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 README.md
 create mode 100644 foo/bar/shouldNOTbeExcluded_bar.txt
 create mode 100644 foo/shouldBeExcluded_foo.txt
 create mode 100644 shouldBeExcluded.txt
alan@lamp ~/GitPlay$ 

正如我所怀疑的,一切都已提交。

所以,我的问题是:
如何让 git 从一开始就根据 .gitignore 跟踪和提交文件?

还是我遗漏了一些基本的东西?
是“正确”的程序
无论.gitignore,在第一次提交中提交所有内容,
然后git会根据.gitignore从下一个git add ./git commit ...开始跟踪变化?


根据 git add all except ignoring files in .gitignore file

我认为您的意思是 git add . 会将所有未在 .gitignore 中指定的文件添加到 repo - 您可以通过键入 git status 来查看这些更改

git add .
这将添加所有路径并忽略来自 .gitignore 的匹配项

这与我的经验不符:git add . 添加所有文件并忽略 .gitignore


Git add . ignores .gitignore

.gitignore 只考虑新文件。

这确实符合我的发现。


进一步发挥

第一次提交之后:

alan@lamp ~/GitPlay$ git status
On branch master
nothing to commit, working tree clean

所以我想,也许清除暂存缓存可以解决问题:

alan@lamp ~/GitPlay$ git rm -r --cached .
rm '.gitignore'
rm 'README.md'
rm 'foo/bar/shouldNOTbeExcluded_bar.txt'
rm 'foo/shouldBeExcluded_foo.txt'
rm 'shouldBeExcluded.txt'

alan@lamp ~/GitPlay$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    deleted:    .gitignore
    deleted:    README.md
    deleted:    foo/bar/shouldNOTbeExcluded_bar.txt
    deleted:    foo/shouldBeExcluded_foo.txt
    deleted:    shouldBeExcluded.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .gitignore
    README.md
    foo/
    shouldBeExcluded.txt

alan@lamp ~/GitPlay$ git rm -r --cached .
fatal: pathspec '.' did not match any files

alan@lamp ~/GitPlay$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    deleted:    .gitignore
    deleted:    README.md
    deleted:    foo/bar/shouldNOTbeExcluded_bar.txt
    deleted:    foo/shouldBeExcluded_foo.txt
    deleted:    shouldBeExcluded.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .gitignore
    README.md
    foo/
    shouldBeExcluded.txt

alan@lamp ~/GitPlay$ git add .

alan@lamp ~/GitPlay$ git commit -m 'First commit'
On branch master
nothing to commit, working tree clean

alan@lamp ~/GitPlay$ git status
On branch master
nothing to commit, working tree clean

alan@lamp ~/GitPlay$ ll
total 24
drwxr-xr-x 4 alan alan 4096 Jul  3 15:08 ./
drwxr-xr-x 8 alan alan 4096 Jul  3 16:05 ../
drwxr-xr-x 8 alan alan 4096 Jul  3 17:16 .git/
-rw-r--r-- 1 alan alan  139 Jul  3 15:03 .gitignore
-rw-r--r-- 1 alan alan  588 Jul  3 14:47 README.md
drwxr-xr-x 3 alan alan 4096 Jul  3 15:09 foo/
-rw-r--r-- 1 alan alan    0 Jul  3 15:08 shouldBeExcluded.txt

alan@lamp ~/GitPlay$ git add -f .

alan@lamp ~/GitPlay$ git status
On branch master
nothing to commit, working tree clean

所以现在我不能暂存和提交任何东西。

【问题讨论】:

  • 感谢您一直关注@acsrujan。我确实注意到了这个问题和接受的答案。但到目前为止,我很难证明“git add . 的真实性,这会将所有文件添加到.gitignore 中未指定的所有文件”。我还有更多的研究/实验要做......

标签: git


【解决方案1】:

虽然你有一个.gitignore 文件,但你还没有告诉 git。您需要先对其进行跟踪:

git add .gitignore

完成此操作后,您想要忽略的文件将被忽略。

运行git add . 不起作用的原因是.gitignore 仅适用于未跟踪的文件。添加所有文件会跟踪所有内容(甚至是您想要忽略的文件),因此此时不使用 .gitignore

【讨论】:

  • 谢谢你,@andy。这是有道理的,很高兴知道——必须明确地告诉 git 关于.gitignore 文件是一种优雅。正如(最初)所说,它确实回答了我的问题。 [我投了赞成票,但没有足够的声誉。] 结果证明我的问题很普通,尽管我相信仍然值得注意,所以我将编辑问题的顶部,并发布答案...
【解决方案2】:

这个问题可能很简单
如何让 git 根据 .gitignore 跟踪和提交文件?
正如@acsrujan 在问题 cmets 中指出的那样,git add all except ignoring files in .gitignore file 已经回答了这个问题。

但是,我的问题没有显示,
-- 由于markdown 伪影,“缩进四个空格以创建转义的&lt;pre&gt; &lt;code&gt;”--
是我从Git - gitignore documentation 复制/粘贴的.gitignore 文件,在my @987654327 的每个pattern 行中引入了前导空白 @ 文件。

这似乎与以 # 开头的行具有相同的效果——“以 # 开头的行用作注释。”——或者可能 git 将这些行视为空行—— - “空行不匹配任何文件,因此它可以用作分隔符以提高可读性。”来自Git - gitignore documentation 的引用。

删除前导空格解决了问题,.gitignore 文件按预期工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-24
    • 2017-12-30
    • 2017-01-16
    • 2019-10-30
    • 2015-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多