【发布时间】: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 initgit 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。
.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