如何让 git 根据 .gitignore 跟踪和提交文件(从一开始就)?



Edit
我问题中的细节掩盖了问题的真实本质。但是,由于这就是问题可能向其他人显现的方式,因此我决定保持原样,只需添加此编辑和下面的答案。


我对 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 添加除忽略 .gitignore 文件中的文件之外的所有内容

我想你的意思是git add .它将.gitignore中未指定的所有文件添加到存储库中 - 您可以通过键入git status来查看这些更改

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

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

根据
Git add . 忽略 .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

所以现在我不能上演和提交任何东西。

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

git add .gitignore

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

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

这个问题可能很简单
如何让 git 根据 .gitignore 跟踪和提交文件?
正如@acsrujan在问题评论中指出的那样,除了忽略 .gitignore 文件中的文件外,git add all 已经回答了这个问题。

然而,我的问题没有显示的是,
由于降价伪影"缩进四个空格以创建一个转义的<pre> <code>"——
是我从 Git - gitignore 文档中复制/粘贴.gitignore文件,在我的.gitignore文件的每个模式行中引入了前导空格

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

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

相关内容

最新更新