Developer toolsWebSecurity4 min· Alexey Bulatnikov

A git repository inside a cloud-sync folder: how a deploy removed 26 icons from our live site

We keep our working folders inside a cloud-sync app, so every project is on a second machine within minutes. It is convenient, and one day it took 26 product icons off this website. Nobody edited those files. The sync app renamed them, the build believed it, and the deploy did what it was told. Here is the chain of events and the short list of checks that prevents it.

What happened

  1. While we were working in the repository, the sync app decided that 26 SVG files had a conflict, and renamed each one from name.svg to name(1).svg. The contents were identical, byte for byte.
  2. To git, the originals were now deleted and 26 unknown files had appeared. Nobody ran git status before the build.
  3. The build copied the public folder as it found it, with the (1) names.
  4. The deploy script copies the build to the server and removes files that are no longer in it. It removed the 26 originals from the live site.
  5. Every page that showed one of those icons had a broken image until the next deploy.

Each tool did its job correctly. The sync app resolved what it saw as a conflict. The build copied a folder. The deploy mirrored it. The failure was in the space between them, where nothing checks that the working folder still matches the repository.

Why sync apps and repositories disagree

A sync app watches files and resolves conflicts by its own rules, one file at a time, knowing nothing about git. A build writes and rewrites many files in a few seconds, which is exactly the pattern a sync app can read as two versions of the same file. Replacing an image that is still uploading is another trigger: we have seen the new file renamed to name(1).jpg and the original dropped.

The checks we run before every deploy

git ls-files --deleted | wc -l      # tracked files missing from disk: should be 0
git status --short | grep '(1)\.'    # conflict copies: should print nothing

If either check finds something, restore the tracked files with git checkout on that folder and delete the copies. Compare first if you are unsure which side is newer; in our case the copies were identical to the committed files.

  • After a deploy, request a few static files from the live site and check that they answer. A deploy that reports success says nothing about what it removed.
  • When you replace media in a synced folder, save it under a new file name instead of overwriting the old one, or pause the sync app first.
  • Keep build output, dependency folders and caches out of the sync app with its ignore list. Fewer files changing at once means fewer conflicts.

Put the two commands at the top of the deploy script and make it stop when either one finds anything. A check that depends on someone remembering it will be skipped on the day it matters.

Should a repository live in a synced folder at all?

The safe answer is no: git already has a way to keep a copy elsewhere, and it is called a remote. We still do it, because documents, designs and code for one project then sit in one place, and because the hidden .git folder can be excluded from the upload. If you do the same, treat the sync app as a second writer to your working folder, because that is what it is, and check what it did before you ship.

We set up deploys with checks like these for small sites and shops, at a fixed price.

See website and server services