7

在 Windows 7 下,我有一个批处理文件,用于检查某些 repos 的状态并将返回结果输出到文件(使用标准 powershell 发出的 git 命令)。

从 git shell 启动时效果很好,我的问题是如何在不先启动 git shell 的情况下做到这一点?

所以我希望能够在标准提示符或可运行的批处理文件中输入一个命令/命令,它将在 git shell 中启动给定的批处理文件。

4

2 回答 2

4

如果您考虑做什么git-cmd.bat,您需要做的就是%PATH%在脚本中的 git 命令之前设置正确的变量:

如果您不这样做,您会看到以下内容:

C:\Users\VonC>git --version
'git' is not recognized as an internal or external command,
operable program or batch file.

我已经解压了最新的便携版 msysgit

test.bat将包含以下内容的脚本(因此不涉及 powershell)放在任何地方:

@setlocal

@set git_install_root="C:\Users\VonC\prg\PortableGit-1.7.11-preview20120620"
@set PATH=%git_install_root%\bin;%git_install_root%\mingw\bin;%git_install_root%\cmd;%PATH%

@if not exist "%HOME%" @set HOME=%HOMEDRIVE%%HOMEPATH%
@if not exist "%HOME%" @set HOME=%USERPROFILE%

@set PLINK_PROTOCOL=ssh

REM here is the specific git commands of your script

git --version
echo %HOME%
git config --global --list

确保HOME设置正确,因为 Git 会在那里寻找你的全局 git 配置。

结果会给你:

C:\Users\VonC>cd prog\git

C:\Users\VonC\prog\git>s.bat

C:\Users\VonC\prog\git>git --version
git version 1.7.11.msysgit.0

C:\Users\VonC\prog\git>echo C:\Users\VonC
C:\Users\VonC

C:\Users\VonC\prog\git>git config --global --list
user.name=VonC

注意:相同的脚本可以在 powershell 会话中完美运行。

于 2012-06-30T11:15:19.020 回答
2

看起来您的 git 可执行文件无法用于命令行使用。

只需将c:\Users\[your_login]\AppData\Local\GitHub\PortableGit_[hash]\bin(或c:\Users\[your_login]\AppData\Local\GitHub\PortableGit_[hash]\cmd)添加到您的 Path 变量中。替换[your_login][hash]使用实际数据。

但我相信文件的位置会因版本而异,所以如果你是重度 git 用户,请考虑安装msysGit。它将自动将其可执行文件添加到系统路径(在安装过程中可以使用相应的选项)。

更重要的是,有一个名为mysysGit-UTF8的项目声称他们在 Windows 上具有完整的 UTF-8 支持。我没有注意到差异,通过。

于 2012-07-03T07:34:23.867 回答