7

Under Windows 7 I have a batch file for checking the status of some repos and outputting the returns to a file (with standard powershell issued git commands).

This works fine when launched from git shell, my question is how can I do this without launching git shell first?

So I want to be able to enter in a standard prompt or in a runnable batch file a command / commands which will launch a given batch file in git shell.

Ingmar Boddington
  • 3,440
  • 19
  • 38
  • A question: What version of git for windows have you got? I was struggling with installing msys Git for win (7) but some commands took ages to complete... Now got git only via cygwin. – bcelary Jun 27 '12 at 15:23
  • I have 1.7.11.rc1.6953.gf229a20 - Was installed with the new GitHub windows application – Ingmar Boddington Jun 27 '12 at 15:32
  • Can you do simple git commands from Powershell? In other words, is the git shell required to do anything with git? If you can access git from Powershell, then you could rewrite your batch file as a powershell script. Alternatively, you should be able to run your batch file from Powershell, unless it sets env vars (if so, search "Invoke-CmdScript" on Google). – David Jun 27 '12 at 19:06
  • I cannot do git commands from Powershell and I dont want to have to open powershell before running a batch file. The objective is to be able to issue a set of git commands in git shell with a single action – Ingmar Boddington Jun 28 '12 at 08:55
  • So you want to run a set of git commands without using any kind of shell. If thats what you want then sorry its impossible. Be more specific with your questions please. I am unable to understand anything from your question. – Learath2 Jul 04 '12 at 12:41
  • Everyone else has @Learath2... the point is not to run git commands without a shell, but to run git commands from a batch file or from the standard command prompt in a single action (ie not from git shell or powershell) – Ingmar Boddington Jul 04 '12 at 13:17

2 Answers2

4

If you consider what git-cmd.bat does, all you need to do is to set the right variable %PATH% before your git commands in your script:

If you don't, here is what you would see:

C:\Users\VonC>git --version
'git' is not recognized as an internal or external command,
operable program or batch file.

I have uncompressed the latest portable version of msysgit.

Put anywhere a test.bat script (so no powershell involved there) with the following content:

@setlocal

@set git_install_root="C:\Users\VonC\prg\PortableGit-1.7.11-preview20120620"
@set PATH=%git_install_root%\bin;%git_install_root%\mingw\bin;%git_install_root%\cmd;%PATH%

@if not exist "%HOME%" @set HOME=%HOMEDRIVE%%HOMEPATH%
@if not exist "%HOME%" @set HOME=%USERPROFILE%

@set PLINK_PROTOCOL=ssh

REM here is the specific git commands of your script

git --version
echo %HOME%
git config --global --list

Make sure HOME is correctly set, because Git will look for your global git config there.

The result will give you:

C:\Users\VonC>cd prog\git

C:\Users\VonC\prog\git>s.bat

C:\Users\VonC\prog\git>git --version
git version 1.7.11.msysgit.0

C:\Users\VonC\prog\git>echo C:\Users\VonC
C:\Users\VonC

C:\Users\VonC\prog\git>git config --global --list
user.name=VonC

Note: that same script would work perfectly from a powershell session.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
2

It looks like your git executable is just not accessible for command line use.

Just add c:\Users\[your_login]\AppData\Local\GitHub\PortableGit_[hash]\bin (or c:\Users\[your_login]\AppData\Local\GitHub\PortableGit_[hash]\cmd) to your Path variable. Replacing [your_login] and [hash] with actual data.

But I believe the location of files will change from version to version, so if you're heavy git user, consider installing msysGit. It will add its executable to the system path automatically (corresponding option available during setup).

Even more, there is the project called mysysGit-UTF8 claiming that they have full UTF-8 support on Windows. I didn't notice the difference, through.

shytikov
  • 9,155
  • 8
  • 56
  • 103