864

I've added .DS_Store to the .gitignore file, but it seems that it is only ignoring .DS_Store in the root directory, not in every folder and subfolder.

How do I fix this?

Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
vickyqiu
  • 8,669
  • 3
  • 13
  • 4
  • 4
    possible duplicate of [How Can I Remove .DS\_Store Files From A Git Repository?](http://stackoverflow.com/questions/107701/how-can-i-remove-ds-store-files-from-a-git-repository) –  Aug 23 '13 at 02:04
  • 1
    How exactly did you add it to .gitignore? It should work in all directories (does for me). – Thilo Aug 23 '13 at 02:06
  • 1
    Its working for me as well. I also tried when its at the end of the file, if you have to have a (platform specific) newline but that didn't change that .DS_Store directories within any part of the hierarchy was still ignored. – enorl76 Aug 23 '13 at 17:35
  • 1
    The question as I understand it was: How to easily ignore all occurrences of .DS_Store in all subdirectories without manually doing this in every subdirectory. I had the same problem. The below answer shows how to solve it (the last 2 paragraphs). – petrsyn Nov 21 '13 at 23:42

12 Answers12

1006

I think the problem you're having is that in some earlier commit, you've accidentally added .DS_Store files to the repository. Of course, once a file is tracked in your repository, it will continue to be tracked even if it matches an entry in an applicable .gitignore file.

You have to manually remove the .DS_Store files that were added to your repository. You can use

git rm --cached .DS_Store

Once removed, git should ignore it. You should only need the following line in your root .gitignore file: .DS_Store. Don't forget the period!

git rm --cached .DS_Store

removes only .DS_Store from the current directory. You can use

find . -name .DS_Store -print0 | xargs -0 git rm --ignore-unmatch

to remove all .DS_Stores from the repository.

Felt tip: Since you probably never want to include .DS_Store files, make a global rule. First, make a global .gitignore file somewhere, e.g.

echo .DS_Store >> ~/.gitignore_global

Now tell git to use it for all repositories:

git config --global core.excludesfile ~/.gitignore_global

This page helped me answer your question.

blkpingu
  • 1,556
  • 1
  • 18
  • 41
Edward Newell
  • 17,203
  • 7
  • 34
  • 36
  • 81
    ´´git rm --cached .DS_Store´´ removes only one .DS_Store from the current directory. Use ´´find . -name .DS_Store -print0 | xargs -0 git rm --ignore-unmatch´´ to remove all .DS_Stores from the repo – auco Feb 12 '14 at 14:20
  • 5
    A list of other common files to ignore might be handy – geotheory Mar 17 '15 at 22:22
  • 21
    Bad idea to use global rule IMO: Trying to maintain global configs across multiple users/machines never works- you want the rules governing your repo in the repo itself. [Agree with this](http://stackoverflow.com/questions/107701/how-can-i-remove-ds-store-files-from-a-git-repository#comment1897703_107921) – Yarin Mar 25 '15 at 16:08
  • 24
    I don't agree with you. (Even though I upvoted your comment since I think it's insightful.) If we do global, then every mac user needs to do that, but only once. If we do repository wide, then we need to do that for every repository. I don't know about you, but I make new repos all the time. By doing it globally, one solves the problem once and for all. – Edward Newell Mar 26 '15 at 22:50
  • 2
    Awesome answer! It's not recommend for Global ignore because hard to team collaboration. – Startry Sep 18 '15 at 05:26
  • 2
    Use `find . -name .DS_Store -print0 | xargs -0 git rm --ignore-unmatch -f`if you have already staged some .DS_Store files. – Timur Bernikovich Oct 17 '15 at 19:06
  • 2
    it works, only a few tag is missing as of today to make it work. I used it for the ".idea" folders. The only addition here is the -r -f tag I needed to add. I hope it helps: # find . -name .idea -print0 | xargs -0 git rm -r -f --ignore-unmatch – Botond Bertalan May 27 '18 at 09:58
  • 1
    I had staged the files but not committed them. Unstaging the files (`git reset -- `) did not work (they were shown as new). I had to commit them then do the `git rm` to remove them. – pamcevoy May 11 '20 at 14:02
  • 1
    I ran the git remove command but the PR is unchanged. It seems to have deleted locally only. when I try to commit the changes it says there's none to commit. I also tried pushing with force but that obv didn't work since there are no commits – Pierre L Jun 11 '21 at 20:50
  • Here's how you get the path to the global Git ignore file: `git config --get core.excludesfile`. For me it is different than the path in this answe. – Tomáš Hübelbauer Mar 28 '23 at 16:55
460

Add**/.DS_Store into .gitignore for the sub directory


If .DS_Store already committed:

find . -name .DS_Store -print0 | xargs -0 git rm --ignore-unmatch

To ignore them in all repository: (sometimes it named ._.DS_Store)

echo ".DS_Store" >> ~/.gitignore_global
echo "._.DS_Store" >> ~/.gitignore_global
echo "**/.DS_Store" >> ~/.gitignore_global
echo "**/._.DS_Store" >> ~/.gitignore_global
git config --global core.excludesfile ~/.gitignore_global
ronald8192
  • 5,003
  • 2
  • 14
  • 23
  • 1
    still uploaded into my repo ? – Freddy Sidauruk Nov 24 '16 at 04:27
  • 1
    @FreddySidauruk use `git rm --cached your_file` first – ronald8192 Nov 24 '16 at 05:28
  • 1
    @FreddySidauruk Should be `find . -name .DS_Store -print0 | xargs -0 git rm --ignore-unmatch`, from: http://stackoverflow.com/questions/18393498/gitignore-all-the-ds-store-files-in-every-folder-and-subfolder/26454988#comment46723678_19299889 – ronald8192 Dec 02 '16 at 07:40
  • 2
    It seems adding `-f` is needed if you have open files in the relavent folders: `find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch` – Mehmet Kaplan Jan 25 '19 at 10:04
  • 1
    And the same in one line: `printf "%b\n" ".DS_Store\n._.DS_Store\n**/.DS_Store\n**/._.DS_Store\n" >> ~/.gitignore_global && git config --global core.excludesfile ~/.gitignore_global` – ikaerom Dec 28 '22 at 14:28
256

If .DS_Store was never added to your git repository, simply add it to your .gitignore file.

If you don't have one, create a file called

.gitignore

In the root directory of your app and simply write

**/.DS_Store

In it. This will never allow the .DS_Store file to sneak in your git.

But, if it's already there, write in your terminal:

find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch

then commit and push the changes to remove the .DS_Store from your remote repo:

git commit -m "Remove .DS_Store from everywhere"

git push origin master

And now add .DS_Store to your .gitignore file, and then again commit and push with the 2 last pieces of code (git commit..., git push...)

drjorgepolanco
  • 7,479
  • 5
  • 46
  • 47
  • 3
    Why double asterisk? What does that mean? – MilanG Jan 16 '17 at 14:02
  • 2
    @MilanG it's a keyword that basically means 'search in this directory and all subdirectories'. – lachie_h Apr 05 '17 at 14:53
  • 1
    Not sure that is a perfect explanation of what the double asterisk stands for. It is a wildcard. Could `*` or `**` depending on what you want to achieve. It is a way of telling the shell how to navigate directories. This stackoverflow answer explains it fully https://stackoverflow.com/a/28199633/4092170 – El'Magnifico Jan 03 '18 at 20:45
  • ** will never match hidden (./) directories : https://facelessuser.github.io/wcmatch/glob/ – Jonathan Aug 29 '19 at 21:11
  • worked like a charm. you have to do it step by step as indicated here, otherwise it won't work – Meg Nov 02 '20 at 14:06
  • Also mind if you try to adapt this example for non-`.DS_store`-files: If you want to exclude all files with this suffix you need to set the wildcard (asterisk `*`) also for the file name: `**\*.csv` – Markus Aug 03 '21 at 12:28
  • 1
    This is cool, I also use `find . -name '.DS_Store' -type f -delete` to delete all created .DS_store recursively – Robby Alvian Jaya Mulia Jun 18 '23 at 10:05
107

Your .gitignore file should look like this:

# Ignore Mac DS_Store files
.DS_Store

As long as you don't include a slash, it is matched against the file name in all directories. (from here)

Community
  • 1
  • 1
Yarin
  • 173,523
  • 149
  • 402
  • 512
90

Simply place this on a new line in .gitignore

**/.DS_Store

From git documentation

  • A leading "**" followed by a slash means match in all directories. For example, "**/foo" matches file or directory "foo" anywhere, the same as pattern "foo". "**/foo/bar" matches file or directory "bar" anywhere that is directly under directory "foo".
stevec
  • 41,291
  • 27
  • 223
  • 311
  • 7
    As noted in the documentation you quote, `**/.DS_Store` is just a more complicated way of writing `.DS_Store`. You only need the double star when you want to gitignore a specific path pattern, regardless of where it occurs. For example `**/foo/.DS_Store` would hide `.DS_Store` files only if found in a `foo` subdirectory (regardless of where `foo` is). – orlp Apr 10 '22 at 08:26
  • 2
    @orlp has it always been like that? I would have sworn I'd added .DS_Store to .gitignore a million times, only to have .DS_Store appear in subdirectories. After testing your information it appears you're absolutely right. It changes my thinking on this SO question. But also more generally: there's a (high) risk of accidentally ignoring files in subdirectories that happen to be the same name as another that is intentionally ignored elsewhere. Seems like a bad (albeit convenient) for git to behave like that. Thanks for pointing it out. – stevec Apr 10 '22 at 10:33
31

Step 1, delete all the *.DS_store files. One can run

git rm -f *.DS_Store

but be aware that rm -f can be a bit dangerous if you have a typo! Step two: add

*.DS_Store
.DS_Store

to .gitignore. This worked for me!

travelingbones
  • 7,919
  • 6
  • 36
  • 43
18

Add *.DS_Store to your .gitignore file. That works for me perfectly

Jacob
  • 407
  • 5
  • 8
14

You can also add the --cached flag to auco's answer to maintain local .DS_store files, as Edward Newell mentioned in his original answer. The modified command looks like this: find . -name .DS_Store -print0 | xargs -0 git rm --cached --ignore-unmatch ..cheers and thanks!

Andrew Wilhelm
  • 141
  • 1
  • 4
9

Step :1)Remove the existing files using this command

find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch

Step : 2)Add .DS_Store in your .gitignore file

Step :3) Commit your changes in .gitignore git add .gitignore git commit -m "removed .DS_Store"

vishnu
  • 241
  • 3
  • 4
6
  1. $ git rm ./*.DS_Store - remove all .DS_Store from git
  2. $ echo \.DS_Store >> .gitignore - ignore .DS_Store in future

commit & push

mate.gvo
  • 1,093
  • 14
  • 20
6

You should add following lines while creating a project. It will always ignore .DS_Store to be pushed to the repository.

*.DS_Store this will ignore .DS_Store while code commit.
git rm --cached .DS_Store this is to remove .DS_Store files from your repository, in case you need it, you can uncomment it.

## ignore .DS_Store file.
# git rm --cached .DS_Store
*.DS_Store
Kiran Jasvanee
  • 6,362
  • 1
  • 36
  • 52
1

In case you want to do this globally so you never have to think about .DS_Store again:

  1. Find the path to the global Git ignore file: git config --get core.excludesfile
  2. Append .DS_Store to the global Git ignore file
  3. Re-run your Git CLI command / IDE action

From this point on, Git will act as if you've placed the patterns in the global Git ignore file into your local ignore file every time.

Tomáš Hübelbauer
  • 9,179
  • 14
  • 63
  • 125