How to set variable from git command output using windows cmd/batch

I have trouble “forwarding” an output to a variable.
I’m using Atlassian SourceTree with my GIT versioning. There I want to create a custom action, with which I can easily create a zip file of all modified files of one commit. The created file should have a formatted date of the commit time in the filename as update-<YYYYmmdd>T<hhMM>.zip.

My working batch-file so far (line numbers for later reference):

1:  setlocal enabledelayedexpansion
2:  set output=
3:  for /f "delims=" %%a in ('git diff-tree --no-commit-id --name-only -r %1^^') do ( set output=!output! "%%a" )
4:  set hours=
5:  git show -s --format=%%ci %1 > tmptmptmp & set /p hours= < tmptmptmp & del tmptmptmp
6:  set hours=%hours:~0,4%%hours:~5,2%%hours:~8,2%T%hours:~11,2%%hours:~14,2%
7:  git archive -o update-%hours%.zip HEAD %output%
8:  endlocal

I’m having trouble to rewrite line 5 (hours-line) to avoid creating a temporary file.
If I do the same as in line 3 (output-line) like:

for /f "delims=" %%a in ('git show -s --format=%%ci %1^^') do ( set hours=!hours! "%%a" )

I get the following error:

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

I assume this has something to do with the needed %ci parameter (for formatting the output).

Can somebody help me with this?

  • Unable to create '/git/index.lock': File exists - but it doesn't
  • Can't use git add with --patch option
  • How can I retrieve environment variables(Jenkins) in java
  • Git freezes on help commands
  • Starter Questions for Using Git Windows Version with Git Source Control Provider
  • Git status ignore line endings / identical files / windows & linux environment / dropbox / mled
  • Changing case of Folder via Git bash on Windows
  • Is there a way to use Windows Authentication (Active Directory) for a Git server?
  • One Solution collect form web for “How to set variable from git command output using windows cmd/batch”

    I do not know why do you add trailing ^ caret (a circumflex accent) over the same command used in line 5 git show -s --format=%%ci %1.

    However, the = equals sign in --format=%%ci should be escaped as follows:

    for /f "delims=" %%a in ('git show -s --format^=%%ci %1^^') do set hours=!hours! "%%a"
    

    A proof sample:

    ==>type 29803777.bat
    
    IF NOT [%1]==[] dir /B %1=
    for /f "delims=" %%a in ('IF NOT [%1]==[] dir /B %1=') do @echo "%%a"
    for /f "delims=" %%a in ('IF NOT [%1]^=^=[] dir /B %1^=') do @echo "%%a"
    
    ==>29803777.bat x
    
    ==>IF NOT [x] == [] dir /B x=
    x.txt
    
    ==>for /F "delims=" %a in ('IF NOT [x] [] dir /B x ') do @echo "%a"
    [] was unexpected at this time.
    
    ==>for /F "delims=" %a in ('IF NOT [x]==[] dir /B x=') do @echo "%a"
    "x.txt"
    
    ==>
    
    Git Baby is a git and github fan, let's start git clone.