是否可以为 Git Bash 制作批处理文件?

发布于 2024-11-06 08:52:27 字数 913 浏览 1 评论 0原文

所以,我想知道是否有一种方法可以编写一个批处理文件,在 cmd 窗口中打开 git bash 并调用某些 git 函数,就像宏一样。可以这样做吗?我知道我可以在 cmd 窗口内调用“C:\Program Files (x86)\Git\bin\sh.exe --login -i”以在窗口内运行 git bash,并且我已经成功做到了一个批处理文件,但在 sh.exe 进程结束之前它不会执行任何操作。

这可能吗?我该怎么做?

注意:我发现了这个:使用批处理(.BAT)文件读取Git命令< /a>.那家伙是这么说的:

大家好,终于找到了我的问题的解决方案。通过使用下面的代码 ssippet,我们可以使用 BAT 文件运行 Git "c:\Program Files\Git\cmd\git.cmd" pull > c:\platform.txt"v上述代码使用 BAT 文件,而不是使用 git pull 或 git push 以及使用 GIT Bash 的任何 git 命令。感谢大家发表宝贵的评论和部分答案

但对我来说有没有 git.cmd 可执行文件...如果合并出错或发生其他情况,我只会使用写入 .txt 文件...但是 Git/bin 中有一个 git.exe 可执行文件,显然它会让我执行所有操作。 git 功能,如添加、推送、提交、所以我有两个问题:我是否必须让批处理文件将目录更改为本地存储库,然后从那里调用 git.exe?因为它不允许我执行 git cd另外,假设我在 cmd 窗口中输入了 git fetchupstream,如果我输入 gitmergeupstream/master,那么它会记住之前获取的内容。 ?

So, I was wondering if there was a way to write a batch file that opens git bash inside the cmd window and called on certain git functions, pretty much like a macro. Is it possible to do this? I know that I can call "C:\Program Files (x86)\Git\bin\sh.exe --login -i" inside a cmd window to run git bash inside the window, and I've managed to do that with a batch file, but then it doesn't do anything until sh.exe's process ends.

Is this possible? How can I do this?

Note: I have found this: Reading Git commands using Batch(.BAT) file. The guy said this:

Hai All, Finally I got the solution for my question.By using below code ssippet, we can run the Git using BAT file "c:\Program Files\Git\cmd\git.cmd" pull > c:\platform.txt"vthe above code using from BAT file instead of using git pull or git push and any git command using GIT Bash. Thanks to all for posting your valuable comments and partial answer

But for me there is no git.cmd executable... and I would only use writing to a .txt file if a merge went wrong or something... however there is a git.exe executable in Git/bin. Apparently it Will let me do all the git functions, like add, push, commit, merge, etc. So I have two questions about it: Will I have to have the batch file change directories to the local repo and then call git.exe from there? Because it won't let me do git cd. Also, say that I typed in git fetch upstream inside a cmd window. If I type git merge upstream/master, then will it remember what it had fetch previously?

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

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

发布评论

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

评论(2

_蜘蛛 2024-11-13 08:52:27

从 Windows 批处理文件通过 git bash 运行 git 几乎肯定不是您真正想要做的。我假设您正在谈论 msysgit,并且您似乎正在尝试解决安装中缺少 git.cmd 文件的问题。如果您丢失了此文件,则说明您的安装已损坏,我建议您重新安装。您会注意到安装程序中有一个选项可以将 git 放入 PATH 中以从 cmd.exe 运行。

修复安装后,git 在 cmd.exe 中的工作方式与在 bash 中的工作方式大致相同,但由于 Windows 和 cmd.exe 特殊字符而有一些注意事项。所以是的,您的获取和合并示例将正常工作。

如果要对 git 存储库进行操作,则需要位于存储库内部或根据需要将 GIT_DIR 环境变量设置为 .git 目录。

Running git via git bash from a Windows batch file is almost certainly not what you really want to do. I assume you are talking about msysgit, and you appear to be trying to work around the lack of git.cmd file in your installation. If you're missing this file, then your installation is broken and I'd suggest you reinstall. You'll notice there is an option in the installer to put git in the PATH to run from cmd.exe, too.

Once you've fixed your installation, git will work more or less the same from cmd.exe as from bash, subject to a few caveats due to Windows and cmd.exe special characters. So yes, your fetch and merge example will work just fine.

If you want to operate on a git repository, you need to be inside the repository or set the GIT_DIR environment variable to the .git directory as needed.

醉生梦死 2024-11-13 08:52:27

看看我在这里的答案:使用 MinGW 中的 msysGit,反之亦然< /a>

可能也适用于您。使用“调整路径环境”中的第三个或第二个选项将为您提供 git.cmd,您可以直接从 cmd 运行它。

Look at my answer here: Using msysGit from MinGW and vice versa

Might be applicable to you as well. Using the third or second option in the "Adjusting your path environment" will give you the git.cmd and you can run that directly from cmd.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文