1

I'm trying to assign the last commit message inside a git repository to a variable in a windows batch script and then export the variable. From a lot of questions from StackOverflow, it seems that the command should be:

for /f "Tokens=2" %a in ('git log -1 --pretty=%B') do @set commit_message=%a

export %commit_message%

But it's giving the following error.

fatal: ambiguous argument '%B': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

In Linux it is quite straight forward. Only export commit_message=$(git log -1 --pretty=%B).

2
  • Double all your percent symbols. Commented Dec 9, 2017 at 17:39
  • This question is specifically tagged batch-file therefore try either, ... %%X In ('git log -1 --pretty^=%%B') Do @Set "commit_message=%%X" or ... %%X In ('"git log -1 --pretty=%%B"') Do @Set "commit_message=%%X". Commented Dec 9, 2017 at 18:32

1 Answer 1

2

It looks like you can't parse the git command directly. This worked on my site:

@echo off
git log -1 --pretty=%%B > %temp%\git.txt
for /f "Tokens=*" %%a in (%temp%\git.txt) do @set commit_message=%%a

echo %commit_message%
::export %commit_message%

Note the doubled '%'. I'm not sure about that export command. I've never seen this. Maybe setx is what you need?

Sign up to request clarification or add additional context in comments.

1 Comment

set is fine as I'm using the variable value in the same script. setx sets the variables to be used by future spawned cmd's not the current one. Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.