在 Windows 中编写 Git 命令脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27539157/
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
Scripting Git Commands in Windows
提问by o.carltonne
I have a few git commands I would like to automate in a restrictive windows environment. I find myself running these same commands over and over, executing in Git Bash.
我有一些 git 命令,我想在受限的 Windows 环境中自动执行。我发现自己一遍又一遍地运行这些相同的命令,在 Git Bash 中执行。
$ git cd "R:/my/directory/repo"
$ git pull
$ git checkout "MyBranch"
$ git merge "MyOtherBranch"
and
和
$ git checkout "MyBranch"
$ git add .
$ git commit -m "My commit comments"
I'm proficient with JScript, and I know it works in my restrictive windows environment, so what I want to do is to start my day by opening the command prompt and typing
我精通 JScript,并且我知道它在我受限的 Windows 环境中工作,所以我想做的是通过打开命令提示符并键入来开始我的一天
cscript.exe "MyGitRefreshScript.wsf" "MyBranch" "MyOtherBranch"
And then on demand, throughout the rest of the day, I can do that, and also use
然后根据需要,在一天剩下的时间里,我可以这样做,也可以使用
cscript.exe "MyGitCommitScript.wsf" "MyBranch" "My commit comments"
Then I can sit there happily with the command prompt open pressing the up arrow and run those all day long as I do my work.
然后我可以愉快地坐在那里,打开命令提示符按下向上箭头,并在我工作时整天运行它们。
I can not install third party applications or tools on my machine, so I am looking to just use JScript inside a .wsf file to accomplish this. I would also like to be able to see the output messages like I do when running the commands in Git Bash as I do now.
我无法在我的机器上安装第三方应用程序或工具,因此我希望仅在 .wsf 文件中使用 JScript 来完成此操作。我还希望能够像现在一样在 Git Bash 中运行命令时看到输出消息。
Something like:
就像是:
<job id="main">
<script language="JScript">
var sh = new ActiveXObject("WScript.Shell");
sh.Run(Git.exe "SomeCommand" ?);
//or
var git = new ActiveXObject("Git");
git.SomeCommand ???
...
Is this possible, and if so, how? Is there a clear alternative that doesn't involve some third party tool installation?
这可能吗,如果可以,怎么做?是否有不涉及某些第三方工具安装的明确替代方案?
Thank you
谢谢
采纳答案by VonC
Is there a clear alternative that doesn't involve some third party tool installation?
是否有不涉及某些第三方工具安装的明确替代方案?
Yes, git uses a bash shell which you can use in git alias, or in git script.
是的,git 使用 bash shell,您可以在git alias或 git script 中使用它。
Even on windows, you can define a file name git-xxx, with a shell script in it (which will be interpreted by the bash from git).
The shebang of that script (again, even on Windows) will be #!/bin/bash
.
即使在 Windows 上,您也可以定义一个文件名 git-xxx,其中包含一个 shell 脚本(将由来自 git 的 bash 解释)。
该脚本的shebang(同样,即使在Windows 上)将是#!/bin/bash
.
In that script, you can use the git commands, and use the shell parameters($1
, $2
, ...).
在该脚本中,您可以使用 git 命令,并使用shell 参数( $1
, $2
, ...)。
回答by Arti Pathak
Check this out -
看一下这个 -
http://nixkb.org/2014/08/git-made-easy-command-line-scripts-to-manage-git-changes-from-linux/
http://nixkb.org/2014/08/git-made-easy-command-line-scripts-to-manage-git-changes-from-linux/
Here are two part scripts that you can run on any Linux box where git is configured for your local repos.
这里有两部分脚本,您可以在任何为本地存储库配置 git 的 Linux 机器上运行它们。
1) Initialize git or called gitInit.sh
1)初始化git或调用gitInit.sh
2) Merge changes to git or called gitMerge.sh
2) 将更改合并到 git 或调用 gitMerge.sh