在 Windows 上通过批处理文件进行 git commit 和 push

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20397800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 11:08:52  来源:igfitidea点击:

git commit and push via batch file on Windows

windowsgitbatch-file

提问by Dev01

I do same task often of committing and pushing changes to remote branch. Being lazy sometimes, I needed to put set of git commands to automatically perform these steps:

我经常执行相同的任务,将更改提交和推送到远程分支。有时懒惰,我需要放置一组 git 命令来自动执行这些步骤:

cd D:\wamp\www\projectName
git checkout dev
git add .
git commit -am "made changes"
git push
pause

I also tried:

我也试过:

cd D:\wamp\www\projectName
call git checkout dev
call git add .
call git commit -am "made changes"
call git push
pause

and

cd D:\wamp\www\projectName
git.exe checkout dev
git.exe add .
git.exe commit -am "made changes"
git.exe push
pause

Everything works excpet for the final pushcommand. Here is output:

除了最终push命令外,一切正常。这是输出:

D:\wamp\www\givingcircle>git checkout dev
Already on 'dev'
Your branch is ahead of 'origin/dev' by 1 commit.

D:\wamp\www\givingcircle>git add .

D:\wamp\www\givingcircle>git commit -am "made changes"
# On branch dev
# Your branch is ahead of 'origin/dev' by 1 commit.
#
nothing to commit, working directory clean

D:\wamp\www\givingcircle>git push
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

D:\wamp\www\givingcircle>pause
Press any key to continue . . .

As you can see, for push, I am getting:

如您所见,对于push,我得到:

D:\wamp\www\givingcircle>git push
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

When I run above commands via git shell itself, everything works fine. I have also added git to Windows Path env variables.

当我通过 git shell 本身运行上面的命令时,一切正常。我还将 git 添加到 Windows Path env 变量中。

Does anyone have an idea of why it works on git shell and not on batch command ? (even though other commands work but not push)

有谁知道为什么它适用于 git shell 而不是批处理命令?(即使其他命令有效但无效push

回答by janos

For me, by default, Windows executes .shfiles correctly using Git Bash. So I would write your script as a regular bash shell script:

对我来说,默认情况下,Windows.sh使用 Git Bash 正确执行文件。因此,我会将您的脚本编写为常规的 bash shell 脚本:

#!/bin/sh
cd /d/wamp/www/projectName
git checkout dev
git add .
git commit -am "made changes"
git push
echo Press Enter...
read

回答by Scott

I had a similar need, to be able to move code from BBCloud to our development test servers, for stage 1 testing.

我有类似的需求,能够将代码从 BBCloud 移动到我们的开发测试服务器,以进行第一阶段的测试。

To do this, I created a Windows scheduled task:

为此,我创建了一个 Windows 计划任务:

Under "Actions", I added "C:\Program Files\Git\bin\bash.exe"in Program/script field (the quotes were required).

在“操作”下,我添加"C:\Program Files\Git\bin\bash.exe"了程序/脚本字段(需要引号)。

In the "Add arguments" field, I entered c:\path\to\bash script\pull.sh.

在“添加参数”字段中,我输入了c:\path\to\bash script\pull.sh.

I then completed the Task Scheduler wizard (run frequency, time, etc.).

然后我完成了任务计划程序向导(运行频率、时间等)。

I then created a bash script, using Nano in Git Bash for Windows containing:

然后我创建了一个 bash 脚本,在 Git Bash for Windows 中使用 Nano 包含:

#!/bin/bash
cd /c/path/to/bash script
git pull

I would prefer a push to the repository automatically pushing down to the test server, but Pipes, Webhooks, and DeployHQ don't seem to be a solution for our environment.

我更喜欢推送到存储库,自动推送到测试服务器,但 Pipes、Webhooks 和 DeployHQ 似乎不是我们环境的解决方案。

回答by Nikita P

Try this one !!

试试这个!!

cd c://TESTS/path
set HOME=%USERPROFILE%
GIT COMMAND GOES HERE
pause