[英]How do I get git to track and commit files according to .gitignore (from the beginning)?
Edit : 编辑 :
The detail in my question hides the true nature of the problem. 我的问题中的细节掩盖了问题的真实本质。 However, as this is how the problem is likely to manifest to others, I've decided to keep it as is and just add this edit, and the answer below.
但是,由于这是问题可能向其他人显现的方式,因此我决定保持原样,仅添加此编辑内容以及下面的答案。
I am completely new to git. 我是git的新手。
I'm working on a PHP-based website and would like to track only my files: 我正在基于PHP的网站上工作,只想跟踪我的文件:
ie only files in /plugins/my-plugins/
and /themes/my-theme/
. 即仅
/plugins/my-plugins/
和/themes/my-theme/
。
Having failed to get this to work I decide to create and play with a minimum working example. 未能成功解决这个问题,我决定创建一个最小的工作示例并进行尝试。
According to https://git-scm.com/docs/gitignore : 根据https://git-scm.com/docs/gitignore :
Example to exclude everything except a specific directory foo/bar (note the /* - without the slash, the wildcard would also exclude everything within foo/bar):
排除特定目录foo / bar以外的所有内容的示例(请注意/ *-不带斜杠,通配符还将排除foo / bar内的所有内容):
$ cat .gitignore
# exclude everything except directory foo/bar
/*
!/foo
/foo/*
!/foo/bar
So, I created files/folders to match: 因此,我创建了文件/文件夹来匹配:
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$
According to https://dzone.com/refcardz/getting-started-git : 根据https://dzone.com/refcardz/getting-started-git :
...initialize the directory as a Git repository by typing the following commands:
...通过键入以下命令将目录初始化为Git存储库:
git init
git add .
git commit –m 'The first commit'
Before git add.
在
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$
So, it's not tracking the files it shouldn't track. 因此,它不会跟踪不应跟踪的文件。 Which suggests it is tracking the files it should track.
这表明它正在跟踪应跟踪的文件。
So maybe I can just commit what is, apparently, being tracked: 因此,也许我可以提交显然可以追踪的内容:
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$
"nothing added to commit". “什么都没提交”。
OK, let's add something: 好,让我们添加一些东西:
alan@lamp ~/GitPlay$ git add .
And now see what happened: 现在看看发生了什么:
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$
Now it's planning to add everything in the next commit. 现在,它计划在下一次提交中添加所有内容。 Which is not what I want.
这不是我想要的。
Lets' see what actually happens: 让我们看看实际发生了什么:
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$
As I suspected, everything is committed. 正如我所怀疑的, 一切都已落实。
So, my question: 所以,我的问题是:
How do I get git to track and commit files according to .gitignore from the beginning? 如何从一开始就让git根据.gitignore跟踪和提交文件?
Or am I missing something fundamental? 还是我缺少基本的东西?
Is the 'correct' procedure 是正确的程序吗
to commit EVERYTHING in the very first commit regardless of .gitignore
, 不管
.gitignore
,在第一次提交中提交所有内容,
and then git will track changes according to .gitignore
from the next git add .
然后git将根据下一个
git add .
.gitignore
跟踪更改git add .
/ git commit ...
onwards? /
git commit ...
向前?
According to git add all except ignoring files in .gitignore file 根据git添加除忽略.gitignore文件中的文件外的所有文件
I think you mean
git add .
我认为您的意思是
git add .
which will add all of the files to the repo that AREN'T specified in the.gitignore
- you can see these changes by typinggit status
这会将所有文件添加到
.gitignore
指定的.gitignore
-您可以通过输入git status
来查看这些更改
git add .
This will add all paths and ignore matches from .gitignore这将添加所有路径,并忽略.gitignore中的匹配项
Which doesn't match my experience: git add .
这与我的经验不符:
git add .
adds ALL files and ignores .gitignore
. 添加所有文件,并忽略
.gitignore
。
According to 根据
Git add . git添加。 ignores .gitignore
忽略.gitignore
.gitignore
is taking into consideration only for new files..gitignore
仅考虑新文件。
Which does fit what I'm finding. 哪个适合我的发现。
Immediately following the first commit: 第一次提交后立即执行:
alan@lamp ~/GitPlay$ git status
On branch master
nothing to commit, working tree clean
So I thought, maybe clearing the staging cache will fix the problem: 所以我认为,也许清除暂存缓存将解决此问题:
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
So now I can't stage and commit anything. 所以现在我不能上演和提交任何东西。
While you have a .gitignore
file, you haven't told git about it. 当您拥有
.gitignore
文件时,您尚未将其告知git。 You need to track it first: 您需要先对其进行跟踪:
git add .gitignore
Once you've done that, then the files that you want ignored will be ignored. 完成此操作后,将忽略您要忽略的文件。
The reason that running git add .
运行
git add .
的原因git add .
didn't work is because .gitignore
only works for untracked files. 之所以不起作用是因为
.gitignore
仅适用于未跟踪的文件。 Adding all files makes everything tracked (even the files you want ignored), so at that point .gitignore
isn't used. 添加所有文件可跟踪所有内容(甚至包括您要忽略的文件),因此此时不使用
.gitignore
。
This question could have been simply 这个问题本来可以很简单
How do I get git to track and commit files according to .gitignore? 如何让git根据.gitignore跟踪和提交文件?
Which, as @acsrujan pointed out in the question comments, has been answered by git add all except ignoring files in .gitignore file . 正如@acsrujan在问题注释中指出的那样,已通过git添加了所有答案, 除了忽略.gitignore file中的文件 。
However, what my question did not show, 但是,我的问题没有显示出来,
-- due to a markdown artefact, " Indent four spaces to create an escaped <pre> <code>
block " -- -由于markdown伪像,“ 缩进四个空格以创建转义的
<pre> <code>
块 ”-
is that my copy/paste of a .gitignore
file from Git - gitignore documentation , introduced leading white space to each pattern line of my .gitignore
file. 是,我的复制/粘贴
.gitignore
从文件的Git - gitignore文件 ,介绍了前导空格 我的每个图案线.gitignore
文件。
This seems to have the same effect as beginning lines with #
-- " A line starting with # serves as a comment. " -- or possibly git treats such lines as blank lines -- " A blank line matches no files, so it can serve as a separator for readability. " Quotes from Git - gitignore documentation . 这似乎与以
#
开头的行具有相同的效果-“以#
开头的行用作注释。 ”-或可能是git将该行视为空行-“ 空行不匹配任何文件,因此它可以用作可读性的分隔符。 “从Git-gitignore文档中引用。
Removing the leading spaces solves the problem and the .gitignore
file works as expected. 删除前导空格可以解决此问题,
.gitignore
文件将按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.