在git shell中运行批处理文件

Ing*_*Ing 6 git powershell batch-file windows-7

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

从git shell启动时这很好用,我的问题是如何在不首先启动git shell的情况下执行此操作?

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

Von*_*onC 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.
Run Code Online (Sandbox Code Playgroud)

我已经解压缩了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
Run Code Online (Sandbox Code Playgroud)

确保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
Run Code Online (Sandbox Code Playgroud)

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