All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

How to remove the .DS_Store file from a Git repository?

Post cover: How to remove the .DS_Store file from a Git repository?
This content has been automatically translated from Ukrainian.
In this post, we will discuss how to add .DS_Store to the .gitignore file and remove already added files from our repository. But first, we need to understand what .DS_Store is, what it is for, why it ends up in Git, and how to delete it.

What is .DS_Store?

.DS_Store (Desktop Services Store) is a hidden system file created by macOS to store information about the display of folders in Finder, such as icon positions, window sizes, background colors, etc.
If you open a folder in Finder and change the position of an icon - Finder will save the position information in .DS_Store. By default, this file is hidden, but it can be seen in the terminal. Even if you enable the display of hidden files in Finder - .DS_Store will remain hidden from you. Read more about hidden files in the previous post - What does the dot at the beginning of the file mean (.gitignore, .DS_Store, .bashrc, etc.)?

Why does .DS_Store end up in Git?

When you are working on a project on macOS and using Git for version control, .DS_Store files can accidentally end up in your repository along with other files. This happens because, by default, Git does not ignore these files, and they can be accidentally added to the index. Let's consider an example. I use GitHub Desktop to work with Git and clone a new repository to my local machine. The first thing I do is create a test.txt file:
Додав test.txt
Додав test.txt
Note that Git sees changes only to the new file (created using VSCode and saved in this repo's folder). This is because Finder currently has no custom settings for this file and folder in general. Let's open the folder in Finder, change the position of the file (icons) in tile mode, and see that a new .DS_Store has been added to the list of changes in Git:
Додався .DS_Store
Додався .DS_Store
Imagine that you made a commit, adding .DS_Store to the repository of a project that many people are working on. Firstly, no one needs your Finder configurations in the project repo. Secondly, these files clutter the commit history and the repo as a whole.

How to prevent .DS_Store from being added to the repo?

It's simple - ignore it. I mean - add it to the .gitignore file.
Just add this line:
.DS_Store
If the .gitignore file does not exist yet - create it (using a code editor or through the terminal).
You can create this file in the terminal using touch:
touch .gitignore
And you can add the text '.DS_Store' to the .gitignore file using echo:
echo .DS_Store >> .gitignore
Створений .gitignore
Створений .gitignore
After this step, people usually try to create a new folder, add a new file to it, and change the position of the file in Finder to check if .DS_Store is truly ignored.
And most likely - new .DS_Store files will appear and will not be ignored. But why?

Why does .gitignore not work and does not ignore .DS_Store?

Let's run the following commands in the terminal to achieve the desired result (ignoring .DS_Store):
find . -name '.DS_Store' -type f -delete
git rm -r --cached .
git add .
git commit -m "Remove .DS_Store files"
After this, we will get a new commit with the removed .DS_Store files, and new ones will be ignored and not added to the file index.
Коміт з видаленим .DS_Store
Коміт з видаленим .DS_Store
But let's break down what we ran in the terminal:
  • First, you delete all .DS_Store files from the file system using the find ... -delete command.
  • Then you remove all files from the Git index (not from the file system) using the git rm --cached command.
  • You add all files to the index again to ensure that .DS_Store files will not make it into the new commit because they are now ignored by .gitignore.
  • You create a new commit with a message that describes the changes made.
In fact, it all comes down to the cache. So we cleared it. Of course, you can selectively clear the cache for a single file:
git rm --cached `.DS_Store`
But I prefer a recursive clear of the entire cache (this is a more universal way):
git rm -r --cached .

This post doesn't have any additions from the author yet.

We are writing a demo game Drones vs Zombies (Gosu / Ruby)
12 Jul 12:17

We are writing a demo game Drones vs Zombies (Gosu / Ruby)

meme code
meme code@memecode
How to fix a Windows crash caused by CrowdStrike?
19 Jul 13:53

How to fix a Windows crash caused by CrowdStrike?

meme code
meme code@memecode
What does .map(&:name) mean in Ruby?
28 Jul 11:18

What does .map(&:name) mean in Ruby?

meme code
meme code@memecode
How does the map method work in Ruby? Overview of the method's operation with examples
30 Jul 07:33

How does the map method work in Ruby? Overview of the method's operation with examples

meme code
meme code@memecode
What does a dot at the beginning of a file (.gitignore, .DS_Store, .bashrc, etc.) mean?
02 Aug 13:15

What does a dot at the beginning of a file (.gitignore, .DS_Store, .bashrc, etc.) mean?

meme code
meme code@memecode
What is .gitignore? What is it for and how to use it
02 Aug 14:58

What is .gitignore? What is it for and how to use it

meme code
meme code@memecode
What is an idempotent method?
21 Aug 20:57

What is an idempotent method?

meme code
meme code@memecode
What is a repository?
21 Aug 21:25

What is a repository?

meme code
meme code@memecode
What is a commit in the context of programming and SCM / Git?
21 Aug 21:37

What is a commit in the context of programming and SCM / Git?

meme code
meme code@memecode
What is SCM (Source Control Management)?
21 Aug 21:46

What is SCM (Source Control Management)?

meme code
meme code@memecode
What hierarchy does the DOM (Document Object Model) have?
23 Aug 09:22

What hierarchy does the DOM (Document Object Model) have?

meme code
meme code@memecode
How does the artificial intelligence model work?
15 Sep 16:42

How does the artificial intelligence model work?

meme code
meme code@memecode