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

前端 未结 10 782
心在旅途
心在旅途 2021-02-07 19:40

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

Can someone explain to me how I can m

相关标签:
10条回答
  • 2021-02-07 20:26

    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.

    0 讨论(0)
  • 2021-02-07 20:27

    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/

    0 讨论(0)
  • 2021-02-07 20:32

    Add the lines

     **/.DS_Store
     **/.gitignore
    
    0 讨论(0)
  • 2021-02-07 20:32

    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.

    0 讨论(0)
  • 2021-02-07 20:35

    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.

    0 讨论(0)
  • 2021-02-07 20:36

    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"
    
    0 讨论(0)
提交回复
热议问题