Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

@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?

like image 243
Pok Avatar asked Mar 23 '11 05:03

Pok


People also ask

How do you sequentially execute commands in batch file?

Try using the conditional execution & or the && between each command either with a copy and paste into the cmd.exe window or in a batch file. Additionally, you can use the double pipe || symbols instead to only run the next command if the previous command failed.

How do you run a batch file and wait until it finishes?

Use /WAIT to Wait for a Command to Finish Execution When we start a program in a Batch file using the START command, we can wait until the program is finished by adding /wait to the START command. Even if there are multiple commands, /wait can be used for each process to finish and move to the next one.

How do you repeat a batch file?

Pressing "y" would use the goto command and go back to start and rerun the batch file. Pressing any other key would exit the batch file.

Can I control a program using a batch file once its been started?

Can I control a program using a batch file once its been started? No. A batch file can only execute or start another program. Once it is started, it cannot perform additional functions within that program.


1 Answers

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.

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).

You can solve this problem in a couple ways:

  1. invoke git in your batch files using the call command to run the git.cmd batch file and return back to yours:

    call git checkout %2 call git fetch origin rem etc... 
  2. invoke git in your batch file using the .exe extension explicitly to avoid the git.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 way git.exe expects (that seems to be what git.cmd does in msysgit):

    git.exe checkout %2 rem etc... 
like image 161
Michael Burr Avatar answered Oct 12 '22 23:10

Michael Burr