I'm using the following git command in git bash on Windows:
git log --format="%C(cyan)%cd%Creset %s" --date=short -5
It displays commit date (%cd
) followed by commit message (%s
). Commit date is wrapped with color markers: %C(cyan)
to start colored output and %Creset
to stop colored output.
While it works fine in git bash, it doesn't do well with cmd
: %cd%
is expanded by Windows shell into current working directory (equivalent of $PWD
in bash).
Hence when that command is run via cmd
, I see current working directory displayed instead of commit date in the first column!
git bash:
2015-10-08 commit msg
2015-10-08 commit msg
2015-10-07 commit msg
2015-10-06 commit msg
2015-10-06 commit msg
cmd:
D:\git\someFolderCreset commit msg
D:\git\someFolderCreset commit msg
D:\git\someFolderCreset commit msg
D:\git\someFolderCreset commit msg
D:\git\someFolderCreset commit msg
Actually, I never use cmd
directly myself, I found this behavior while writing a nodejs (0.12) script in which I had
require('child_process').execSync('git log --format=...', {stdio: 'inherit'})
which gets executed by node using the cmd
when on Windows).
A simple workaround could be to introduce a space to prevent %cd%
being found, i.e. change
git log --format="%C(cyan)%cd%Creset %s" --date=short -5
to
git log --format="%C(cyan)%cd %Creset%s" --date=short -5
However this introduced a redundant space (I deleted another space before %s
but it's still a hack, and requires manual intervention).
Is there a way to prevent expansion by the Windows shell?
I've found information about using %%
or ^%
to escape the %
but they are not the solution here:
# produces superfluous ^ characters
git log --format="%C(cyan)^%cd^%Creset %s" --date=short -5
^2015-10-08^ commit msg
^2015-10-08^ commit msg
^2015-10-07^ commit msg
^2015-10-06^ commit msg
^2015-10-06^ commit msg
# seems the expansion is done at command parse time
git log --format="%C(cyan)%%cd%%Creset %s" --date=short -5
%D:\git\someFolder commit msg
%D:\git\someFolder commit msg
%D:\git\someFolder commit msg
%D:\git\someFolder commit msg
%D:\git\someFolder commit msg
The ideal solution should be either compatible with bash and cmd, without producing redundant characters, or an escape function in javascript to escape the generic UNIX-y command for Windows to prevent the expansions (if such an escape function can be created).
%%
works in BAT files but not in the command line – LamoreauxECHO git log --format=^"^%C^(cyan^)^%cd^%Creset ^%s^" --date=short -5
Escaped allCMD
poisonous characters forECHO
command. – Rene^
chars. are retained. – Kennard