Powershell’s cool. The syntax may be opinionated, but having a proper scripting environment in Windows is excellent. Git is cool. If you don’t use git, you’re @£$££!###. Typically, I create .gitignore files from the git bash. The way I do it is:
touch .gitignore
The main reason for doing this is because Windows Explorer (or File Explorer as it’s known these days) won’t let me create the .gitignore file – “YOU MUST TYPE A FILENAME”. And most of the time, I resort to copying one in from an existing project anyway. However, my default setup is a nice looking combination of Console2, Posh-Git, and Powershell. I like to pick up bits and bobs of Powershell here and there, and having a nice command line isn’t exactly the worst thing in the world. Could I avoid going into git bash for one simple command? Well, the obvious way to create a file in powershell is to pipe some text to it:
“#gitignore” >> .gitignore
Voila – a file called .gitignore with the contet “#gitignore”. Awesome, right? Not quite. If you try to use the .gitignore file created above, git will simply ignore it. Joy. What gives?
A bit of head scratching a experiments, and the reason is unveiled. Powershell by default creates the file as Unicode. Git wants ASCII. So how do we get powershell to give us a new file in ASCII?
“#gitignore” | out-file –encoding ASCII .gitignore
The nice little >> operator doesn’t support specifying the encoding. In fact, all it does is pipes to the Out-File powershell command. The Out-File command has a number of parameters, and if we are to specify non-default values, we need to pipe to it ourselves. Oh, and in case you’re wondering, having a comment on the first line can sometimes help with Windows. I’ve heard of people having trouble without it.
This might seem trivial, but it caused me a bit of pain, and I hope it can save somebody else’s time.