gitgit-rm

git rm --cached and fatal: pathspec


I just tried to checkout my master branch and ran into:

error: Untracked working tree file 'app.xcodeproj/project.xcworkspace/xcuserdata/u.xcuserdatad/UserInterfaceState.xcuserstate' would be overwritten by merge. 

So, I tried to delete this file from git (I'd already added an expression in .gitignore to catch it) using:

git rm --cached app.xcodeproj/project.xcworkspace/xcuserdata/u.xcuserdatad/UserInterfaceState.xcuserstate

and got:

fatal: pathspec 'app.xcodeproj/project.xcworkspace/xcuserdata/u.xcuserdatad/UserInterfaceState.xcuserstate' did not match any files 

So, at a bit of a loss. From my understanding the working file isn't the issue here. However, for completeness, a working file does exist. E.g.

ls -l app.xcodeproj/project.xcworkspace/xcuserdata/u.xcuserdatad/UserInterfaceState.xcuserstate
-rw-r--r--  1 u  u  56061 24 Sep 12:42 app.xcodeproj/project.xcworkspace/xcuserdata/u.xcuserdatad/UserInterfaceState.xcuserstate

Solution

  • So, the solution is this:

    The file is untracked in this current branch B

    But it exists in the branch we are trying to check out, branch A, so we get a warning that the file in our current working tree will be overwritten (even though we aren't tracking it)

    So:

    1. delete the file in your existing directory (I just moved it somewhere out of the working tree initially to be safe) of branch B

    2. check out the branch you want - i.e. branch A

    3. Remove it from branch A using something like this:

      git rm --cached app.xcodeproj/project.xcworkspace/xcuserdata/u.xcuserdatad/UserInterfaceState.xcuserstate

    Note: Fwiw, Branch A was my master branch. Branch B was my dev branch.