【问题标题】:Scripting Git Commands in Windows在 Windows 中编写 Git 命令脚本
【发布时间】:2023-03-21 20:38:01
【问题描述】:

我有一些 git 命令想在受限的 Windows 环境中自动执行。我发现自己一遍又一遍地运行这些相同的命令,在 Git Bash 中执行。

$ git cd "R:/my/directory/repo"
$ git pull
$ git checkout "MyBranch"
$ git merge "MyOtherBranch"

和

$ git checkout "MyBranch"
$ git add .
$ git commit -m "My commit comments"

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

cscript.exe "MyGitRefreshScript.wsf" "MyBranch" "MyOtherBranch"

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

cscript.exe "MyGitCommitScript.wsf" "MyBranch" "My commit comments"

然后我可以愉快地坐在那里,打开命令提示符,按下向上箭头,并在我工作的时候整天运行它们。

我无法在我的机器上安装第三方应用程序或工具,因此我希望只在 .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 ???

        ...

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

谢谢

【问题讨论】:

    标签: git scripting jscript wsh


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 所以 bash 脚本就像一个 .vbs 文件或 .js 文件,其中包含一个用 VBScript 或 JScript 编写的脚本?我用什么语言编写 bash 脚本?
    • @o.carltonne 用于编写 bash 脚本的语言是... bash: en.wikipedia.org/wiki/Bash_%28Unix_shell%29。它由与 Git for Windows (msysgit) 一起打包的 unix shell 解释,甚至可以在 Windows 上工作。
    【解决方案2】:

    看看这个 -

    http://nixkb.org/2014/08/git-made-easy-command-line-scripts-to-manage-git-changes-from-linux/

    这里有两部分脚本,您可以在为本地存储库配置 git 的任何 Linux 机器上运行。

    1) 初始化git或调用gitInit.sh

    2) 将更改合并到 git 或称为 gitMerge.sh

    【讨论】:

    • 链接已损坏!
    猜你喜欢
    • 2017-05-30
    • 2016-11-22
    • 2015-01-18
    • 1970-01-01
    • 2016-04-08
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多