在Windows中编写Git命令脚本

o.c*_*nne 6 git scripting wsh jscript

我有一些git命令,我想在限制性的Windows环境中自动化.我发现自己一遍又一遍地运行这些命令,在Git Bash中执行.

$ git cd "R:/my/directory/repo"
$ git pull
$ git checkout "MyBranch"
$ git merge "MyOtherBranch"
Run Code Online (Sandbox Code Playgroud)

$ git checkout "MyBranch"
$ git add .
$ git commit -m "My commit comments"
Run Code Online (Sandbox Code Playgroud)

我精通JScript,我知道它在我的限制性Windows环境中工作,所以我想要做的就是通过打开命令提示符并输入来开始我的一天

cscript.exe "MyGitRefreshScript.wsf" "MyBranch" "MyOtherBranch"
Run Code Online (Sandbox Code Playgroud)

然后根据需要,在一天的剩余时间里,我可以做到这一点,也可以使用

cscript.exe "MyGitCommitScript.wsf" "MyBranch" "My commit comments"
Run Code Online (Sandbox Code Playgroud)

然后我可以愉快地坐在那里,按下向上箭头,然后在我工作的那一整天运行那些.

我无法在我的机器上安装第三方应用程序或工具,因此我希望在.wsf文件中使用JScript来完成此任务.我也希望能像我现在一样在Git Bash中运行命令时看到输出消息.

就像是:

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

        ...
Run Code Online (Sandbox Code Playgroud)

这是可能的,如果是的话,怎么样?是否有明确的替代方案,不涉及某些第三方工具安装?

谢谢

Von*_*onC 4

是否有一个不涉及安装第三方工具的明确替代方案?

是的,git 使用 bash shell,您可以在git alias或 git 脚本中使用它。

即使在 Windows 上,您也可以定义一个文件名 git-xxx,其中包含 shell 脚本(将由 git 中的 bash 解释)。
该脚本的最终版本(同样,即使在 Windows 上)将是#!/bin/bash.

在该脚本中,您可以使用 git 命令,并使用shell 参数$1, $2, ...)。

  • @o.carltonne 您编写 bash 脚本的语言是... bash:http://en.wikipedia.org/wiki/Bash_%28Unix_shell%29。它由与 Windows 版 Git (msysgit) 一起打包的 unix shell 进行解释,甚至可以在 Windows 上运行。 (2认同)