windows 如何在批处理文件中执行多个 git 命令而不在第一个命令后终止?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5401229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I execute several git commands in a batch file without terminating after the first command?
提问by Pok
I tried to put a series of GIT commands that I always use continuously togeter as batch files so that I don't repeat myself too much. For example, I have this batch file called update_repo_branch.bat
to update a local repo and synch a branch with the remote branch:
我试着把我一直连续使用的一系列 GIT 命令作为批处理文件放在一起,这样我就不会重复太多。例如,我调用update_repo_branch.bat
了这个批处理文件来更新本地存储库并将分支与远程分支同步:
@echo off
if(%1) == () goto end
if(%2) == () goto end
cd %1
git checkout %2
git fetch origin
git merge oring/%2
:end
@echo off
if(%1) == () goto end
if(%2) == () goto end
cd %1
git checkout %2
git fetch origin
git merge oring/%2
:end
Good to be lazy, but what I found is that when a GIT command is finished, it seems to send an exit flag back to terminate whatever is running. Therefore, using a batch file to exectute them all in one go simply doesn't work. Any idea how to work around it?
懒惰是好事,但我发现当 GIT 命令完成时,它似乎会发送一个退出标志来终止正在运行的任何东西。因此,使用批处理文件一次性执行所有这些根本行不通。知道如何解决它吗?
回答by Michael Burr
I'm not sure if this is true for all Windows git packages, but at least some use a git.cmd
script as a wrapper around the actual git executables (for example git.exe
). So when you're batch file uses a git
command, Windows is actually running another batch file.
我不确定这是否适用于所有 Windows git 包,但至少有些使用git.cmd
脚本作为实际 git 可执行文件的包装器(例如git.exe
)。所以当你在批处理文件中使用一个git
命令时,Windows 实际上正在运行另一个批处理文件。
Unfortunately, when one batch file invokes another, by default it 'jumps' to the invoked batch file, never to return (this is for compatibility with ancient MS-DOS command processors or something).
不幸的是,当一个批处理文件调用另一个批处理文件时,默认情况下它“跳转”到调用的批处理文件,永远不会返回(这是为了与古老的 MS-DOS 命令处理器或其他东西兼容)。
You can solve this problem in a couple ways:
您可以通过以下几种方式解决此问题:
invoke
git
in your batch files using thecall
command to run thegit.cmd
batch file and return back to yours:call git checkout %2 call git fetch origin rem etc...
invoke
git
in your batch file using the.exe
extension explicitly to avoid thegit.cmd
batch file altogether. For this to work, you might need to make sure that you have your path and other environment variables set the waygit.exe
expects (that seems to be whatgit.cmd
does in msysgit):git.exe checkout %2 rem etc...
git
使用call
命令在批处理文件中调用以运行git.cmd
批处理文件并返回到您的:call git checkout %2 call git fetch origin rem etc...
显式
git
使用.exe
扩展名在批处理文件中调用以git.cmd
完全避免批处理文件。为此,您可能需要确保您的路径和其他环境变量设置为git.exe
预期的方式(这似乎是git.cmd
在 msysgit 中所做的):git.exe checkout %2 rem etc...
回答by Jim Mitchener
Assuming you are using msysGit as your Git client you might actually want to use Bash scripts for this. You could place a bash function in your ~/.bashrc
(~ is usually your C:\Users\- see here) as follows
假设您使用 msysGit 作为您的 Git 客户端,您可能实际上想要为此使用 Bash 脚本。你可以在你的~/.bashrc
(~通常是你的 C:\Users\-见这里)中放置一个 bash 函数,如下所示
update_repo_branch() {
if [ $# != "2" ]; then
echo "Usage: update_repo_branch REPO BRANCH" 1>&2
return 1
fi
cd
git checkout
git fetch origin
git merge origin/
}
You can then run update_repo_branch myrepo cool-branch
from the mysysGit shell.
然后您可以update_repo_branch myrepo cool-branch
从 mysysGit shell运行。
Of course, this won't be accessible from cmd.exe. You will only be able to use it within the msysGit cygwin shell.
当然,这不能从 cmd.exe 访问。您将只能在 msysGit cygwin shell 中使用它。
回答by Eugene Sajine
As i see from your example you're actually trying to sync your local branch 'branchname' with origin/branchname
正如我从您的示例中看到的,您实际上是在尝试将本地分支 'branchname' 与 origin/branchname 同步
For this you don't need any additional scripting, you just have to use git pull
instead of sequence git checkout branchname; git fetch origin; git merge origin/branchname
为此,您不需要任何额外的脚本,您只需要使用git pull
而不是序列git checkout branchname; git fetch origin; git merge origin/branchname
take a look at the docs about tracking branches in git and their benefits.
查看有关在 git 中跟踪分支及其好处的文档。
generally speaking if you have a repo layout like this:
一般来说,如果您有这样的回购布局:
git branch -a
...
master
dev1
dev2
remotes/origin/master
remotes/origin/dev1
remotes/origin/dev2
And your dev1 and dev2 branches are tracking branches for origin/dev1 and origin/dev2 correspondingly then you just need to execute in repository:
并且您的 dev1 和 dev2 分支相应地跟踪 origin/dev1 和 origin/dev2 的分支,那么您只需要在存储库中执行:
git pull
This command will effectively sync up all you local tracking branches with remote ones.
此命令将有效地将所有本地跟踪分支与远程分支同步。
for more see here:
有关更多信息,请参见此处: