gitpost-build-event

Why can't I see the commit hash of my application in a post-build event?


When I go to the build directory of my application, and I ask for the commit hash, everything is working fine:

Prompt>git log -1 --format=format:"%H"
c03245bcdf6ddd7b54af4c70d9012e16a6c8c64f

When I try this in a post-build event (Visual Studio, C# application), this does not work:

Post-build event:

git log -1 --format=format:"%H" >Release_Note2.txt

Contents of the file "Release_Note2.txt:

H

(indeed, just one single letter)

Does anybody know what I can do in order to get the commit hash of my application in a post-build event of my application?

I'm working on a Windows machine.


Solution

  • To get the current commit:

    git rev-parse HEAD

    That will print the current commit ID your HEAD is pointing to. You can substitute HEAD with any tag or branch you'd like to know as well. (And also @ in most shells is a shorthand for HEAD.)

    As for why your attempt didn't work, my guess is that maybe your string needs to be escaped inside of your C# application, e.g.:

    git log -1 --format=format:\"%H\" >Release_Note2.txt

    Though, if you switch to the preferred rev-parse command perhaps you don't need to worry about why. 😉