3

I am new to git, I am trying to create a .gitignore file to ignore file while git add. I understand that I have to do

vim .gitignore
*.c(ignore c file)
*.cpp(ignore cpp file)
'dotSTAR' ignore every hidden file

I was looking at this answer, It says that you have to do

'!/.gitignore'

as well

Even though I know that it is saying that while you are ignoring hidden files(.*), do not ignore that /.gitignore

Can anyone explain to me why gitignore should not be ignored? What purpose it is serving here?

Community
  • 1
  • 1
Max
  • 9,100
  • 25
  • 72
  • 109
  • If you do not exclude `.gitignore` from the list of ignored `.*` files, and try to add the file to your repository with `git add .gitignore`, you'll get an error (which can be overridden with `git add -f`. Since `.gitignore` only applies to untracked file, I think the question is really whether or not you want `git` to track `.gitignore`, and if not, do you want a reminder in the output of `git status` that it exists. – chepner Sep 16 '13 at 19:26
  • @chepner Thank you! I think in a distant way that is what I wanted to ask, What is the convention regarding tracking the .gitignore file? – Max Sep 16 '13 at 19:29
  • My guess is that it's a matter of personal taste. – chepner Sep 16 '13 at 19:32
  • 1
    @JoeDimaggio: In most cases, you'll likely want to track it. A C++ `.gitignore` file will differ greatly from a Python `.gitignore` file, for instance, and you'll often have executable or library names in there, and the like. It'd be really annoying if you had to create your own `.gitignore` file for every repo you cloned. – Crowman Sep 16 '13 at 19:32

2 Answers2

4

.gitignore is the list of files which git will not count as "untracked" in its status output. These are generally files that you don't want to be added to the repository. If you do not add .gitignore to your repository, anyone who clones it will not get this list! That makes it harder to collaborate since others won't know which files are supposed to be added.

Using a .gitignore and tracking it with git are not requirements of using git, they are merely good practices.

Max
  • 21,123
  • 5
  • 49
  • 71
  • And I believe if I do not track .gitignore, It will constantly appear in my git status -s. – Max Sep 16 '13 at 19:33
  • 2
    @JoeDimaggio: Unless you get it to ignore itself. – Crowman Sep 16 '13 at 19:34
  • @JoeDimaggio:- Yes it will and if you want to ignore it again use git rm --cached name_of_file – Rahul Tripathi Sep 16 '13 at 19:35
  • @RahulTripathi so just to be clear, Even if I add !/.gitignore in .gitignore, I can always unstage it using git rm chached, right? – Max Sep 16 '13 at 19:38
  • 1
    @JoeDimaggio I don't even bother with `!/.gitignore`. If you have a rule that excludes `.gitignore` just use `git add -f .gitignore` and afterwards it will be tracked normally. – Max Sep 16 '13 at 19:42
2

Run git rm --cached name_of_file and your file will be ignored again. .gitignore ignores the files that weren't tracked before

Also do check Ignore .gitignore in Git

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • I apologize, I am new to git so I am not accustomed to the terminology, Can you please elaborate it a bit for instance by giving step by step exmaple? – Max Sep 16 '13 at 19:23
  • @JoeDimaggio:- Do check out this link:- https://help.github.com/articles/ignoring-files – Rahul Tripathi Sep 16 '13 at 19:25