John Topley
John Topley

Reputation: 115292

How can I Remove .DS_Store files from a Git repository?

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

Upvotes: 1809

Views: 1182997

Answers (30)

LoveMob
LoveMob

Reputation: 566

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

Upvotes: 36

t0is
t0is

Reputation: 49

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.

Upvotes: 1

Turadg
Turadg

Reputation: 7681

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

Upvotes: 292

Ashlou
Ashlou

Reputation: 694

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

Upvotes: -1

Maksym Kosenko
Maksym Kosenko

Reputation: 595

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.

Upvotes: 0

benzado
benzado

Reputation: 84308

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!'

Upvotes: 3482

Kasem777
Kasem777

Reputation: 865

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.

Upvotes: 51

RajVimalC
RajVimalC

Reputation: 817

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

Upvotes: 62

stevec
stevec

Reputation: 52198

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

Upvotes: 13

Fernando Comet
Fernando Comet

Reputation: 36

I made:

git checkout -- ../.DS_Store

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

Upvotes: -1

2rahulsk
2rahulsk

Reputation: 508

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.

Upvotes: 4

Wael Assaf
Wael Assaf

Reputation: 1303

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

Upvotes: 2

Invincible
Invincible

Reputation: 1450

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

Upvotes: 6

manat
manat

Reputation: 1132

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

Upvotes: 7

user7718859
user7718859

Reputation:

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

Upvotes: 13

Cubiczx
Cubiczx

Reputation: 1115

Remove ignored files:

(.DS_Store)

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

Upvotes: 5

John Topley
John Topley

Reputation: 115292

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.

Upvotes: 6

zeozod
zeozod

Reputation: 81

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.

Upvotes: 7

Joshua Dance
Joshua Dance

Reputation: 10472

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!'

Upvotes: 32

Sunny
Sunny

Reputation: 831

Use this command to remove the existing files:

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

Then add .DS_Store to .gitignore

Upvotes: 19

Karthick Vadivel
Karthick Vadivel

Reputation: 431

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

Upvotes: 26

Reggie Pinkham
Reggie Pinkham

Reputation: 12708

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

git rm --cached -f *.DS_Store

Upvotes: 53

Ezequiel Garc&#237;a
Ezequiel Garc&#237;a

Reputation: 2776

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

Upvotes: 3

JLunceford
JLunceford

Reputation: 41

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

Upvotes: 4

dav1dhunt
dav1dhunt

Reputation: 179

When initializing your repository, skip the git command that contains

-u

and it shouldn't be an issue.

Upvotes: 4

Nerve
Nerve

Reputation: 6841

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.

Upvotes: 229

David Kahn
David Kahn

Reputation: 211

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

Upvotes: 19

vinny
vinny

Reputation: 370

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.

Upvotes: 1

JZ.
JZ.

Reputation: 21877

$ git commit -m "filter-branch --index-filter 'git rm --cached --ignore-unmatch .DS_Store"
$ git push origin master --force

Upvotes: 3

jordantbro
jordantbro

Reputation: 91

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

Upvotes: 9

Related Questions