如何编写一个批处理文件来打开 GitBash shell 并在 shell 中运行命令?

发布于 2024-10-20 20:00:36 字数 370 浏览 6 评论 0原文

我在 Windows 7 上尝试使用批处理文件打开 GitBash shell 并进行 git 调用。 这是我的批处理文件的内容:

REM Open GitBash 
C:\Windows\SysWOW64\cmd.exe /c ""C:\Program Files (x86)\Git\bin\sh.exe" --login -i"
REM retrieve archive
git archive master | tar -x -C %~1
REM quit GitBash
exit

我注意到 GitBash 在下一个命令“git archive...”之前注销。有谁知道我是否可以将命令传递到 GitBash 以及如何传递?

麦克风

I'm on Windows 7 trying to use a batch file to open the GitBash shell and make a git call.
This is the contents of my batch file:

REM Open GitBash 
C:\Windows\SysWOW64\cmd.exe /c ""C:\Program Files (x86)\Git\bin\sh.exe" --login -i"
REM retrieve archive
git archive master | tar -x -C %~1
REM quit GitBash
exit

I noticed that the GitBash is logging out before the next command "git archive...". Does anybody know if I can pass the command into GitBash and how?

Mike

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

往日 2024-10-27 20:00:37
"C:\Program Files (x86)\Git\bin\sh.exe" --login -i -c "git archive master | tar -x -C $0" "%~1"
"C:\Program Files (x86)\Git\bin\sh.exe" --login -i -c "git archive master | tar -x -C $0" "%~1"
半葬歌 2024-10-27 20:00:37

您还可以运行 shell 脚本来运行多个命令,

#! /bin/bash
cd /c/GitRepo/PythonScripts
git status
read -p "Press enter to continue"

然后从 cmd 行调用该命令:

"c:\Program Files (x86)\Git\bin\sh.exe" --login -i -c "/c/GitRepo/PythonScripts/statusandwait.sh"

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"
一绘本一梦想 2024-10-27 20:00:37

在 Windows 中,我创建了一个 git.bat 文件,并将其与 .hook 扩展名相关联。

if not exist %1 exit
set bash=C:\Program Files (x86)\Git\bin\bash.exe
"%bash%" --login -i -c "exec "%1""

之后,您可以像每个 .bat 或 .cmd 文件一样运行 .hook 文件,只不过它们在 git shell 下运行...

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...

美胚控场 2024-10-27 20:00:37

经过多次试验,我终于成功了。使用当前版本的 Git For Windows Portable。打开 Windows 命令窗口,并执行此脚本。如果您的工作目录发生更改,它将在您的工作目录中打开一个 bash 终端,并显示当前的 git 状态。它通过调用 exec bash 使 bash 窗口保持打开状态。

如果您有多个项目,您可以使用不同的项目文件夹创建此脚本的副本,并从主批处理脚本中调用它。

更新 - 检查新添加的文件,即未跟踪的文件。

=============

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

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
月下伊人醉 2024-10-27 20:00:37

使用 Bash 更加友好,例如

# 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;

你可以运行脚本:

"C:\Program Files (x86)\Git\bin\sh.exe" --login -i -c "/c/myProyectPath/run.sh"

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"
瑶笙 2024-10-27 20:00:37

就我而言,某个 HTTP 请求可以在 Windows 上的 Git Bash 中的 curl 中工作。但是,当使用 HttpClient 和 HttpGet (org.apache.http.client.methods.HttpGet) 在 Java 上运行时,我会收到连接重置错误。

如果我尝试使用 exec 直接运行命令,由于某种原因它无法工作。

作为解决方法,此代码会将命令写入批处理文件中,然后运行该批处理文件并将输出放置在 command.txt 中。

这是需要在 command.bat 文件中的命令(我已经更改了端点和密码):

"C:\Users\scottizu\AppData\Local\Programs\Git\bin\sh.exe" --login -i -c "curl 'https://my.server.com/validate/user/scottizu' -H 'Password: MY_PASSWORD' > command.txt"

这是代码(注意该命令有特殊字符转义):

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

public class CURL_Runner {
    public static void main (String[] args) throws Exception {
        String command = "\"C:\\Users\\scottizu\\AppData\\Local\\Programs\\Git\\bin\\sh.exe\" --login -i -c \"curl 'https://my.server.com/validate/user/scottizu' -H 'Password: MY_PASSWORD' > command.txt\"";
        createAndExecuteBatchFile(command);
    }

    public static void createAndExecuteBatchFile(String command) throws Exception {

        // Step 1: Write command in command.bat
        File fileToUpload = new File("C:\\command.bat");
        try {
            if(fileToUpload.getParentFile() != null && !fileToUpload.exists()) {
                fileToUpload.getParentFile().mkdirs();
            }
            FileWriter fw = new FileWriter(fileToUpload);
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(command);
            bw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Step 2: Execute command.bat
        String[] cmdArray = new String[1];
        cmdArray[0] = "C:\\command.bat";

        Process process = Runtime.getRuntime().exec(cmdArray, null, new File("C:\\"));
        int processComplete = process.waitFor();
    }
}

In my case, a certain HTTP Request would work in curl within Git Bash on Windows. However, I would get a Connection Reset error when running on Java using HttpClient and HttpGet (org.apache.http.client.methods.HttpGet).

If I tried to use exec to directly run the command, for some reason it would not work.

As a workaround, this code will write the command in a batch file, then run the batch file and place the output in command.txt.

Here is the command which needs to be in the command.bat file (I have changed the endpoint and password):

"C:\Users\scottizu\AppData\Local\Programs\Git\bin\sh.exe" --login -i -c "curl 'https://my.server.com/validate/user/scottizu' -H 'Password: MY_PASSWORD' > command.txt"

Here is the code (notice the command has special characters escaped):

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

public class CURL_Runner {
    public static void main (String[] args) throws Exception {
        String command = "\"C:\\Users\\scottizu\\AppData\\Local\\Programs\\Git\\bin\\sh.exe\" --login -i -c \"curl 'https://my.server.com/validate/user/scottizu' -H 'Password: MY_PASSWORD' > command.txt\"";
        createAndExecuteBatchFile(command);
    }

    public static void createAndExecuteBatchFile(String command) throws Exception {

        // Step 1: Write command in command.bat
        File fileToUpload = new File("C:\\command.bat");
        try {
            if(fileToUpload.getParentFile() != null && !fileToUpload.exists()) {
                fileToUpload.getParentFile().mkdirs();
            }
            FileWriter fw = new FileWriter(fileToUpload);
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(command);
            bw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Step 2: Execute command.bat
        String[] cmdArray = new String[1];
        cmdArray[0] = "C:\\command.bat";

        Process process = Runtime.getRuntime().exec(cmdArray, null, new File("C:\\"));
        int processComplete = process.waitFor();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文