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!

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

10 Comments

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.
@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.
I tried runnint it from a .bat file (now something tells me that's a complete face plant)
@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)
@123apd I have edited the answer to add those links, by the way.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.