~/rails_projects/sample_app2 $ git branch
* master
~/rails_projects/sample_app2$ cat .gitignore
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
#
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile '~/.gitignore_global'
# Ignore bundler config.
/.bundle
# Ignore the default SQLite database.
/db/*.sqlite3
/db/*.sqlite3-journal
# Ignore all logfiles and tempfiles.
/log/*.log
/tmp
# Ignore other unneeded files.
database.yml
doc/
.*.s[a-w][a-z] #all swap files
.*.*.s[a-w][a-z]
.*.*.*.s[a-w][a-z]
*~
.project
.DS_Store
.idea
.secret
~/rails_projects/sample_app2$ touch .gitignore.swp
~/rails_projects/sample_app2$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 15 commits.
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# .gitignore.swp
nothing added to commit but untracked files present (use "git add" to track)
~/rails_projects/sample_app2$ git add .
~/rails_projects/sample_app2$ git commit -m "Add swap file"
[master 364570c] Add swap file
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 .gitignore.swp
~/rails_projects/sample_app2$ git rm --cached .gitignore.swp
rm '.gitignore.swp'
~/rails_projects/sample_app2$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 16 commits.
#
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: .gitignore.swp
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# .gitignore.swp
~/rails_projects/sample_app2$ git commit -m "Remove swap file"
[master 485217f] Remove swap file
0 files changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 .gitignore.swp
~/rails_projects/sample_app2$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 17 commits.
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# .gitignore.swp
nothing added to commit but untracked files present (use "git add" to track)
~/rails_projects/sample_app2$ $ git add .
~/rails_projects/sample_app2$ git commit -m "Trying NOT to add swap files"
[master d743282] Trying NOT to add swap files
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 .gitignore.swp
一直在继续。我这样做时,交换文件一直显示在未跟踪的文件列表中:
$ git status
因此,当我进行下一次提交时,交换文件会添加到git正在跟踪的文件中。如何让git忽略所有交换文件?
这似乎是一个常见的问题,应该有一个命令:
$ git ignore --all-swap-files
回应其中一条评论:
~/rails_projects$ mkdir test_gitignore
~/rails_projects$ cd test_gitignore/
~/rails_projects/test_gitignore$ touch .gitignore
~/rails_projects/test_gitignore$ echo '.*.s[a-w][a-z]' > .gitignore
~/rails_projects/test_gitignore$ git init
Initialized empty Git repository in /Users/7stud/rails_projects/test_gitignore/.git/
~/rails_projects/test_gitignore$ touch file1.txt .file1.swp
~/rails_projects/test_gitignore$ mkdir subdir
~/rails_projects/test_gitignore$ touch subdir/.file2.swp
~/rails_projects/test_gitignore$ ls -al
total 8
drwxr-xr-x 7 7stud staff 238 Sep 14 10:41 .
drwxr-xr-x 19 7stud staff 646 Sep 14 10:38 ..
-rw-r--r-- 1 7stud staff 0 Sep 14 10:40 .file1.swp
drwxr-xr-x 10 7stud staff 340 Sep 14 10:40 .git
-rw-r--r-- 1 7stud staff 15 Sep 14 10:40 .gitignore
-rw-r--r-- 1 7stud staff 0 Sep 14 10:40 file1.txt
drwxr-xr-x 3 7stud staff 102 Sep 14 10:41 subdir
~/rails_projects/test_gitignore$ ls -al subdir
total 0
drwxr-xr-x 3 7stud staff 102 Sep 14 10:41 .
drwxr-xr-x 7 7stud staff 238 Sep 14 10:41 ..
-rw-r--r-- 1 7stud staff 0 Sep 14 10:41 .file2.swp
~/rails_projects/test_gitignore$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# .gitignore
# file1.txt
nothing added to commit but untracked files present (use "git add" to track)
~/rails_projects/test_gitignore$
答案 0 :(得分:3)
以下是正在发生的事情:您的.gitignore
规则在其末尾有额外的空格。
Git将gitignore中的每个条目视为一个完整的忽略规则,正是出于同样的原因,你不应该在你的gitignore中有这个评论 - 模式会发生变化。
所以,.*.s[a-w][a-z]
虽然在.gitignore中看起来很明显,但恰好是字符串".*.s[a-w][a-z] "
因此git ignore无法匹配交换文件。
顺便说一句,你可以取消三个交换的ingore规则,而不是使用单个规则
*.s[a-w][a-z]
这是一个示例python片段,用于测试新模式匹配(或不匹配)的确切内容
In [1]: import fnmatch
In [2]: pattern = "*.s[a-w][a-z]"
In [3]: filenames = [".file.swp", "folder/.file.swp", "folder/subfolder/.file.swp", ".f.i.l.e.swp", "folder/.f.i.l.e.swp"]
In [4]: for filename in filenames:
...: print filename, fnmatch.fnmatch(filename, pattern)
...:
.file.swp True
folder/.file.swp True
folder/subfolder/.file.swp True
.f.i.l.e.swp True
folder/.f.i.l.e.swp True
In [5]: pattern = ".*.s[a-w][a-z]"
In [6]: for filename in filenames:
print filename, fnmatch.fnmatch(filename, pattern)
...:
.file.swp True
folder/.file.swp False
folder/subfolder/.file.swp False
.f.i.l.e.swp True
folder/.f.i.l.e.swp False
答案 1 :(得分:1)
问题很简单,就是行
.*.s[a-w][a-z] #all swap files
在.gitignore
文件中,评论只能在行的开头。将其更改为
#all swap files
.*.s[a-w][a-z]
它将按预期工作:
$ /bin/echo -e '.*.s[a-w][a-z] # swap' > .gitignore
$ touch .gitignore.swp
$ touch .x.swp
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: .gitignore
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore.swp
.x.swp
no changes added to commit (use "git add" and/or "git commit -a")
$ /bin/echo -e '.*.s[a-w][a-z]\n# swap' > .gitignore
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: .gitignore
no changes added to commit (use "git add" and/or "git commit -a")