可以在 git bash 命令行终端内右键依次选择 Options>About 就可以查看 git bash 的庐山真面目啦!
Mintty works on all Windows versions from Windows XP onwards. Similarly to other Cygwin/MSYS terminals based on pseudo terminal ("pty") devices, however, mintty is not a full replacement for the Windows Console window(by default running the Windows Command Processor / command prompt / cmd.exe). While native console programs with simple text output usually work fine, interactive programs often have problems, although sometimes there are workarounds. See the Wiki section about Input/Output interaction for hints, especially on the winpty wrapper.
以上摘抄自 https://mintty.github.io/ 官网.
既然 git bash 终端模拟器是 mintty,所以直接输入 mintty--help 命令也可以查看 mintty 的帮助信息.
Administrator@snowdreams1006 MINGW64 /f/workspace/test/git-bash $ mintty --help Usage: mintty [OPTION]... [ PROGRAM [ARG]... | - ] Start a new terminal session running the specified program or the user's shell. If a dash is given instead of a program, invoke the shell as a login shell. Options: -c, --config FILE Load specified config file (cf. -C or -o ThemeFile) -e, --exec ... Treat remaining arguments as the command to execute -h, --hold never|start|error|always Keep window open after command finishes -p, --position X,Y Open window at specified coordinates -p, --position center|left|right|top|bottom Open window at special position -p, --position @N Open window on monitor N -s, --size COLS,ROWS Set screen size in characters (also COLSxROWS) -s, --size maxwidth|maxheight Set max screen size in given dimension -t, --title TITLE Set window title (default: the invoked command) (cf. -T) -w, --window normal|min|max|full|hide Set initial window state -i, --icon FILE[,IX] Load window icon from file, optionally with index -l, --log FILE|- Log output to file or stdout --nobidi|--nortl Disable bidi (right-to-left support) -o, --option OPT=VAL Set/Override config file option with given value -B, --Border frame|void Use thin[表情] window border -R, --Report s|o Report window position (short/long) after exit --nopin Make this instance not pinnable to taskbar -D, --daemon Start new instance with Windows shortcut key -H, --help Display help and exit -V, --version Print version information and exit See manual page for further command line options and configuration.
既然 mintty 本身也说自己不能完全替代cmd,那到底什么情况无法胜任,抑或说应该如何与 cmd 互助合作呢?
答案其实就隐藏在安装 Git 时的配置界面中,当然也可以在 https://mintty.github.io/ 官网中找到些许蛛丝马迹.
如果想要在 git bash 命令行中调用 cmd 程序必须借助 winpty 包装器.
- 什么是
winpty包装器
winpty是一种提供与cmd通信的软件包,详情请参考 https://github.com/rprichard/winpty
听得云里雾里,不妨直接上手一试!
- 如何使用
winpty包装器
git bash 命令行内没有 tree 命令而 cmd 命令行却有 tree 命令.
只不过 cmd 命令中的 tree 命令实际上是 tree.com 并不是 tree.exe 文件.
tree命令是以树状结构显示文件目录的一种命令,查看目录结构层次非常直观.
# `cmd` 命令行内 `tree` 等价于 `tree.com` ,并不是 `tree.exe` ! F:workspaceest>tree.com 卷 工作 的文件夹 PATH 列表 卷序列号为 00000080 CC3C:50D0 G:. └─cmd ├─git └─git-bash
git bash 本身是没有 tree 命令,如果想要调用 tree 命令应该是调用 cmd 的 tree 命令,所以这里需要指明命令的全称: tree.com
Administrator@snowdreams1006 MINGW64 /f/workspace/test # `git bash` 命令行内直接调用 `tree.com` 命令输出中文乱码,即使设置 `git bash` 编码也无济于事! $ tree.com ▒▒ ▒▒▒▒ ▒▒▒ļ▒▒▒ PATH ▒б▒ ▒▒▒▒▒к▒Ϊ 00000005 CC3C:50D0 G:. ▒▒▒▒cmd ▒▒▒▒git ▒▒▒▒git-bash Administrator@snowdreams1006 MINGW64 /f/workspace/test # `winpty tree.com` 效果等价于 `cmd` 内直接执行 `tree.com` 命令,瞬间解决了中文乱码问题. $ winpty tree.com 卷 工作 的文件夹 PATH 列表 卷序列号为 00000073 CC3C:50D0 G:. └─cmd ├─git └─git-bash
如果直接调用 cmd 的 tree.com 命令会出现中文乱码,此时加上 winpty 包装器后再次调用就不会出现乱码现象了,也能顺利调用命令.
此外,还有一些交互性命令也是需要借助 winpty 包装器才能正常传递给 cmd 命令行处理,否则的话,无法进行正常的交互.
总之, git bash 命令行中想要调用 cmd 的命令,最好加上在命令的开头加上 winpty 或者直接在 cmd 中操作也行.
第一招之欢朋唤友来帮忙
正常情况下 git bash 的基本命令足够应付日常工作需求,如果遇到某些命令发现并无此命令,那么不妨尝试下 cmd命令行是否存在该命令?比如说上述示例中演示的 tree 命令.
git bash 和 cmd 两者相辅相成,强强联合才能更强大,毕竟 git 都是寄生在 windows 操作系统的租客,自然无法提供完整的 linux 体验,缺少一些命令很正常,看开点!
现在简单回顾总结一下本文的知识要点:
GitBash和GitGUI都是安装Git时默认集成的工具,两者均可以进行版本控制,相辅相成.GitBash默认集成的终端是Mintty终端模拟器,如果需要调用原生cmd程序,应使用winpty包装器.- 如果不借助
winpty包装器直接调用cmd的某些命令,可能会出现中文乱码以及无法与原生cmd程序进行交互!
在上述例子中, git bash 没有 tree 命令而 cmd 刚好存在 tree 命令,所以我们可以借助 cmd 来实现调用 tree 命令的意图.
但是,如果 git bash 命令没有某些命令而 cmd 也没有该命令,此时还如何借鸡生蛋?
举例:
git bash 命令行中没有 wget 下载命令.
Administrator@snowdreams1006 MINGW64 /f/workspace/test $ wget www.baidu.com bash: wget: command not found
cmd 命令行中也没有 wget 下载命令.
F:workspaceest>wget www.baidu.com 'wget' 不是内部或外部命令,也不是可运行的程序 或批处理文件。
这种情况下又该如何处理才能在 git bash 中支持 wget 命令呢?
小伙伴们不妨思考一下或者将你的答案贴在留言区,大家一起沟通交流下!
这种情况,我会在下一篇文章---三招教你轻松扩展 git bash 命令(中)文章中给出一种解决思路,如果你有更好的解决思路,欢迎联系我一起完善,谢谢你的阅读支持.

