gitignore just doesn't work. I can't get it to ignore .DS_Store & .gitignore files

北城以北 提交于 2019-12-20 19:41:25

问题


I have .gitignored .DS_Store and .gitignore files. But still see them in the "git status".

Can someone explain to me how I can make sure that the files that I am trying to ignore don't show up while checking status?

git status

# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   .DS_Store
#   .gitignore

I have the .DS_Store file in all the subfolders of my repository. So I want all of those to be ignored. What should I do?

I have configured .gitignore file in the root directory of my repository. Should I copy this to all the subfolders for git to ignore these files? Is adding .gitignore in .gitignore file not acceptable?

EDIT: Jan 21st 2014 ==================

I still can't figure this out. The comments below didn't really help me. So I am reposting my query.

Below is a snippet of my git status output. The actually output spans couple of pages. And all the files in this output are files/folders I don't want in my repo. These are untracked files so I have never git added them. But when I create a new project, I have to search for the related files/folders in this huge list before adding them, which is making the whole process a bit irritating.

# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   ../../.DS_Store
#   ../../Classes/
#   ../.DS_Store
#   ../Basics/.DS_Store
#   ../adt/.metadata/
#   ../adt/Fundamentals/.classpath
#   ../adt/Fundamentals/.project
#   ../adt/Fundamentals/.settings/
#   ../adt/Fundamentals/bin/
#   .DS_Store
#   .metadata/

Below is my .gitignore file. Can someone please point out my mistake?

*.class
*.pyc                                                                           
colors                                                                          
**/.DS_Store                                                                    

# Mobile Tools for Java (J2ME)                                                  
.mtj.tmp/                                                                       

# Package Files #                                                               
*.jar                                                                           
*.war                                                                           
*.ear                                                                           

#Eclipse files                                                                  
.project                                                                        

# folders                                                                       
.classpath/                                                                     
.settings/                                                                      
.metadata/                                                                      
WEB-INF/lib/                                                                    
META-INF/                                                                       
Servers/ 

# Byte-compiled / optimized / DLL files                                         
__pycache__/                                                                    
*.py[cod]                                                                       

# C extensions                                                                  
*.so                                                                            

# Distribution / packaging                                                      
bin/                                                                            
build/                                                                          
develop-eggs/                                                                   
dist/                                                                           
eggs/                                                                           
lib/                                                                            
lib64/                                                                          
parts/   

回答1:


  1. You shouldn't ignore your .gitignore. I don't think the ignore feature works when you don't have it in your repository. I'm not sure though.
  2. Files that are already tracked won't be ignored, even if you add them to .gitignore. If you want them to be ignored after they've been added to the repository already, you have to remove them with git rm filename first.



回答2:


If you want to ignore a change to a file that is tracked by git you can specify to git that it should assume that the file has not changed:

git update-index --assume-unchanged file

and to start tracking changes again do:

git update-index --no-assume-unchanged file

if you have already added a file that should be completely ignored (.gitignore) you have to git rm it first.

reference: https://www.kernel.org/pub/software/scm/git/docs/git-update-index.html
source: http://blog.pagebakers.nl/2009/01/29/git-ignoring-changes-in-tracked-files/




回答3:


This is what your .gitignore should look like:

.DS_Store

Also, the .gitignore is meant to be tracked, ignoring it doesn't make sense. So after you've updated the file to contain just .DS_Store do:

$ git add .gitignore
$ git commit -m "Track .gitignore"



回答4:


Why you’re ignoring .gitignore? This doesn’t make a sense. If you want to ignore some files just locally (i.e. don’t track and publish this setting to the remote repository), then use .git/info/exclude instead. Just add .DS_Store to it, that’s all.




回答5:


I had an issue getting .DS_Store ignored where the problem was that it had already been committed to the repo. Removing it resolved the issue.

$ git rm --cached .DS_Store



回答6:


Add the lines

 **/.DS_Store
 **/.gitignore



回答7:


I experience the same issue. The solution is to add a .gitignore under your home directory first.

My end the repository .gitignore no longer appears in indexing tracking.




回答8:


This is building off of Jakub Jirutka's response. I was a little confused upon first glance, but I tried it and it worked. Here is his answer stated - hopefully - more clearly.

His answer put simply: store unwanted files in .git/info/exclude (which is inside every git repository). Put each on a new line.

The .gitignore file is useful to help ignore files and directories that you know are or will be generated but are unwanted. The fact that you can git add .gitignore and then commit that and push it is really useful. When you clone this repository on another computer, for example, all of the files that should be ignored (after a ./configure; make; sudo make install;, for example, usually generates example files and a lot of other links) will be ignored (if they are in the .gitignore.

However, sometimes, one is not sure whether a file is wanted in the repository or wants the file to just exist outside of git control as if it were in a non-git-controlled directory. This can be done by adding this to the exclude file inside of the exclude file inside the .git/info directory.

Every git repository has a .git folder. To completely exclude a file or directory, go to the good ole terminal, cd to the root of the repository (i.e. cd src/best_repo_eva). Then you will see a .git/. Then, cd .git/info/. There will probably only be one file there called exclude. Use your favorite editor (just kidding; you may only use butterflies' wings to focus cosmic rays in the upper atmosphere to flip the correct bits in your drive platter ;) to edit the exclude such that it contains the files you want to exclude on new lines.

Easier solution: You have two files you want excluded: tmp1 and ignoreadf

Go to terminal and...

  • cd src/best_repo_eva
  • echo -e "tmptab\n igntab" >> .git/info/exclude

The tabs, obviously, only work if your terminal supports it. Note the -e (supports backslash escapes) and the \n (which is emphasized with a space) between the the two files.

Storing a lot of files with a similar ending, could be as easy as cd src/best_repo_eva then echo -e $(ls -1 | grep "tmp") >> .git/info/exclude (that's ls -ONE not ls -L - note that ls -a -1 doesn't work, unfortunately). That would store all files that contain the sequence "tmp" into the exclude file.




回答9:


I had the same exact issue on Mac OS X. I had .gitignore file in the repo but it just won't work.

What was weird when I wanted to print the .gitignore content to the console with the cat command it printed as it was empty. When opened with vim it appeared as normal.

The solution was to remove the file, create a new one and put the content into it again. After that, ignoring files started to work and when I done cat .gitignore it printed properly.



来源:https://stackoverflow.com/questions/21043132/gitignore-just-doesnt-work-i-cant-get-it-to-ignore-ds-store-gitignore-fil

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!