In a command line of a pre-build event in a VS 2026 C++ project, I have the line
git log -1 --date=format:%G --output=year.tmp --format="%cd"
which is supposed to write the numerical year of the latest revision to a file, but the code %cd somehow gets evaluated to a capital I with an acute accent, so the command does not work.
Steps to reproduce:
- in Visual Studio, create a new project for, say, a C++ Console App.
- Use the popup Add to Source Control at the bottom of the window, and initialize a local git repository.
- Open the project property window.
- Under Build Events, go to Pre-Build Event.
- Open the pop-up menu at the right of the command line, and select Edit…
- In the command line text field, paste in
git log -1 --date=format:%G --format="%cd" - Look at the Evaluated value below, and observed that the %cd part has changed.
I found a question related to this: How to include the percent character in the visual studio debugger command line arguments?
That explains that something like %cd may be treated as a hex code for a character, and suggests representing the percent sign by its hex code, %25. So, suppose in step 6 above you use git log -1 --date=format:%G --format=%25cd. In that case the “evaluated value” in the editing window looks right. But if you actually build the project, the output window shows that you get a log in which the date is the literal string cd.
I thought some indirection might help, and tried:
set FMT=%25cd
git log -1 --date=format:%G --format=%FMT%
but then the log shows a date as the literal string FMT.