Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add .gitignore to gitignore

Tags:

git

gitignore

People also ask

How add ignore to Gitignore?

If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a . gitignore rule for it. Using the --cached option with git rm means that the file will be deleted from your repository, but will remain in your working directory as an ignored file.

How do I add an existing Gitignore file?

Simply move the files to a folder outside of git, then do "git add .", "git commit". (This removed the files) then add the gitignore, referencing the files/folders, commit again to add the gitignore file to git, then copy/move back in the folders, and they should be ignored.

Do I have to add Gitignore?

Normally yes, . gitignore is useful for everyone who wants to work with the repository. On occasion you'll want to ignore more private things (maybe you often create LOG or something. In those cases you probably don't want to force that on anyone else.

Should Gitignore ignore itself?

No, . gitignore needs to be checked in so that other users ignore the same files.


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

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.


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.


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.