简体   繁体   English

通过批处理文件在GIT中创建已修改文件的存档

[英]Create archive of modified files in GIT via batch file

I'm using this git command to create an archive of the files modified within a specific commit: 我正在使用此git命令创建在特定提交中修改的文件的存档:

git archive -o update.zip HEAD $(git diff --name-only COMMITID^)

where COMMITID is the id of the commit I'm looking to archive. 其中COMMITID是我要存档的提交的ID。 This works fine from the command line but I would like to run it from a batch file. 这可以从命令行正常工作,但我想从批处理文件中运行它。 Here are the contents of the batch file I'm using: 以下是我正在使用的批处理文件的内容:

git archive -o update.zip HEAD $(git diff --name-only %1^^)

where %1 is the commit id being passed in via SourceTree. 其中%1是通过SourceTree传入的提交ID。 The problem I'm having is that this command when run from a batch file returns the following error: 我遇到的问题是从批处理文件运行时此命令返回以下错误:

error: unknown option `name-only' 错误:未知选项`name-only'

I'm guessing there may be some character escaping issues going but I'm unable to find what is specifically breaking. 我猜可能有一些角色逃避问题,但我无法找到具体的突破。

How would I write that git command so that it will work from a batch file? 我如何编写git命令以便它可以从批处理文件中运行?

UPDATE UPDATE

I tried removing the --name-only option and received the following error when trying the batch script via SourceTree: 我尝试删除--name-only选项,并在通过SourceTree尝试批处理脚本时收到以下错误:

fatal: path not found: $(git 致命:未找到路径:$(git

Hopefully that helps narrow down what may be going on. 希望这有助于缩小可能发生的事情。

FURTHER UPDATE 进一步更新

Turns out my syntax was wrong for grabbing only the modified files from a specific commit but using msandiford's answer I came up with this batch file script that works perfectly: 事实证明我的语法错误只是从特定提交中获取修改后的文件,但是使用msandiford的答案我想出了这个完美运行的批处理文件脚本:

setlocal enabledelayedexpansion
set output=
for /f "delims=" %%a in ('git diff-tree --no-commit-id --name-only -r %1^^') do ( set output=!output! "%%a" )
git archive -o update.zip HEAD %output%
endlocal

Assuming you need a windows batch file, and not a bash script, here is a minimal batch file that may do what you want: 假设您需要一个Windows批处理文件,而不是一个bash脚本,这里有一个最小的批处理文件可以做你想要的:

setlocal enabledelayedexpansion
set output=
for /f "delims=" %%a in ('git diff --name-only %1^^') do ( set output=!output! "%%a" )
git archive -o update.zip HEAD %output%
endlocal

It works by collecting all lines of the output of the git diff ... command into an environment variable, and then using this to perform the git archive ... operation. 它的工作原理是将git diff ...命令的所有输出行收集到一个环境变量中,然后使用它来执行git archive ...操作。

Documentation for the Windows for command is here , including the /f switch. 这里有Windows for命令的文档,包括/f开关。 Documentation for the setlocal command is here . setlocal命令的文档在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM