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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:12:21  来源:igfitidea点击:

How do I execute several git commands in a batch file without terminating after the first command?

windowsgitbatch-file

提问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.batto 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.cmdscript as a wrapper around the actual git executables (for example git.exe). So when you're batch file uses a gitcommand, 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:

您可以通过以下几种方式解决此问题:

  1. invoke gitin your batch files using the callcommand to run the git.cmdbatch file and return back to yours:

    call git checkout %2
    call git fetch origin
    rem etc...
    
  2. invoke gitin your batch file using the .exeextension explicitly to avoid the git.cmdbatch file altogether. For this to work, you might need to make sure that you have your path and other environment variables set the way git.exeexpects (that seems to be what git.cmddoes in msysgit):

    git.exe checkout %2
    rem etc...
    
  1. git使用call命令在批处理文件中调用以运行git.cmd批处理文件并返回到您的:

    call git checkout %2
    call git fetch origin
    rem etc...
    
  2. 显式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-branchfrom 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 pullinstead 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:

有关更多信息,请参见此处:

Git pull docs

Git 拉取文档

Git remote branches and tracking branches (Progit book)

Git远程分支和跟踪分支(Progit书)