Add .gitignore to gitignore

2019-01-16 07:41发布

问题:

Is it possible to add the .gitignore file to .gitignore itself?

.gitignore

Doesn't work though

I don't want to see it in edited files

回答1:

The .gitignore file is to prevent everyone who collaborates on a project to include some common files in a project, such as generated cache files. Therefore you should not ignore .gitignore, because it's purpose is to be included in the repo.

If you want to ignore files in just one repo but without committing the ignore list you can add them to .git/info/exclude in that repo.

If you want to ignore some files on every repo on your machine you can create the file ~/.gitignore_global and then run

git config --global core.excludesfile ~/.gitignore_global


回答2:

A .gitignore can ignore itself if it's never been checked in:

mhaase@ubuntu:~$ git --version
git version 1.7.9.5
mhaase@ubuntu:~$ git init temp
Initialized empty Git repository in /home/mhaase/temp/.git/
mhaase@ubuntu:~$ cd temp
mhaase@ubuntu:~/temp$ touch .gitignore foo bar baz bat
mhaase@ubuntu:~/temp$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       .gitignore
#       bar
#       bat
#       baz
#       foo
mhaase@ubuntu:~/temp$ echo "foo" >> .gitignore
mhaase@ubuntu:~/temp$ echo ".gitignore" >> .gitignore
mhaase@ubuntu:~/temp$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       bar
#       bat
#       baz
nothing added to commit but untracked files present (use "git add" to track)

If you check in .gitignore (before you tell it to ignore itself), then it will always show up in git status, even if you later modify it to ignore itself.



回答3:

There's not really a good reason to do this. If you want files ignored for your clone only, add them to .git/info/exclude, not .gitignore



回答4:

Yes you can; you still see it in edited files because it is still tracked by git, and files tracked by git are always marked as modified even if they are in .gitignore. So simply untrack it.

But why not committing or resetting changes that you have on it? It's a much better way to remove it from status... Also be aware that any fresh clone of you repo will have to add its .gitignore, which can be annoying.



标签: git gitignore