简体   繁体   中英

git ignore all .DS_Store files

I have untracked files:

src/main/.DS_Store
src/main/java/.DS_Store
src/main/java/com/.DS_Store

I would like to git ignore all of those.DS_Store files. I tried putting **/.DS_Store in .gitignore , but no effect. Why? How to ignore them?

A plain .DS_Store on a single row should do it.

I have that row, and others, in my ~/.gitignore_global which makes it unnecessary to add that line to every git repository everywhere.

You can setup a global gitignore file with this configuration in your ~/.gitconfig:

[core]
        excludesfile = ~/.gitignore_global

and then keep on filling it up with more artifacts present on your system (IMO putting .DS_Store in the .gitignore of a project that is used by more developers than only those using Mac is a bit spammy, you can end up with a lot of different file patterns there.)


edit after comment:

This is what happens for me:

$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
    .DS_Store
    asdf2/.DS_Store

nothing added to commit but untracked files present (use "git add" to track)
$ echo .DS_Store > .gitignore
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
    .gitignore

nothing added to commit but untracked files present (use "git add" to track)

It might be caused by "!"directives in the .gitignore file.

If your project is a gradle project, gradle default .gitignore contains the lines

!**/src/main/**
!**/src/test/**

which keeps (because of the ! ) everything in src/main . If you have a complex project, with multiple .gitignore , the innermost .gitignore takes precedence. Thus, my main project had a .DS_Store line in its root .gitignore , but it was overridden by the (inner) gradle projects standard .gitignore .

Hence the solution: put the line .DS_Store somewhere after the

!**/src/main/**
!**/src/test/**

lines of your project (or alternatively, in a .gitignore in src , but this might be less practical.

With:

!**/src/main/**
!**/src/test/**
.DS_Store

everything should be fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM