三招教你轻松扩展 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 命令应该是调用 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 命令(中)文章中给出一种解决思路,如果你有更好的解决思路,欢迎联系我一起完善,谢谢你的阅读支持.

相关文章
|
Shell Linux 网络安全
宝塔服务器面板部署安装git通过第三方应用安装收费怎么办—bash: git: command not found解决方案-优雅草卓伊凡
宝塔服务器面板部署安装git通过第三方应用安装收费怎么办—bash: git: command not found解决方案-优雅草卓伊凡
1814 3
宝塔服务器面板部署安装git通过第三方应用安装收费怎么办—bash: git: command not found解决方案-优雅草卓伊凡
|
开发工具 git
Git版本控制工具合并分支merge命令操作流程
通过以上步聚焦于技术性和操作层面指南(guidance), 可以有效管理项目版本控制(version control), 并促进团队协作(collaboration).
3048 15
|
安全 开发工具 git
git的常用操作命令
git的常用操作命令
915 57
|
人工智能 前端开发 Java
用git rebase命令合并开发阶段中多条commit提交记录
通过 `git rebase`,可以合并多个提交记录,使开发历史更简洁清晰。操作分为 6 步:查看提交历史 (`git log --oneline`)、设置需合并的提交数 (`git rebase -i HEAD~N`)、修改动作标识为 `s`(squash)、保存退出编辑、调整提交信息、强制推送至远程仓库 (`git push -f`)。此方法适合清理本地无关提交,但若有团队协作或冲突风险,需谨慎使用以避免问题。
2991 60
|
存储 项目管理 开发工具
Git常用命令及操作技巧
以上是Git的常用命令及操作技巧,尽管看起来有些繁琐,但实际上只要花费一些时间进行实践,您将很快熟练掌握。随着使用熟练度的提高,您会发现Git对项目管理和协同工作的强大帮助。
421 20
|
存储 缓存 开发工具
Git stash命令的详细使用说明及案例分析。
通过上述案例,我们看到stash命令能够在不丢失进度的情况下,帮助开发者临时切换开发上下文,这在处理多个任务或紧急bug时特别有用。正确使用Git stash可以大大提高开发的灵活性和效率。
2946 0
|
Linux 开发工具 git
版本控制工具:Git的安装和基本命令使用指南。
结束这段探险,掌握了Git你就等于掌握了一个宝藏,随时可以瞥见你的编程历程,轻松面对日后的挑战。Git,无疑是编程者的强大武器,开始你的Git探险之旅吧!
658 28
|
网络安全 开发工具 git
mac git clone命令提示git@gitee.com: Permission denied (publickey).问题修复
mac git clone命令拉取gitee上项目代码时提示密钥问题
1696 19
|
Java 网络安全 开发工具
Git进阶笔记系列(01)Git核心架构原理 | 常用命令实战集合
通过本文,读者可以深入了解Git的核心概念和实际操作技巧,提升版本管理能力。