How do I write a batch file which opens the GitBash shell and runs a command in the shell?
"C:\Program Files (x86)\Git\bin\sh.exe" --login -i -c "git archive master | tar -x -C $0" "%~1"
You can also run a shell script to run multiple commands
#! /bin/bash
cd /c/GitRepo/PythonScripts
git status
read -p "Press enter to continue"
then call that from your cmd line:
"c:\Program Files (x86)\Git\bin\sh.exe" --login -i -c "/c/GitRepo/PythonScripts/statusandwait.sh"
In windows I created a git.bat file, and associated it to the .hook extension.
if not exist %1 exit
set bash=C:\Program Files (x86)\Git\bin\bash.exe
"%bash%" --login -i -c "exec "%1""
After that you can run the .hook files like every .bat or .cmd file except that they are running under git shell...
After a lot of trials , I got this one working. With current version of Git For Windows Portable. Open a Windows command window, and execute this script. If there is a change in your working directory, it will open a bash terminal in your working directory, and display the current git status. It keeps the bash window open, by calling exec bash
.
If you have multiple projects you may create copies of this script with different project folder, and call it from a main batch script.
Update - check new added files , that is untracked files.
=============
REM check git status of given folders.
setlocal EnableDelayedExpansion
set GIT_HOME=D:\eclipse\devtools\PortableGit-2.6.2
set GIT_EXEC=%GIT_HOME%\mingw64\bin\git.exe
set GIT_BASH=%GIT_HOME%\bin\bash.exe
set GITREPO=D:\source\myproject
pushd %GITREPO%
%GIT_EXEC% status
%GIT_EXEC% diff-index --quiet --cached HEAD
set VAR1=%errorlevel%
if not "%VAR1%"=="0" (
echo.
echo There are changes which are staged i.e. in index - but not committed.
echo.
)
%GIT_EXEC% diff-files --quiet
set VAR2=%errorlevel%
if not "%VAR2%"=="0" (
echo.
echo There are changes in working directory.
echo.
)
rem below for loop requires enabledDelayedExpansion
for /f "delims=" %%i in ('%GIT_EXEC% ls-files --others --exclude-standard') do (
if "!VAR3!"=="" (set VAR3=%%i) else (set VAR3=!VAR3!#%%i)
)
if not "%VAR1%"=="0" set REQUIRECOMMIT=true
if not "%VAR2%"=="0" set REQUIRECOMMIT=true
if not "%VAR3%"=="" set REQUIRECOMMIT=true
if "%REQUIRECOMMIT%"=="true" (
start "gitbash" %GIT_BASH% --login -i -c "git status; exec bash"
)
popd
endlocal
Use Bash is more friendly, for example
# file: backup.sh
cd /c/myProyectPath/
PWD=$(pwd);
function welcome() {
echo "current Dir : $PWD";
}
function backup() {
git pull
#if you have install wamp <http://www.wampserver.com>, we making slqBackup
MYSQLDUMP="/c/wamp/bin/mysql/mysql5.6.12/bin/mysqldump.exe";
$MYSQLDUMP --user=login --password=pass --no-create-info bd > data/backup.sql
git add data/backup.sql;
#generating tar file
git archive -o latest.tar HEAD
}
welcome;
backup;
echo "see you";
sleep 30;
You can run the script:
"C:\Program Files (x86)\Git\bin\sh.exe" --login -i -c "/c/myProyectPath/run.sh"