How can I stop .gitignore from appearing in the li

2020-01-23 04:51发布

I just did a git init on the root of my new project.

Then I created a .gitignore file.

Now, when I type git status, .gitignore file appears in the list of untracked files. Why is that?

19条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-01-23 04:59

Just incase someone else has the same pain we had. We wanted to exclude a file that had already been committed.

This post was way more useful: working with .git/info/exclude too late

Specifically what you need to ignore a file is actually use the command git remove See git rm (http://www.kernel.org/pub/software/scm/git/docs/git-rm.html)

you test it by going

git rm --dry-run *.log
(if you say wanted to exclude all the log files)

this will output what would be excluded if you ran it.

then

you run it by going

git rm *.log
(or whatever filename path / expression you want to)

Then add a *.log line to your .gitignore file.

查看更多
ゆ 、 Hurt°
3楼-- · 2020-01-23 05:01

After you add the .gitignore file and commit it, it will no longer show up in the "untracked files" list.

git add .gitignore
git commit -m "add .gitignore file"
git status
查看更多
叛逆
4楼-- · 2020-01-23 05:03

The .gitignore file should be in your repository, so it should indeed be added and committed in, as git status suggests. It has to be a part of the repository tree, so that changes to it can be merged and so on.

So, add it to your repository, it should not be gitignored.

If you really want you can add .gitignore to the .gitignore file if you don't want it to be committed. However, in that case it's probably better to add the ignores to .git/info/exclude, a special checkout-local file that works just like .gitignore but does not show up in "git status" since it's in the .git folder.

See also https://help.github.com/articles/ignoring-files

查看更多
Lonely孤独者°
5楼-- · 2020-01-23 05:05

The idea is to put files that are specific to your project into the .gitignore file and (as already mentioned) add it to the repository. For example .pyc and .o files, logs that the testsuite creates, some fixtures etc.

For files that your own setup creates but which will not necessarily appear for every user (like .swp files if you use vim, hidden ecplise directories and the like), you should use .git/info/exclude (as already mentioned).

查看更多
虎瘦雄心在
6楼-- · 2020-01-23 05:06

Navigate to the base directory of your git repo and execute the following command:

echo '\\.*' >> .gitignore

All dot files will be ignored, including that pesky .DS_Store if you're on a mac.

查看更多
趁早两清
7楼-- · 2020-01-23 05:09

First of all, as many others already said, your .gitignore should be tracked by Git (and should therefore not be ignored). Let me explain why.

(TL;DR: commit the .gitignore file, and use a global .gitignore to ignore files that are created by your IDE or operating system)

Git is, as you probably already know, a distributed version control system. This means that it allows you to switch back and forth between different versions (even if development has diverged into different branches) and it also allows multiple developers to work on the same project.

Although tracking your .gitignore also has benefits when you switch between snapshots, the most important reason for committing it is that you'll want to share the file with other developers who are working on the same project. By committing the file into Git, other contributers will automatically get the .gitignore file when they clone the repository, so they won't have to worry about accidentally committing a file that shouldn't be committed (such as log files, cache directories, database credentials, etc.). And if at some point the project's .gitignore is updated, they can simply pull in those changes instead of having to edit the file manually.

Of course, there will be some files and folders that you'll want to ignore, but that are specific for you, and don't apply to other developers. However, those should not be in the project's .gitignore. There are two other places where you can ignore files and folders:

  • Files and folders that are created by your operating system or IDE should be placed in a global .gitignore. The benefit is that this .gitignore is applied to all repositories on your computer, so you don't have to repeat this for every repository. And it's not shared with other developers, since they might be using a different operating system and/or IDE.
  • Files that don't belong in the project's .gitignore, nor in the global .gitignore, can be ignored using explicit repository excludes in your_project_directory/.git/info/exclude. This file will not be shared with other developers, and is specific for that single repository
查看更多
登录 后发表回答