Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git pull invalid Windows file names

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.

like image 798
Nryan6 Avatar asked Nov 13 '15 21:11

Nryan6


People also ask

What is an illegal Windows file name?

Do not use the following reserved names for the name of a file: CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9. Also avoid these names followed immediately by an extension; for example, NUL. txt is not recommended.

Can't checkout filename too long?

Go to Computer Configuration → Administrative Templates → System → Filesystem in gpedit. msc , open Enable Win32 long paths and set it to Enabled.

How do I enable long path support in git?

windows-10-git.mdGo to Computer Configuration > Administrative Templates > System > Filesystem in gpedit. msc , open Enable Win32 long paths and set it to Enabled .


2 Answers

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

Use Sparse checkout to skip those files.

like image 92
max630 Avatar answered Oct 04 '22 12:10

max630


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.

like image 22
Svend Hansen Avatar answered Oct 04 '22 11:10

Svend Hansen