~/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
And on and on and on. Swap files keep showing up in the untracked files list when I do:
$ git status
Therefore when I do my next commit, the swap files get added to the files that git is tracking. How do I make git ignore all swap files?
This seems like such a common problem that there should be a command:
$ git ignore --all-swap-files
Response to one of the comments:
~/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$