1706

How can I remove those annoying Mac OS X .DS_Store files from a Git repository?

jub0bs
  • 60,866
  • 25
  • 183
  • 186
John Topley
  • 113,588
  • 46
  • 195
  • 237

32 Answers32

3325

Remove existing .DS_Store files from the repository:

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

Add this line:

.DS_Store

to the file .gitignore, which can be found at the top level of your repository (or create the file if it isn't there already). You can do this easily with this command in the top directory:

echo .DS_Store >> .gitignore

Then commit the file to the repo:

git add .gitignore
git commit -m '.DS_Store banished!'
Kamlesh
  • 5,233
  • 39
  • 50
benzado
  • 82,288
  • 22
  • 110
  • 138
  • You could also use the exec option to find find . -name .DS_Store -exec git-rm {} \; It would be one less command. – Milhous Sep 21 '08 at 01:54
  • 18
    It's really trivial, but using `-exec` will launch `git-rm` once for every .DS_Store file, while `xargs` will put all the paths on one command line. Mostly I prefer `xargs` because I don't have to worry about escaping a lot of special characters. – benzado Sep 21 '08 at 07:13
  • 10
    This should use .git/info/exclude, not .gitignore. See http://stackoverflow.com/questions/1753070/git-ignore-files-only-locally/1753078#1753078 for an explanation of the difference between the two. – Andrew Grimm Dec 30 '09 at 05:35
  • 122
    @Andrew: I don't agree. It would never be useful for a .DS_Store file to be checked in, so it makes more sense as a repository-wide, all-users setting, rather than something each Mac user needs to remember to set on her own machine. – benzado Dec 30 '09 at 19:27
  • Works great, just need to change "git-rm" to "git rm" to work with newer versions of git. – braitsch Jun 14 '12 at 20:27
  • 72
    Does adding `.DS_Store` to `.gitignore` work recursively? That is, will it ignore `some/level/of/folders/.DS_Store` as well? – Charlie Schliesser Aug 08 '12 at 16:35
  • 89
    @CharlieS Yes, if the pattern does not contain a slash, it is matched against the file name in all directories. – benzado Aug 08 '12 at 17:02
  • 1
    Does the answer still applies on ubuntu? I am getting this usage error usage: git rm [options] [--] ... – pal4life Jul 29 '14 at 23:55
  • 2
    What about: `find . -name .DS_Store -delete`? – kenorb Sep 29 '15 at 19:06
  • @kenorb That will remove the files but won't notify git of the change, so you would still need to follow that up with `git add` or something like it. – benzado Sep 29 '15 at 22:13
  • 3
    You can also `find . -name .DS_Store -exec git rm -f --ignore-unmatch '{}' +` -- this works like `xargs` but is shorter to type. – Dietrich Epp Dec 13 '15 at 23:44
  • It's a shame that in 2016, the .gitignore for Swift at Gihub does not include DS_Store from the beginning... or does it? – Nicolas Miari Apr 15 '16 at 11:33
  • 1
    @NicolasMiari [it does](https://github.com/apple/swift/blob/4af9d0a1e3d434bccf9544976c54b22db83c2c18/.gitignore#L35), however I recommend to [ignore such files globally](http://stackoverflow.com/a/108108/2123108) – Alfred Bez Jun 01 '16 at 11:22
  • That is Apple's .gitignore, isn't it? I just created a repo and chose the Swift .gitignore, but .DS_Store **was missing** (had to add it and go throught all the cleanup mentioned in this answer). Github's official version (if my Google search led me to the right place) of the file seems to still be missing it: https://github.com/github/gitignore/blob/master/Swift.gitignore – Nicolas Miari Sep 20 '16 at 04:36
  • 1
    @NicolasMiari There's no point in discussing GitHub's gitignore project here, comment on the relevant pull request: https://github.com/github/gitignore/pull/2109 – benzado Sep 20 '16 at 21:05
  • 1
    @benzado Using -exec with + will only execute git rm one time: `find . -name .DS_Store -exec git rm -f {} +` https://linux.die.net/man/1/find – Avi Tevet Sep 15 '17 at 20:40
  • Noob question here: What is this type of command `find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch` called and how can I learn these? – remonses Jan 30 '18 at 17:13
  • @user122121 I'm not exactly sure what you mean by "type" of command; you should open a new question and/or read the Bash manual. – benzado Jan 31 '18 at 20:03
  • Downvote because this does not solve the underlying underlying [sic] problem. The other answer does. – William Entriken Mar 27 '18 at 22:59
  • @benzado I think it's weird for the repo to ignore contributor-specific temp files. What if Ubuntu started creating its own, we ignore them too? Same with Vim swapfiles, Emacs, etc. But... eh, I don't really care, I just use .gitignore for this cause it's easier, and everyone uses a Mac anyway. – sudo Aug 29 '18 at 15:40
  • 1
    @RajarshiGanguly that is find command and OP is piping the result to remove all >ds_store association. It is bash scripting – jeevjyot singh chhabda Feb 04 '19 at 18:59
  • 1
    @AndrewGrimm That's a pretty Grim way of looking at it. Actually it depends on the repo itself: policy, who's the ones committing/working on it, the purpose of the repo - and as was pointed out there is no reason to have .DS_Store in revision control anyway is there so why not have it as in .gitignore? One is not 'better' or more 'correct' than the other whatever one might say to the contrary. I use .gitignore and I have my reasons and since they're my repos it's my rules and not the rules of someone or some people on SO who say otherwise. – Pryftan Jun 30 '19 at 23:39
  • I get this issue: `find: -: unknown primary or operator` – mesqueeb Feb 02 '21 at 03:39
  • git status | grep DS_Store | awk '{print $1}' >> .gitignore – Mohammad Selim Miah Mar 03 '21 at 13:03
  • 1
    @MohammadSelimMiah That will ignore any existing `.DS_Store` files, and _only_ those files. Any new ones created in other directories won't be ignored. Also, you'll have multiple lines in your `.gitignore` when only one is needed. – benzado Mar 05 '21 at 04:36
  • @benzado I don't think it belongs to a repository wide configuration file, as Mac users are expected to adapt to the rest of us (not the opposite). – 6infinity8 May 22 '21 at 07:09
  • @6infinity8 What belongs or doesn't belong in `.gitignore` is up to the human beings who share the repository. No one else's opinions matter. The whole purpose of Git is to enable a group of humans to collaborate and build something together. Git is wonderful because it can adapt to the needs of different groups of humans. – benzado May 23 '21 at 16:32
  • 1
    @benzado `find . -name .DS_Store -exec git rm -f --ignore-unmatch {} +` is the same as `find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch`. In the `find` page of the man page manual: -exec utility [argument ...] {} + Same as -exec, except that ``{}'' is replaced with as many path- names as possible for each invocation of utility. This behaviour is similar to that of xargs(1). – hustnzj Nov 12 '21 at 14:59
  • Note for noobies like me: when I ran the command on the windows terminal, returned me an error message ```xargs : The term 'xargs' is not recognized as the name of a cmdlet...```. I had to open a git bash terminal and ran the command. Worked perfectly. – Jorge Mauricio Feb 19 '23 at 17:53
279

Combining benzado and webmat's answers, updating with git rm, not failing on files found that aren't in repo, and making it paste-able generically for any user:

# remove any existing files from the repo, skipping over ones not in repo
find . -name .DS_Store -print0 | xargs -0 git rm --ignore-unmatch
# specify a global exclusion list
git config --global core.excludesfile ~/.gitignore
# adding .DS_Store to that list
echo .DS_Store >> ~/.gitignore
Turadg
  • 7,471
  • 2
  • 48
  • 49
  • 3
    I found that when using a git repo in my home directory (AKA a "dotfiles" repo), having `~/.gitignore` as my global excludes file is a pain. There are files that I want to exclude from my home directory that I _don't_ want to ignore globally. I use `~/.gitexcludes` instead via the same directive: `core.excludesfile` – AL the X May 18 '15 at 21:35
209

The best solution to tackle this issue is to Globally ignore these files from all the git repos on your system. This can be done by creating a global gitignore file like:

vi ~/.gitignore_global

Adding Rules for ignoring files like:

# Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.o
*.so

# Packages #
############
# it's better to unpack these files and commit the raw source
# git has its own built in compression methods
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip

# Logs and databases #
######################
*.log
*.sql
*.sqlite

# OS generated files #
######################
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db

Now, add this file to your global git config:

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

Edit:

Removed Icons as they might need to be committed as application assets.

Nerve
  • 6,463
  • 4
  • 29
  • 29
  • You can also add above rules in your repository's .gitignore. This would keep away these files at repo level if there are multiple contributors to your project. However, in such cases, .gitignore should deal with every OS' specific files etc if different developers are using different OS. Like rules for linux, OSX etc. – Nerve Jul 13 '13 at 19:15
  • 3
    I would recommend that each developer deals with his os specific files in his own .global_ignore, that way the project .gitignore is project specific. – mcfedr Aug 08 '13 at 07:54
  • must run: git add .gitignore_global , in order another programmers have that file too in his local environment ? – stackdave Jun 18 '17 at 14:51
  • @stackdave No. That file isn't even under git control. That's in the home directory and they would have to update their git config too. There isn't even a reason to do it this way. No need to make a new file when there already exists the files for the same purpose. Each directory can have a .gitignore and there's also the repo specific info/exclude. Also - though I'm vague on this one - I think you can use ~/.gitignore too (not certain!). Oh and I have repos that have .jar files and .log files too so that could be a problem for some (and I imagine other people different extensions added here). – Pryftan Jun 08 '20 at 17:13
  • As I am new to get I find this to be a fantastic resource for me at first glance, but I was wondering if there is a video or article someone could point me to so I can understand the reasons behind why the file types are typically ignored. Thanks in advance! – timSully Jan 19 '21 at 22:32
66

In some situations you may also want to ignore some files globally. For me, .DS_Store is one of them. Here's how:

git config --global core.excludesfile /Users/mat/.gitignore

(Or any file of your choice)

Then edit the file just like a repo's .gitignore. Note that I think you have to use an absolute path.

Tim
  • 59,527
  • 19
  • 156
  • 165
webmat
  • 58,466
  • 12
  • 54
  • 59
50

If you are unable to remove the files because they have changes staged use:

git rm --cached -f *.DS_Store
Reggie Pinkham
  • 11,985
  • 4
  • 38
  • 36
47

Sometimes .DS_Store files are there at remote repository, but not visible at your local project folders. To fix this, we need to remove all cached files and add again.

Step 1: Add this to .gitignore file.

# Ignore Mac DS_Store files
.DS_Store
**/.DS_Store

Step 2: Remove the cached files and add again using these commands.

git rm -r --cached .
git add .
git commit -am "Removed git ignored files"
git push -f origin master
RajVimalC
  • 657
  • 6
  • 7
41

The best way to get rid of this file forever:

Make a global .gitignore file:

echo .DS_Store >> ~/.gitignore_global

Let Git know that you want to use this file for all of your repositories:

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

That’s it! .DS_Store will be ignored in all repositories.

McKinley
  • 1,123
  • 1
  • 8
  • 18
Kasem777
  • 737
  • 7
  • 10
30

Top voted answer is awesome, but helping out the rookies like me, here is how to create the .gitignore file, edit it, save it, remove the files you might have already added to git, then push up the file to Github.

Create the .gitignore file

To create a .gitignore file, you can just touch the file which creates a blank file with the specified name. We want to create the file named .gitignore so we can use the command:

touch .gitignore

Ignore the files

Now you have to add the line which tells git to ignore the DS Store files to your .gitignore. You can use the nano editor to do this.

nano .gitignore

Nano is nice because it includes instructions on how to get out of it. (Ctrl-O to save, Ctrl-X to exit)

Copy and paste some of the ideas from this Github gist which lists some common files to ignore. The most important ones, to answer this question, would be:

# OS generated files #
######################
.DS_Store
.DS_Store?

The # are comments, and will help you organize your file as it grows.

This Github article also has some general ideas and guidelines.

Remove the files already added to git

Finally, you need to actually remove those DS Store files from your directory.

Use this great command from the top voted answer. This will go through all the folders in your directory, and remove those files from git.

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

Push .gitignore up to Github

Last step, you need to actually commit your .gitignore file.

git status

git add .gitignore

git commit -m '.DS_Store banished!'

Joshua Dance
  • 8,847
  • 4
  • 67
  • 72
25

Open terminal and type "cd < ProjectPath >"

  1. Remove existing files: find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch

  2. nano .gitignore

  3. Add this .DS_Store

  4. type "ctrl + x"

  5. Type "y"

  6. Enter to save file

  7. git add .gitignore

  8. git commit -m '.DS_Store removed.'

Karthick Vadivel
  • 421
  • 5
  • 10
  • `nano .gitignore` creates the file if not present, this command should be executed from project root folder – Saif Aug 02 '17 at 07:44
  • 1
    @Saif Not necessarily. You can have .gitignore in any directory where you want to exclude some files in a subdirectory but not the root directory. I have this in a number of my repos for example. – Pryftan Jun 08 '20 at 17:16
19

I had to change git-rm to git rm in the above to get it to work:

find . -depth -name '.DS_Store' -exec git rm --cached '{}' \; -print
Nakilon
  • 34,866
  • 14
  • 107
  • 142
David Kahn
  • 211
  • 2
  • 4
18

Use this command to remove the existing files:

find . -name '*.DS_Store' -type f -delete

Then add .DS_Store to .gitignore

upful
  • 850
  • 10
  • 26
Sunny
  • 821
  • 6
  • 17
13

In case you want to remove DS_Store files to every folder and subfolder:


In case of already committed DS_Store:
find . -name .DS_Store -print0 | xargs -0 git rm --ignore-unmatch

Ignore them by:

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
11

Step 1

This will remove every .DS_Store file in a directory (including subdirectories)

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

Step 2

Add this to .gitignore to prevent any DS_Store files in the root directory and every subdirectory from going to git!

**/.DS_Store

From the git docs:

  • 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
10

delete them using git-rm, and then add .DS_Store to .gitignore to stop them getting added again. You can also use blueharvest to stop them getting created all together

Nathan
  • 11,938
  • 12
  • 55
  • 62
9

The following worked best for me. Handled unmatched files, and files with local modifications. For reference, this was on a Mac 10.7 system running git 1.7.4.4.

Find and remove:

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

I also globally ignore .DS_Store across all repositories by setting a global core.excludesfile.

First, create the file (if one doesn't already exist):

touch ~/.gitignore

Then add the following line and save:

.DS_Store

Now configure git to respect the file globally:

git config --global core.excludesfile ~/.gitignore
jordantbro
  • 91
  • 1
  • 2
7

I found that the following line from snipplr does best on wiping all .DS_Store, including one that has local modifications.

find . -depth -name '.DS_Store' -exec git-rm --cached '{}' \; -print

--cached option, keeps your local .DS_Store since it gonna be reproduced anyway.

And just like mentioned all above, add .DS_Store to .gitignore file on the root of your project. Then it will be no longer in your sight (of repos).

brooksrelyt
  • 3,925
  • 5
  • 31
  • 54
manat
  • 1,102
  • 11
  • 13
7

I'm a bit late to the party, but I have a good answer. To remove the .DS_Store files, use the following commands from a terminal window, but be very careful deleting files with 'find'. Using a specific name with the -name option is one of the safer ways to use it:

cd directory/above/affected/workareas
find . -name .DS_Store -delete

You can leave off the "-delete" if you want to simply list them before and after. That will reassure you that they're gone.

With regard to the ~/.gitignore_global advice: be careful here. You want to place that nice file into .gitignore within the top level of each workarea and commit it, so that anyone who clones your repo will gain the benefit of its use.

zeozod
  • 81
  • 1
  • 3
  • Indeed: whenever you are going to do a destructive command it is arguably best to start out by printing the files that will be affected before actually doing the destructive command. It can save you a great deal of pain (esp if you're one who doesn't backup though even those of us who regularly backup can be saved time this way)! – Pryftan Jun 08 '20 at 17:21
6

This will work:

find . -name "*.DS_Store" -type f -exec git-rm {} \;

It deletes all files whose names end with .DS_Store, including ._.DS_Store.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
John Topley
  • 113,588
  • 46
  • 195
  • 237
6

For some reason none of the above worked on my mac.

My solution is from the terminal run:

rm .DS_Store

Then run following command:

git pull origin master
Invincible
  • 1,418
  • 18
  • 23
  • 1
    that's because you are trying to remove it from the local. whereas the question asks about to remove it from git. – BlackPearl12 Nov 03 '20 at 11:08
  • One day I upvoted the answer and now I am here again, investigated more deeply and it is turned out gitignore has specific setup in our project. So .DS_Store files are not ignored under a couple of directories only. That is why none of the above did not help(e.g. gitignore has the highest priority over other places like global config or exclude). – Maksym Kosenko Nov 24 '22 at 15:34
5

Remove ignored files:

(.DS_Store)

$ find . -name .DS_Store -print0 | xargs -0 git rm --ignore-unmatch
Cubiczx
  • 1,005
  • 11
  • 10
4

When initializing your repository, skip the git command that contains

-u

and it shouldn't be an issue.

dav1dhunt
  • 179
  • 1
  • 10
4

This worked for me, combo of two answers from above:

  • $ git rm --cached -f *.DS_Store
  • $ git commit -m "filter-branch --index-filter 'git rm --cached --ignore-unmatch .DS_Store"
  • $ git push origin master --force
JLunceford
  • 41
  • 3
4

create a .gitignore file using command touch .gitignore

and add the following lines in it

.DS_Store

save the .gitignore file and then push it in to your git repo.

brooksrelyt
  • 3,925
  • 5
  • 31
  • 54
2rahulsk
  • 488
  • 4
  • 10
3

add this to your file .gitignore

#Ignore folder mac
.DS_Store

save this and make commit

git add -A
git commit -m "ignore .DS_Store"

and now you ignore this for all your commits

Ezequiel García
  • 2,616
  • 19
  • 12
3
$ git commit -m "filter-branch --index-filter 'git rm --cached --ignore-unmatch .DS_Store"
$ git push origin master --force
JZ.
  • 21,147
  • 32
  • 115
  • 192
1

There are a few solutions to resolve this problem. To avoid creating .DS_Store files, do not to use the OS X Finder to view folders. An alternative way to view folders is to use UNIX command line. To remove the .DS_Store files a third-party product called DS_Store Terminator can be used. To delete the .DS_Store files from the entire system a UNIX shell command can be used. Launch Terminal from Applications:Utilities At the UNIX shell prompt enter the following UNIX command: sudo find / -name ".DS_Store" -depth -exec rm {} \; When prompted for a password enter the Mac OS X Administrator password.

This command is to find and remove all occurrences of .DS_Store starting from the root (/) of the file system through the entire machine. To configure this command to run as a scheduled task follow the steps below: Launch Terminal from Applications:Utilities At the UNIX shell prompt enter the following UNIX command:

sudo crontab -e When prompted for a password enter the Mac OS X Administrator password. Once in the vi editor press the letter I on your keyboard once and enter the following:

15 1 * * * root find / -name ".DS_Store" -depth -exec rm {} \;

This is called crontab entry, which has the following format:

Minute Hour DayOfMonth Month DayOfWeek User Command.

The crontab entry means that the command will be executed by the system automatically at 1:15 AM everyday by the account called root.

The command starts from find all the way to . If the system is not running this command will not get executed.

To save the entry press the Esc key once, then simultaneously press Shift + z+ z.

Note: Information in Step 4 is for the vi editor only.

vinny
  • 360
  • 3
  • 10
1

No need to remove .DS_STORE locally

Just add it to .gitignore file

The .gitignore file is just a text file that tells Git which files or folders to ignore in a project.

Commands

  • nano .gitignore
  • Write .DS_Store Then click CTRL+X > y > Hit Return
  • git status To have a last look at your changes
  • git add .gitignore
  • git commit -m 'YOUR COMMIT MESSAGE'
  • git push origin master
Wael Assaf
  • 1,233
  • 14
  • 24
0

For those who have not been helped by any of the above methods - try to inspect your .gitignore more thoroughly, it could have some combination of rules between directories and subdirectories so the annoying .DS_Store files are not ignored in those folders only. For instance you want to ignore gensrc folders except ones in a custom directories, so you would have the following .gitignore:

.DS_Store
gensrc
!custom/**

So with this setup any/path/.DS_Store ignored, but not custom/gensrc/.DS_Store and the fix will be moving .DS_Store entry to the bottom of .gitignore file.

Maksym Kosenko
  • 515
  • 7
  • 12
0

I found a nice oneliner to get rid of this for good via global .gitignore file, therefore you will never need this thread again

git config --global core.excludesfile "~/.gitignore" &&  echo .DS_Store >> ~/.gitignore

This oneliner creates a global .gitignore used by every single repository and all the new ones you create in the future and ignores the .DS_Store file entirely.

t0is
  • 29
  • 7
0

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 your the root directory of your app and simply write

.DS_Store
._.DS_Store
**/.DS_Store
**/._.DS_Store

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

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

Other Solution

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
LoveMob
  • 46
  • 7
-1

I made:

git checkout -- ../.DS_Store

(# Discarding local changes (permanently) to a file) And it worked ok!

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • 1
    If you intend your answer as based on something else (e.g. something that did not work for you) then you need to reference more clearly. Please understand that "above" is not a reliable refernce here, because the display order of answers here is unpredictable. That is among other things because the order policy is configurable. – Yunnosch Nov 09 '20 at 08:19
-1

This method works if you are using the GitHub Desktop app.

  1. If not already, open your repository in the app (⌘O) enter image description here
  2. From Repository Tab, choose Repository Settings. enter image description here
  3. Go to "Ignored files" and add the file(s) you wish to be ignored. enter image description here
Ashlou
  • 684
  • 1
  • 6
  • 21