1

Synopsis

Pass arguments to git bash using a .bat file

OR

Use git some other way by running a batch file.

Disclaimer

There are a lot of answers to similar questions out there, but having tried and failed most or all of them, I hope you don't smack me with a DUPLICATE stamp at first sight.

My system

Windows 7, 64 bit

git version 1.9.4.msysgit.2

The challenge

I want to rationalize my workflow from navigating to a number of git repos and running git status, git add --all etc, to simply run a batch.

Other posts certainly suggests it's possible. I think most of them use Unix, but this one at least got me up and running on Windows so I could test it out. However, I'm having the same problems as OP in that post when it comes to passing the commands to the git bash, and to a complete beginner like me it seems a bit complicated to use the suggestions from @Erik further down in the same post if you want to include more commands in the work flow.

Following the suggestions from @inf3rno in the same post, it seems I'm able to change folders but not able to use git commands like this:

set bash=C:\Program Files (x86)\Git\bin\bash.exe
"%bash%" --login -i -c "exec "%1""
cd c:\repos\research
git status
pause

Does anyone have suggestions as to how I can change the code above to get it working?

And by the way, what's the deal with #!bin\bash from other posts like this one? I assumed I had to use #!Program Files (x86)\Git\bin\bash, but I had no luck with that either...

Anyway, thank you for any suggestions!

Community
  • 1
  • 1
vestland
  • 55,229
  • 37
  • 187
  • 305

1 Answers1

3

First, uninstall git 1.9.4 (msygit, which is now obsolete): git-for-windows will offer a much recent bash (2013 vs. 2005).

Simply unzip PortableGit-2.6.1-64-bit.7z.exe anywhere you want, add C:\path\PortableGit-2.6.1-64-bit\bin to your %PATH% and you are good to go.

Second, inf3rno's answer is about executing any bash script ("%bash%" --login -i -c "exec "%1"": the %1 is the path/name of the bash script to be executed)

The right shebang to use in your bash scripts would be #!/bin/bash (see "What is the preferred Bash shebang?")

With the latest git 2.6, that would be:

c:\prgs\git\PortableGit-2.6.1-64-bit\bin\bash.exe --login -i -c "exec ./myscript"

Since that folder is supposed to be in your %PATH%:

bash --login -i -c "exec ./myscript"

With myscript being a file using Unix-style eol (LF), not Windows-style (CRLF)

Note also that any bash script (even on Windows) called git-myscript can be directly called with:

git myscript

I described in 2012 another approach in "Running a batch file in git shell" for executing git command.
But for a pure bash script, you will want to go with bash --login -i -c "exec ./myscript".

For writing bash scripts with unix eol style, you can choose various editor from Notepad++, SublimeText 3 or Atom.io.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for your answer! Here's what I've done so far: 1. Removed git 1.9.4 successfully 2. Installed PortableGit-2.6.1-64-bit.7z.exe to the folder c:\prgs\git 3. Added c:\prgs\git\bin to %PATH% 4. Tried to run a bath containing `#!/bin/bash cd /c/repos/research git status pause` But I'm getting an error saying #!/bin/bash is not recognized. Am I missing something embarrasingly obvious, or am I approaching the whole thing the wrong way? Edit: I'm trying to make the comment look better by editing the mini-Markdown formatting. – vestland Oct 08 '15 at 08:48
  • @123apd how exactly did you run your bash script? I have tested the `bash --login -i -c "exec ./myscript"` approach myself, and it works just fine. – VonC Oct 08 '15 at 08:54
  • I tried runnint it from a .bat file (now something tells me that's a complete face plant) – vestland Oct 08 '15 at 09:00
  • @123apd yes, a .bat would invoke the Windows CMD shell. The line I mention above is made to execute a bash script (again, make sure to write that bash script in an editor using unix-style eol) – VonC Oct 08 '15 at 09:01
  • Ok, I was under the impression that having git for windows installed, adding the correct path to %PATH%, and then running a .bat file including the #!/bin/bash part somehow would enable me to pass git commands through the Windows CMD shell. I guess I'm going to have to go through the details again and get a better understanding of the whole thing. – vestland Oct 08 '15 at 09:13
  • @123apd Indeed: any bash script must be executed in a bash shell (even if it starts with `#!/bin/bash`). Hence my suggestion to call that bash in order to execute said script. `bash --login -i -c "exec ./myscript"`. – VonC Oct 08 '15 at 09:16
  • Do you have any suggestions as to which editor to use? And if it's relevant, what is the last part of your myscript filename? – vestland Oct 08 '15 at 09:50
  • My first step of a better understanding of this whole thing was to focus on your suggestions on other posts. After following your answer [here](http://stackoverflow.com/questions/11229619/running-a-batch-file-in-git-shell) i got things working exactly as I was hoping by editing your `test.bat`. I have to admit I don't know exactly why, but it works like a charm, and results from for example `git status` are displayed directly in the CMD shell. Thank you for all your help and hints! Both here and elsewhere. – vestland Oct 08 '15 at 10:23
  • @123apd the bash command will run any filnename (no extension required). A Notepad++, SublimeText or Atom.io are all capable of handling bash script with unix eol. – VonC Oct 08 '15 at 10:27
  • 1
    @123apd I have edited the answer to add those links, by the way. – VonC Oct 08 '15 at 12:13