Reputation: 61104
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!
Upvotes: 1
Views: 276
Reputation: 1323115
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.
Upvotes: 3