137

When I try to do this, I get the following error:

gitignore error

Obviously, Windows Explorer doesn't allow me to create this type of file patterns. How can I overcome this problem?

2

5 Answers 5

332

In the File Explorer, the trick is to call your file .gitignore. and it will remove the ending .

A strange behavior but, hey!, it works 😅

Or create it from a text editor...

Sign up to request clarification or add additional context in comments.

5 Comments

That is a great trick. I highlighted filename just to make sure people don't miss that extra . at the end.
To clarify, you change your file name (e.g., .env) to append .gitignore. (.env.ignore.), correct?
@woodchuck No, I just add an ending . that will be removed by the explorer.
That's why we gotta use a MAC
No @MorrisS just turn off "File Name Extensions" in Explorer Show/Hide tab.
14

Windows Explorer doesn't allow you to create files that consist essentially of a file extension only. This is because Windows Explorer has the option to hide file extensions, leaving you with a file you cannot see (see Why doesn’t Explorer let you create a file whose name begins with a dot?). This is not a restriction of Windows itself, or the file system in use, though.

To create a file named .gitignore, you will have to use another tool to create it. A common solution is to create a text file (e.g. test.txt), open it in Notepad, and select Save As... to rename the file to .gitignore.

The Windows command interpreter also allows you to create files without imposing the additional restrictions of Windows Explorer. A more direct solution would then be to create the file from the command line. This can be done using the following command:

copy NUL .gitignore 


Note: When dealing with files that don't have a name, it's helpful to disable the option "Hide extensions for known file types" in Windows Explorer. Otherwise Windows Explorer might show files with no names, or hide them altogether.

4 Comments

The explanation why is a bit strange, as it makes no difference if you create it with other tool and view it in Windows Explorer. It's just that they were lazy to fix their stuff, and provided a workaround.
@d.popov: What's strange about the explanation? This is a usability issue, and File Explorer does the Right Thing. There's nothing to fix either, because nothing is broken (other than the *NIX convention to place additional semantics on file names starting with a dot character). In fact, there is code in File Explorer, that explicitly disallows creating files a user potentially cannot see. The 'fix' would be to remove code that's there for a reason. This is a strange comment indeed.
.gitignore or .hgignore are shown and can be selected/opened/renamed even with the option to hide known extensions. So not allowing to enter it is a bit of an annoying. And the thing with the last dot is very intuitive ;) Of course, that's just my opinion.
@d.popov: You have not made a convincing point, why you believe this to be a defect. You merely reiterated, that you would want to have it another way. The appropriate place to ask for design changes are Microsoft's UserVoice channels. If you insist, that this is a bug, Microsoft Connect is the right place.
3

In cmd, just type

echo. 2>.name_you_want 

or

. 2>.name_you_want 

to create a file.

If creating a directory, just type

mkdir .folder_name_you_want 

Comments

1

Use command line instead.
I was also having the same error. The problem was . at the start of file or folder name.
So I created it by command line.

Comments

0

You can do this with command prompt for folder creation:

mkdir .folder_name

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.