vickyqiu
vickyqiu

Reputation: 9269

.gitignore all the .DS_Store files in every folder and subfolder

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?

Upvotes: 924

Views: 531305

Answers (12)

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.

Upvotes: 5

stevec
stevec

Reputation: 52198

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".

Upvotes: 108

Kiran Jasvanee
Kiran Jasvanee

Reputation: 6554

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

Upvotes: 6

Edward Newell
Edward Newell

Reputation: 18429

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.

Upvotes: 1062

epicrato
epicrato

Reputation: 8398

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...)

Upvotes: 278

mate.gvo
mate.gvo

Reputation: 1182

  1. $ git rm ./*.DS_Store - remove all .DS_Store from git
  2. $ echo \.DS_Store >> .gitignore - ignore .DS_Store in future

commit & push

Upvotes: 7

vishnu
vishnu

Reputation: 241

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"

Upvotes: 9

ronald8192
ronald8192

Reputation: 5383

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

Upvotes: 494

travelingbones
travelingbones

Reputation: 8408

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!

Upvotes: 34

Yarin
Yarin

Reputation: 183419

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)

Upvotes: 111

Jacob
Jacob

Reputation: 407

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

Upvotes: 18

Andrew Wilhelm
Andrew Wilhelm

Reputation: 141

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!

Upvotes: 14

Related Questions