MacOS Big Sur creates duplicate versions of files

command linemacos

Several times in a day, I notice duplicate versions of my files being created. Refer to the screenshot – empty.html.erb was duplicated, and a new empty.html 2.erb was created.

What is causing this, and how can I stop this?

enter image description here

I feel like this might have something to do with TimeMachine and backups, since it only started after I enabled backups to iCloud and moved my files from ~ to ~/Documents/

Best Answer

The problem seems to be that you're using git ontop of iCloud Drive. I.e. the problem is not with Time Machine and backups at all - it is instead that you have chosen to store your ~/Documents/ folder in iCloud Drive, which is not compatible with programs such as git.

The issue is that iCloud Drive treats each file as an individual and independent file, whereas git assumes a specific relationship between the files. Git uses hidden files that contain information about other files in the folder - it expects them to be "in sync" - and it expects it is the only program manipulating the files; this assumption does not necessarily hold here.

Imagine for example that you run a git operation that touches several files in your folder - it might move stuff around, delete and recreate, change access times, etc. It does so very quickly while keeping its own state. iCloud Drive is triggered by these changes and starts a sync - however there's a lag between git something and the sync being triggered. Then later iCloud Drive might have sent something to the server and received data back, and it goes to store it and sees that the file (or its metadata) has changed underneath it. Now iCloud Drive has a conflict that could be solved by overwriting your local file, trashing the file just received from the server - or as the case is here - preserving the new data by saving it with a "2", "3", "4", etc. suffix.

This problem is common with almost any file syncing system, so you would experience similar problems with Dropbox, Google Drive, etc.

The solution to this is to not an iCloud Drive backed Documents folder, or move your git repository out of the Documents folder.