繁体   English   中英

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

[英]How do I get git to track and commit files according to .gitignore (from the beginning)?

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


我是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.之前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将根据下一个git add . .gitignore跟踪更改git add . / git commit ...向前?


根据git添加除忽略.gitignore文件中的文件外的所有文件

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

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

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

根据
git添加。 忽略.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 .的原因git add . 之所以不起作用是因为.gitignore仅适用于未跟踪的文件。 添加所有文件可跟踪所有内容(甚至包括您要忽略的文件),因此此时不使用.gitignore

这个问题本来可以很简单
如何让git根据.gitignore跟踪和提交文件?
正如@acsrujan在问题注释中指出的那样,已通过git添加了所有答案, 除了忽略.gitignore file中的文件

但是,我的问题没有显示出来,
-由于markdown伪像,“ 缩进四个空格以创建转义的<pre> <code> ”-
是,我的复制/粘贴.gitignore从文件的Git - gitignore文件 ,介绍了前导空格 的每个图案线.gitignore文件。

这似乎与以#开头的行具有相同的效果-“以#开头的行用作注释。 ”-或可能是git将该行视为空行-“ 空行不匹配任何文件,因此它可以用作可读性的分隔符。 “从Git-gitignore文档中引用。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM