windows 退出 git 控制台:如何执行批处理文件然后返回到 git 控制台?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11865085/
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 09:47:01  来源:igfitidea点击:

Out of a git console: how do I execute a batch file and then return to git console?

windowsgitbatch-filecmd

提问by eckes

I have a small utility script called clear.batthat does some housekeeping work on my sources.

我有一个名为的小实用程序脚本clear.bat,它对我的​​资源进行一些整理工作。

It is a .batfile so that I could easily double-click it in Windows Explorer.

这是一个.bat文件,因此我可以轻松地在 Windows 资源管理器中双击它。

Sometimes, I find it more handy to execute it from my Git bash (msysgit, if this matters).

有时,我发现从我的 Git bash(msy​​sgit,如果这很重要)执行它更方便。

To do this, I type

为此,我输入

cmd
clear.bat
exit

cmdturns my Git bash into a normal cmdwindow where I could easily execute my batch. When I type exit, the cmdenvironment is terminated and I'm back in my Git bash.

cmd将我的 Git bash 变成一个普通cmd窗口,在那里我可以轻松地执行我的批处理。当我输入 时exitcmd环境终止,我又回到了我的 Git bash 中。

Could this be achieved in an easier way?

这可以以更简单的方式实现吗?

I tried cmd /C clean.batsince the docssay

我试过,cmd /C clean.bat因为文档

Syntax
      CMD [charset] [options]

      CMD [charset] [options] [/c Command] 

      CMD [charset] [options] [/k Command] 

Options   
   /C     Run Command and then terminate

   /K     Run Command and then return to the CMD prompt.
          This is useful for testing, to examine variables


Edit:
Just noticed that the post is broken.

编辑:
刚刚注意到该帖子已损坏。

What I want is to execute clean.batfrom within the Git bash without having to type the three commands above (cmd, clear.bat, exit). I just want to execute the .batfile from within my Git bash. Obvious way would be to create a separate .shfile that does the same work but this will lead to double code.

我想要的是clean.bat从 Git bash 中执行而不必键入上面的三个命令 ( cmd, clear.bat, exit)。我只想.bat从我的 Git bash 中执行该文件。明显的方法是创建一个单独的.sh文件来完成相同的工作,但这会导致双重代码。



Edit 2:When I execute cmd /C clean.bat, the Git bash turns into a plain CMD environment and only displays the prompt. The file clean.batdoes not get executed. It's the same as if I just type cmd.

编辑 2:当我执行时cmd /C clean.bat,Git bash 变成一个普通的 CMD 环境并且只显示提示。该文件clean.bat不会被执行。就好像我只是输入一样cmd

Also, adding a /debugswitch does literally nothing. Seems like only cmdgets evaluated and all further parameters are getting ignored.

此外,添加/debug开关实际上没有任何作用。似乎只cmd得到评估,所有进一步的参数都被忽略了。

回答by eckes

After playing around a bit more, I found the solution myself:

玩了一会儿之后,我自己找到了解决方案:

cmd "/C clean.bat"

does the trick. But I got no clue, why...

诀窍。但我不知道为什么...

回答by half-potato

./clear.batwill do the trick.

./clear.bat会做的伎俩。

回答by Yitzhak Steinmetz

The Git for Windows(msysGit has been superseded by Git for Windows1) FAQsays you have 3 options:

混帐的Windows(msysGit已经通过GIT中的Windows取代1常见问题解答说,你有3种选择:

  • Run programs that have problems using the winptyutility. This allows you to keep using the nicer mintty terminal, but can become unwieldy if you need the workaround for many programs.

  • Modify the shortcut for Git Bash to run bashdirectly without minttyso it uses the default console host and configure it for "Quick Edit", reasonable size and scroll-back and suitable unicode font. You'll still have to live with the other quirks of console host.

  • Install and use ConEmu.

  • 运行使用该winpty实用程序时出现问题的程序。这允许您继续使用更好的 mintty 终端,但如果您需要许多程序的解决方法,则可能会变得笨拙。

  • 修改 Git Bash 的快捷方式,使其bash直接运行而不mintty使用默认的控制台主机,并将其配置为“快速编辑”,合理的大小和回滚以及合适的 unicode 字体。您仍然必须忍受控制台主机的其他怪癖。

  • 安装和使用ConEmu

回答by lav

I like start clean, it opens a new window with cmd. This method has some benefits:

我喜欢start clean,它用 cmd 打开一个新窗口。这种方法有一些好处:

  • cmd.exe gets a native console
  • the new console has a native windows character encoding (e.g. cp1251 vs utf8)
  • cmd.exe 获取本地控制台
  • 新控制台具有原生 Windows 字符编码(例如 cp1251 与 utf8)