Search code examples
windowsgitkeybase

Cannot git checkout master: invalid path '?'


After an "unsuccessful" git pull on my local master, an error prevents to switch back to master:

C: repo_folder> git checkout master
error: invalid path '?'

The ? must be because it is a keybase repo.

From another branch where I checked out some files of the last commit:

C: repo_folder> git diff origin/master --compact-summary
 "\004" (gone)                               | 1902  ---------------------------
 some irrelevant stuff                       | (num) -

The removed file "\004" (that was never present in my local) seems to come from some Mac OS (someone might have opened a csv and a temporary file was created when that user did the commit and pushed?).

  • observe that the file that is marked as (gone) is to be removed by git
  • the problem is that the filename has characters that are not compatible with the Windows file system and that the file never existed in my local Windows repo.

If I clone from a Linux platform, I can checkout to master with no problems. However, in Windows, there's no way back to the master branch.

Any ideas on how to solve this issue? (already tried some posts with no success) I can't really understand how it comes git doesn't even allow me to checkout to master. Should I file a bug report?

Alternatively, perhaps I could create a new master branch and get rid of the current one.

EDIT

A clone from Linux helped to identify that the file ? was actually there. This could be checked directly from Windows as well by using the command: git ls-tree origin/master (which was showing the original problematic name "\004")

The accepted answer includes the case where you want to save the content of the file, while in my case I only wanted to get rid of it. So in my case, I have just deleted the file from Linux, committed and pushed the change, and did a git fetch origin master:master to fetch my local master with being checked out in another branch (as I was not able to checkout to master). This finally did the trick and I could checkout to master.

Hope this clarifies to someone with a similar problem.


Solution

  • ? (or maybe it's EOT) cannot be used as a filename on Windows. The file will have to be deleted or renamed. You can do this most easily by cloning on a system which does allow ? and making the fix.

    If you only have Windows, Fixing Invalid Git Paths on Windows offers a method of renaming the file without checking it out. In brief...

    1. git checkout origin/master -f to get the checkout without the problematic file.
    2. Make a branch.
    3. Add and commit the "deleted" problematic file.
    4. Use git ls-tree HEAD^ to get the ID of the problem file.
    5. Use git cat-file -p <ID> to get the content of the problem file.
    6. Put the content into a new file.
    7. Add and commit.