三招教你轻松扩展 git bash 命令(上)(二)

简介: GitBash 是 Windows 系统安装 Git 时默认集成的命令行工具,提供运行 Git 命令的集成环境.

可以在 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/ 官网中找到些许蛛丝马迹.


60.png

如果想要在 git bash 命令行中调用 cmd 程序必须借助 winpty 包装器.

  • 什么是 winpty 包装器

winpty 是一种提供与 cmd 通信的软件包,详情请参考 https://github.com/rprichard/winpty


61.jpg

听得云里雾里,不妨直接上手一试!

  • 如何使用 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 命令应该是调用 cmdtree 命令,所以这里需要指明命令的全称: 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

如果直接调用 cmdtree.com 命令会出现中文乱码,此时加上 winpty 包装器后再次调用就不会出现乱码现象了,也能顺利调用命令.

此外,还有一些交互性命令也是需要借助 winpty 包装器才能正常传递给 cmd 命令行处理,否则的话,无法进行正常的交互.

总之, git bash 命令行中想要调用 cmd 的命令,最好加上在命令的开头加上 winpty 或者直接在 cmd 中操作也行.

第一招之欢朋唤友来帮忙

正常情况下 git bash 的基本命令足够应付日常工作需求,如果遇到某些命令发现并无此命令,那么不妨尝试下 cmd命令行是否存在该命令?比如说上述示例中演示的 tree 命令.

git bashcmd 两者相辅相成,强强联合才能更强大,毕竟 git 都是寄生windows 操作系统的租客,自然无法提供完整的 linux 体验,缺少一些命令很正常,看开点!

现在简单回顾总结一下本文的知识要点:

  • GitBashGitGUI 都是安装 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 命令(中)文章中给出一种解决思路,如果你有更好的解决思路,欢迎联系我一起完善,谢谢你的阅读支持.

相关文章
|
19天前
|
机器学习/深度学习 人工智能 开发工具
《Git 简易速速上手小册》第10章:未来趋势与扩展阅读(2024 最新版)
《Git 简易速速上手小册》第10章:未来趋势与扩展阅读(2024 最新版)
47 3
|
19天前
|
缓存 数据可视化 网络安全
Git命令大全
Git命令大全
66 1
|
19天前
|
开发工具 git
Git教程:深入了解删除分支的命令
【4月更文挑战第3天】
112 0
Git教程:深入了解删除分支的命令
|
19天前
|
存储 Shell Linux
【Shell 命令集合 文件管理】Linux git命令使用教程
【Shell 命令集合 文件管理】Linux git命令使用教程
42 0
|
19天前
|
开发工具 git
git常用命令整理
git常用命令整理
18 0
|
19天前
|
开发工具 git 开发者
Git常用命令大全:让你轻松驾驭版本控制
Git命令速查:`git init`新建仓库,`git clone`克隆,`git add`入暂存区,`git commit -m`提交,`git status`查看状态,`git log`查看历史,`git branch`创建分支,`git checkout`切换,`git merge`合并,`git pull`拉取更新,`git push`推送,`git remote -v`查看远程,`git checkout --`撤销本地修改,`git reset HEAD`取消暂存,`git reset --hard`回退版本。掌握这些,提升代码管理效率!
25 0
|
19天前
|
Shell 网络安全 开发工具
GIT常用命令
GIT常用命令
|
17天前
|
网络安全 开发工具 git
版本管理 git 常用命令
版本管理 git 常用命令
22 1
|
19天前
|
存储 Linux 开发工具
Git 分布式版本控制系统基本概念和操作命令
Git 分布式版本控制系统基本概念和操作命令
126 0
|
19天前
|
算法 Java BI
云效产品使用报错问题之平台上导出的统计数据和 git 中使用命令导出的数据统计都对不上,如何解决
本合集将整理呈现用户在使用过程中遇到的报错及其对应的解决办法,包括但不限于账户权限设置错误、项目配置不正确、代码提交冲突、构建任务执行失败、测试环境异常、需求流转阻塞等问题。阿里云云效是一站式企业级研发协同和DevOps平台,为企业提供从需求规划、开发、测试、发布到运维、运营的全流程端到端服务和工具支撑,致力于提升企业的研发效能和创新能力。