How to execute multiple git commands in a batch file without completion after the first command?

I tried putting a series of GIT commands that I always use in continuous mode as batch files so that I don't repeat too much. For example, I have this batch file called update_repo_branch.bat to update the local repo and synchronize the branch with the remote branch:

@echo off
if (% 1) == () goto end
if (% 2) == () goto end
cd% 1
Git checkout% 2
git fetch source
GIT merge oring /% 2
: The end

Good to be lazy, but I found that when the GIT command is finished, it seems to send the exit flag back to complete everything that works . Thus, using a batch file to extract them all in one go simply does not work. Any idea how to get around this?

+44
git windows batch-file
Mar 23 '11 at 5:32
source share
3 answers

I'm not sure if this is true for all windows git packages, but at least some use the git.cmd script as a wrapper around the actual git executables (e.g. git.exe ). Therefore, when you use the git command in a batch file, Windows actually runs another batch file.

Unfortunately, when one batch file calls another, by default it “jumps” to the called batch file, never returns (this is for compatibility with ancient MS-DOS commands or something like that).

You can solve this problem in several ways:

  • call git in your batch files using the call command to launch the git.cmd batch file and return back to yours:

     call git checkout %2 call git fetch origin rem etc... 
  • Call git in your batch file using the .exe extension explicitly to avoid the git.cmd batch file. To do this, you may need to make sure that your path and other environment variables set the way git.exe expects (this is similar to what git.cmd does in msysgit):

     git.exe checkout %2 rem etc... 
+54
Mar 23 '11 at 6:10
source share

Assuming you are using msysGit as your Git client, you can use Bash scripts to do this. You can put the Bash function in your ~/.bashrc (~ usually your C: \ Users \ - see here ) as follows

 update_repo_branch() { if [ $# != "2" ]; then echo "Usage: update_repo_branch REPO BRANCH" 1>&2 return 1 fi cd $1 git checkout $2 git fetch origin git merge origin/$2 } 

Then you can run update_repo_branch myrepo cool-branch from the mysysGit shell.

Of course, this will not be available from cmd.exe. You can only use it in the msysGit cygwin command shell.

+3
Mar 23 2018-11-11T00:
source share

As I can see from your example, you are actually trying to synchronize the local branchname branch with origin / branchname

You do not need any additional scripts for this, you just need to use git pull instead of the git checkout branchname; git fetch origin; git merge origin/branchname git checkout branchname; git fetch origin; git merge origin/branchname

take a look at the git branch tracking docs and their benefits.

generally speaking, if you have a repo layout:

 git branch -a ... master dev1 dev2 remotes/origin/master remotes/origin/dev1 remotes/origin/dev2 

And your branches dev1 and dev2 track the branches for origin / dev1 and origin / dev2 respectively, then you just need to execute in the repository:

 git pull 

This command will effectively synchronize all local tracking branches with remote ones.

See here for more details:

Git pull docs

Git remote branches and tracking branches (Progit book)

+3
Mar 23 '11 at 16:32
source share



All Articles