Repository: iterative/dvc

Issue:
git checkout some-file causes dvc to try to remove a dvc-managed file
When I `git checkout some-file` to clean away my local changes to that file, the dvc hook butts in and says:

> ERROR: unexpected error - unable to remove 'data/qix/1a285cea-7b71-4492-9b4d-87092bfb9869.json.gz' without a confirmation from the user. Use '-f' to force.

But I did not request removal of that file, and I don't understand why DVC is trying to do that.

Note that I'm _not_ switching to a different branch/tag/commit, because I specified a path to `git checkout`. So I'm only modifying files in my working tree. I don't think that should trigger DVC to [try to] remove files.

Platform: macOS, Python 3.7.4 virtualenv installed using `pyenv`, and:
```bash
$ dvc --version
0.54.1+5e4d26
```

Discussion:

Patch:
diff --git a/dvc/scm/git/__init__.py b/dvc/scm/git/__init__.py
index 290ca5802..582d777dd 100644
--- a/dvc/scm/git/__init__.py
+++ b/dvc/scm/git/__init__.py
@@ -216,7 +216,11 @@ class Git(Base):
         return [t.name for t in self.repo.tags]
 
     def _install_hook(self, name, cmd):
-        command = '[ -z "$(git ls-files .dvc)" ] || exec dvc {}'.format(cmd)
+        command = (
+            '[ "$3" == "0" ]'
+            ' || [ -z "$(git ls-files .dvc)" ]'
+            " || exec dvc {}".format(cmd)
+        )
 
         hook = os.path.join(self.root_dir, self.GIT_DIR, "hooks", name)
 


Test patch:
diff --git a/tests/func/test_install.py b/tests/func/test_install.py
index 140e0b5d5..1ff4ca23a 100644
--- a/tests/func/test_install.py
+++ b/tests/func/test_install.py
@@ -39,7 +39,9 @@ class TestInstall(object):
         expected_script = (
             "#!/bin/sh\n"
             "echo hello\n"
-            '[ -z "$(git ls-files .dvc)" ] || exec dvc checkout\n'
+            '[ "$3" == "0" ]'
+            ' || [ -z "$(git ls-files .dvc)" ]'
+            " || exec dvc checkout\n"
         )
 
         with open(self._hook("post-checkout"), "r") as fobj:
@@ -51,7 +53,9 @@ class TestInstall(object):
 
         expected_script = (
             "#!/bin/sh\n"
-            '[ -z "$(git ls-files .dvc)" ] || exec dvc checkout\n'
+            '[ "$3" == "0" ]'
+            ' || [ -z "$(git ls-files .dvc)" ]'
+            " || exec dvc checkout\n"
         )
 
         with open(self._hook("post-checkout"), "r") as fobj: