Git pull invalid Windows file names

断了今生、忘了曾经 提交于 2020-06-10 12:53:46

问题


I'm working a shared project using git for version control. I'm on windows while my partner is on Unix.

My partner has named some files with <file1>.txt. When I try to pull these files they are not accepted as the < and > are invalid characters for Windows. This is fine, I don't need to touch the files. However, they are added to my commit as deleted. So, if I push then I'll delete these files which I don't want to do.

  1. I can't use git reset --hard as it finds an invalid path for each of these "deleted" files.

  2. Is there a way to exclude these files from my commits? I've tried adding <file1> to my .git/info/exclude but that didn't work.


回答1:


Ignoring doesn't help if the files are tracked already.

Use Sparse checkout to skip those files.




回答2:


You would need to get your partner to change the names to be something that is also valid on Windows. After they have renamed them, what I'd do is this:

  1. Backup any changes that you only have locally (both uncommitted AND committed but not pushed).
  2. Run git reset --hard <commit> where <commit> is any commit from before the files were added.
  3. Run git pull to get all the way to the latest revision (where the files are renamed).
  4. Restore your backed up changes from 1.

This should then get the newer revision where the files aren't named in this, to Windows, illegal way, and they won't be deleted (or ever created) from under git by the OS :)

P.S. I know this is an old question, but I've been getting this issue recently, so hopefully the solution I've arrived at can help others as well.

EDIT:

To avoid this happening again, your partner can add a pre-commit hook that will stop them from committing files with names that would not be allowed on Windows. There's a sample/example often in pre-commit.sample. I've changed the bit a little in the past and end up with something like:

# Cross platform projects tend to avoid non-ASCII filenames; prevent
# them from being added to the repository. We exploit the fact that the
# printable range starts at the space character and ends with tilde.
if [ "$allownonascii" != "true" ] &&
    # Note that the use of brackets around a tr range is ok here, (it's
    # even required, for portability to Solaris 10's /usr/bin/tr), since
    # the square bracket bytes happen to fall in the designated range.
    echo $(git diff --cached --name-only --diff-filter=A -z $against | LC_ALL=C)
    test $(git diff --cached --name-only --diff-filter=A -z $against |
      LC_ALL=C tr -d '[ !#-)+-.0-9;=@-[]-{}~]\0' | wc -c) != 0
then
    cat <<\EOF
Error: Attempt to add a non-ASCII file name.

This can cause problems if you want to work with people on other platforms.

To be portable it is advisable to rename the file.

If you know what you are doing you can disable this check using:

  git config hooks.allownonascii true
EOF
    exit 1
fi

The '[ !#-)+-.0-9;=@-[]-{}~]\0' bit is the important part that I've changed a little. It defines all the allowed ranges of characters, and the example one only disallows "non-ascii" characters (which is what the comment at the top says), but there are also ascii characters that are not allowed in file names on Windows (such as ? and :).

All the allowed characters are removed, and if there's anything left (wc -c != 0) it errors. It can be a bit difficult to read, as you can't see any of the disallowed characters. It helps if you have a list of the char ranges to look at when reading or editing it.



来源:https://stackoverflow.com/questions/33702140/git-pull-invalid-windows-file-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!