0

I have these lines written in my .gitignore file:

logs/
main.log
*.log
bin/
*.bin

When i execute the command: git show HEAD~1:.gitignore, i get this weird result: git command output

When i'm supposed to get this: extected results

I run this command from poweshell, cmd, git bash, windows terminal and Cygwin64 and from the integrated terminal in VS Code and i get the same result from everywhere. .gitignore's encoding is UTF-16 LE. I even changed the encoding to ANSI and UTF-8 but still got the same result. Stuck with this one for days, any help will be much appreciated!

darab
  • 1
  • 1

1 Answers1

0

The <FF><FE> is a byte order mark or BOM that marks the file as UTF-16-LE. The remainder of the file consists of UTF-16-LE characters. That is, this isn't a plain text file, it's a UTF-16 file. Git does not know how to read that (and the result is that its contents don't actually matter).

I even changed the encoding to ANSI and UTF-8 but still got the same result.

The actual bytes of the file matter: a UTF-8 file would, ideally, not begin with a BOM (UTF-8 files don't have byte order in the first place so these are just junk). The entire text of the file would then just be the UTF-8 data. This requires rewriting the file, i.e., you will need to make a new commit: the existing commit has a useless file in it.

To actually rewrite the file, you will need a tool that does it. You do not say how you changed the encoding. See UTF-16 to UTF-8 conversion (for scripting in Windows) for various options. It's possible you've already done this correctly, and made a new commit, because HEAD~1 is not the current commit, but rather some existing, previous commit. No existing commit can ever be changed, though the special name HEAD always refers to the current commit, so as you make new commits, the commit that HEAD means—and therefore the one that HEAD~1 means—changes over time.

torek
  • 448,244
  • 59
  • 642
  • 775
  • i changed the encoding by opening and saving it as UTF-8 and ANSI, i didn't use a tool, but i'll try what you 're suggesting – darab Oct 05 '20 at 08:11