238

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

7ochem
  • 2,183
  • 1
  • 34
  • 42
  • 19
    why would you want to do that? just commit your changes. `.gitignore` is supposed to be part of your repository, listing file patterns that are junk for the project. – KurzedMetal Apr 16 '12 at 15:25
  • 5
    `.gitignore` should be part of your repository, so that everyone on your team is ignoring or checking in the same files. Just because `.gitignore` is in your code folders somewhere doesn't mean you have to deploy it. – Ryan Lundy May 23 '12 at 21:40
  • possible duplicate of [How do I tell Git to ignore ".gitignore"?](http://stackoverflow.com/questions/767147/how-do-i-tell-git-to-ignore-gitignore) – That Brazilian Guy Sep 09 '13 at 14:58
  • 2
    you can use `git rm --cached .gitignore` and untracking **.gitignore** – hassanzadeh.sd Jan 13 '20 at 13:29
  • The use case for ignoring .gitignore itself is if you want to just locally create a directory that should not be checked in and that no one else needs to know about. You should not have to check in `my-local-hack/.gitignore` when no one else should ever see that directory. – Christopher Barber Aug 23 '22 at 19:29
  • This is one of the epic questions in stackoverflow LOL – Drew Aguirre May 11 '23 at 00:55

6 Answers6

283

The .gitignore file's purpose is to prevent everyone who collaborates on a project from accidentally commiting some common files in a project, such as generated cache files. Therefore you should not ignore .gitignore, since it's supposed to be included in the repository.

If you want to ignore files in just one repository but want to avoid committing the ignore list (for example for personal files) you can add them to .git/info/exclude in that repository.

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

git config --global core.excludesfile ~/.gitignore_global
Gabriel Ravier
  • 358
  • 1
  • 6
  • 17
Lars Nyström
  • 5,786
  • 3
  • 32
  • 33
  • 10
    Although this is a detailed answer, it doesn't answer the question. "Should not" doesn't mean you can't. In my particular case I have a symlink that contains a .gitignore file, and now when I want to commit, git tries to include that .gitignore file. This seems like a perfect use-case IMO. – xorinzor Aug 09 '20 at 14:12
  • Why to complicated things when simple answer is NO. – darth vader Oct 01 '20 at 14:55
  • 1
    You can ignore the .gitignore file by adding it to the .gitignore file. I just tried it and it worked for me. – zachdyer Feb 22 '22 at 01:10
  • The solution with gitignore_global is quite interesting and elegant when you use tools that others does not. Someone asked me why I added intellij folders/files in the .gitignore but in my opinion it should not matter as everyone can use the tools they want. – рüффп Feb 24 '22 at 09:39
  • the solution with gitignore_global is quite interesting when you use tools that others does not. Someone asked me why I added intellij folders/files in the .gitignore but in my opinion it should not matter as everyone can use the tools they want. – рüффп Feb 24 '22 at 09:41
  • @zachdyer agreed. I added .gitignore to the .gitignore file and it worked fine for me as well. – Turq6ise Feb 24 '23 at 02:44
65

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.

Mark E. Haase
  • 25,965
  • 11
  • 66
  • 72
  • 3
    Exactly, this is my case. I want to have a local `.gitignore` for working with `git-svn`, which no-one should see. So I just added it to itself and it just worked. But then I googled to see if some uptight people will explain to others how this is "bad". This answer should be accepted, and then I would upvote the `.git/info/exclude` suggestions also. So it just needs to be untracked... Not very surprising, at least if you've ever been "subverted" (svn). – Tomasz Gandor Oct 21 '14 at 13:03
  • 2
    I have `.gitignore` file got backup first. Then I use `git rm .gitignore` and commit that change. At last, add and commit `.gitignore` file again with rule for `.gitignore`. It works! – qzhang Dec 17 '15 at 19:51
  • 7
    To fix this you can run `git rm --cached .gitignore`. – Gideon Apr 09 '18 at 11:36
  • Makes perfect sense. `.gitignore` ignores **untracked** files. If the `.gitignore` file itself is untracked, it can ignore itself too. _Yes, this violates the original purpose of `.gitignore` but that doesn't mean it can't be used this way._ – ADTC Aug 17 '21 at 20:05
37

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 in .gitignore file.

Venkataraman R
  • 12,181
  • 2
  • 31
  • 58
Daenyth
  • 35,856
  • 13
  • 85
  • 124
  • i am getting "Cannot complete the operation because of existing changes to the following file" and the file it lists it the one I have in exclude. Any idea why? – Paul McCarthy Aug 14 '19 at 10:30
11

After you enter .gitignore in your gitignore file, try the following,

git rm -r --cached .
git add --all
git commit -m "ignoring gitignore"
git push --set-upstream origin master

It should work although like already said, ignoring gitignore can be counter-productive if your repo is shared by multiple users.

senior_mle
  • 809
  • 1
  • 10
  • 20
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.

Community
  • 1
  • 1
CharlesB
  • 86,532
  • 28
  • 194
  • 218
  • Yes, and you will need to remove it separately from git by `got rm --cached .gitignore`, probably. – Tadeck Apr 16 '12 at 15:21
  • the git rm --cached .gitignore is removing from git. That is destructive and not desirable. How to just have .gitignore ignored for the local clone? – WestCoastProjects May 08 '15 at 14:56
1

Adding the folder or files into .git/info/exclude will help us in retaining the folder changes as well as not committing the changes to the repository.

In My case, I had a CMD folder which contained the .bat files, which I wrote to run the commands on the repository.

enter image description here

making the above changes in the .git/info/exclude file helped me in achieving my requirement.

S J BHAVANA
  • 1,421
  • 11
  • 8