Git pull invalid Windows file names

后端 未结 2 1974
别跟我提以往
别跟我提以往 2021-02-15 13:03

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 .txt

2条回答
  •  南方客
    南方客 (楼主)
    2021-02-15 13:12

    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 where 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.

提交回复
热议问题