问题描述
在 Windows 7 下,我有一个批处理文件,用于检查某些 repos 的状态并将返回结果输出到文件(使用标准 powershell 发出的 git 命令).
Under Windows 7 I have a batch file for checking the status of some repos and outputting the returns to a file (with standard powershell issued git commands).
从 git shell 启动时效果很好,我的问题是如何在不先启动 git shell 的情况下做到这一点?
This works fine when launched from git shell, my question is how can I do this without launching git shell first?
所以我希望能够在标准提示符或可运行的批处理文件中输入一个命令/命令,该命令将在 git shell 中启动给定的批处理文件.
So I want to be able to enter in a standard prompt or in a runnable batch file a command / commands which will launch a given batch file in git shell.
推荐答案
如果你考虑一下 git-cmd.bat
可以,你需要做的就是设置正确的变量 %PATH%
在脚本中的 git 命令之前:
If you consider what git-cmd.bat
does, all you need to do is to set the right variable %PATH%
before your git commands in your script:
如果您不这样做,您会看到以下内容:
If you don't, here is what you would see:
C:UsersVonC>git --version
'git' is not recognized as an internal or external command,
operable program or batch file.
我已经解压了最新的便携版msysgit.
将具有以下内容的 test.bat
脚本(因此不涉及 powershell)放在任何地方:
Put anywhere a test.bat
script (so no powershell involved there) with the following content:
@setlocal
@set git_install_root="C:UsersVonCprgPortableGit-1.7.11-preview20120620"
@set PATH=%git_install_root%in;%git_install_root%mingwin;%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 配置.
Make sure HOME
is correctly set, because Git will look for your global git config there.
结果会给你:
C:UsersVonC>cd proggit
C:UsersVonCproggit>s.bat
C:UsersVonCproggit>git --version
git version 1.7.11.msysgit.0
C:UsersVonCproggit>echo C:UsersVonC
C:UsersVonC
C:UsersVonCproggit>git config --global --list
user.name=VonC
注意:同样的脚本可以在 powershell 会话中完美运行.
Note: that same script would work perfectly from a powershell session.
这篇关于在 git shell 中运行批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!