git log --pretty=format windows strange behavior

2019-06-14 05:01发布

问题:

I reduce the problem to its lowest terms. Under linux a git command like this:

git log --pretty=format:{"commit":"%H"}

gives me a valid json element, so something like:

{"commit":"20cafdecc9898113ac6215ae70cd7622dc2cae3b"}

under windows I obtain a not-valid json element, because in some way windows seems to remove the double-quotes elements and I obtain:

{commit:20cafdecc9898113ac6215ae70cd7622dc2cae3b}

Do you know why, or how can I fix it making it work under both os? Thank you!

回答1:

I assume that your shell on Linux is NOT a bash shell because my bash shell on Linux gives me the same output as on Windows - without double-quotes.

The doubles-quotes are special characters for most shells - so you have to protect them either by "escaping" or by "quoting".

git log --pretty=format:{\"commit\":\"%H\"}
git log --pretty='format:{"commit":"%H"}'

This works for bash on Linux and bash on Windows. I have not tested it with cmd on Windows.