$ 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
-e, --exec Treat remaining arguments as the command to execute
-h, --hold never|start|error|always Keep window open after command finishes
-i, --icon FILE[,IX] Load window icon from file, optionally with index
-l, --log FILE|- Log output to file or stdout
-o, --option OPT=VAL Override config file option with given value
-p, --position X,Y Open window at specified coordinates
-s, --size COLS,ROWS Set screen size in characters
-t, --title TITLE Set window title (default: the invoked command)
-u, --utmp Create a utmp entry
-w, --window normal|min|max|full|hide Set initial window state
--class CLASS Set window class name (default: mintty)
-H, --help Display help and exit
-V, --version Print version information and exit
adsryen@home MINGW64 /g/sublime/test
$ alias
alias ll='ls -l'
alias ls='ls -F --color=auto --show-control-chars'
alias node='winpty node.exe'
按照 linux 的操作习惯,命令行设置的一般都是临时性的,想要永久生效,都要写入到文件中,别名这种当然要一劳永逸设置成永久文件.
adsryen@home MINGW64 /g/sublime/test
$ cat /etc/bashrc
cat: /etc/bashrc: No such file or directory
adsryen@home MINGW64 /g/sublime/test
$ cat ~/.bashrc
cat: /c/Users/adsryen/.bashrc: No such file or directory
竟然配置文件都不存在?
当然不存在了啊!快醒醒,你是在 Windows 系统上并不是 Linux 系统,上哪给你弄这些配置文件去?
脑海中迅速闪现哲学基本问题: 我是谁,我在那,我在干什么?
我是 Windows 系统用户,正在 git bash 命令行中试图设置别名,没有找到类似于 linux 配置文件.
Administrator@adsryen MINGW64 /f/workspace/test
$ mingw-get
bash: mingw-get: command not found
Administrator@adsryen MINGW64 /f/workspace/test
$ pacman
bash: pacman: command not found
频繁出现 Git For Windows 名词,根据软件的命令规则,Git for Windows 可能是独立的软件,更何况 git bash 集成的终端也不是自身研发的终端而是第三方的 mintty 终端.
所以,我们有理由相信 Git for Windows 是另外的团队在维护,而不是 Git 团队.
根据官方说明,安装后正在下载相关依赖,下载速度比较慢的话,请自行解决.
下载完成后,原来的 cmd 窗口会自动关闭并且打开新的 git bash 窗口.
Administrator@adsryen MINGW64 / (master)
$ sdk help
The 'sdk' shell function helps you to get up and running
with the Git for Windows SDK. The available subcommands are:
create-desktop-icon: install a desktop icon that starts the Git for
Windows SDK Bash.
cd <project>: initialize/update a worktree and cd into it. Known projects:
git git-extra msys2-runtime installer build-extra
MINGW-packages MSYS2-packages mingw-w64-busybox mingw-w64-curl
mingw-w64-cv2pdb mingw-w64-git mingw-w64-git-credential-manager
mingw-w64-git-lfs mingw-w64-git-sizer mingw-w64-wintoast bash
curl gawk git-flow gnupg heimdal mintty nodejs openssh openssl
perl perl-HTML-Parser perl-Locale-Gettext perl-Net-SSLeay
perl-TermReadKey perl-XML-Parser perl-YAML-Syck subversion tig
init <project>: initialize and/or update a worktree. Known projects
are the same as for the 'cd' command.
build <project>: builds one of the following:
git-and-installer git git-extra msys2-runtime installer
mingw-w64-busybox mingw-w64-curl mingw-w64-cv2pdb mingw-w64-git
mingw-w64-git-credential-manager mingw-w64-git-lfs
mingw-w64-git-sizer mingw-w64-wintoast bash curl gawk
git-flow gnupg heimdal mintty nodejs openssh openssl
perl perl-HTML-Parser perl-Locale-Gettext perl-Net-SSLeay
perl-TermReadKey perl-XML-Parser perl-YAML-Syck subversion tig
edit <file>: edit a well-known file. Well-known files are:
git-sdk.sh sdk.completion ReleaseNotes.md install.iss
reload: reload the 'sdk' function.
现在安装完成后,我们再次打开 Git for Windows 的开发文档简介,从中不难发现该项目使用了 MSYS2 项目,那么问题迎刃而解.
根据科普知识,我们知道 MSYS2 和 MinGW 都是操作系统,而 Git For Windows 将两者结合在一起,默认使用 MSYS2 的包管理工具.
功夫不负有心人,竟然真的找到了,搜索 GNU 发现了 gnuwin32 项目,完整提供了原生命令,其中就有我们需要的 tree 命令.
于是下载二进制文件找到其中的 /bin/tree.exe 并将其复制到 /git/usr/bin 目录下,这样 git bash 本身就支持 tree 命令了.
首先清除掉上一步设置的别名,防止干扰以确保此二进制文件真实有效.
adsryen@home MINGW64 /g/sublime/test
# 切换到 `git` 安装目录
$ cd /e/git
adsryen@home MINGW64 /e/git
# 编辑 `bash.bashrc` 配置文件,移除别名
$ vim ./etc/bash.bashrc
adsryen@home MINGW64 /e/git
# 查看配置文件内容,别名设置已移除
$ tail ./etc/bash.bashrc
[[ "$-" != *i* ]] && return
# Set a default prompt of: user@host, MSYSTEM variable, and current_directory
#PS1='\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[35m\]$MSYSTEM\[\e[0m\] \[\e[33m\]\w\[\e[0m\]\n\$ '
# Uncomment to use the terminal colours set in DIR_COLORS
# eval "$(dircolors -b /etc/DIR_COLORS)"
# Fixup git-bash in non login env
shopt -q login_shell || . /etc/profile.d/git-prompt.sh
adsryen@home MINGW64 /e/git
# 刷新配置文件,使其立即生效
$ source ./etc/bash.bashrc
adsryen@home MINGW64 /e/git
# 移除 `tree` 别名,适用于命令行方式设置而不是文件设置
$ unalias tree
adsryen@home MINGW64 /e/git
# 运行 `tree` 命令,确保已经无法通过别名方式调用系统的 `tree.com` 命令
$ tree
bash: tree: command not found
真的成功添加了 tree.exe 命令,明显和 cmd 自带的 tree.com 命令不一致.
adsryen@home MINGW64 /e/git
$ cd /g/sublime/test
adsryen@home MINGW64 /g/sublime/test
# 扩展命令 `tree` 帮助信息
$ tree.exe --help
usage: tree [-adfghilnpqrstuvxACDFNS] [-H baseHREF] [-T title ] [-L level [-R]]
[-P pattern] [-I pattern] [-o filename] [--version] [--help] [--inodes]
[--device] [--noreport] [--nolinks] [--dirsfirst] [--charset charset]
[--filelimit #] [<directory list>]
-a All files are listed.
-d List directories only.
-l Follow symbolic links like directories.
-f Print the full path prefix for each file.
-i Don't print indentation lines.
-q Print non-printable characters as '?'.
-N Print non-printable characters as is.
-p Print the protections for each file.
-u Displays file owner or UID number.
-g Displays file group owner or GID number.
-s Print the size in bytes of each file.
-h Print the size in a more human readable way.
-D Print the date of last modification.
-F Appends '/', '=', '*', or '|' as per ls -F.
-v Sort files alphanumerically by version.
-r Sort files in reverse alphanumeric order.
-t Sort files by last modification time.
-x Stay on current filesystem only.
-L level Descend only level directories deep.
-A Print ANSI lines graphic indentation lines.
-S Print with ASCII graphics indentation lines.
-n Turn colorization off always (-C overrides).
-C Turn colorization on always.
-P pattern List only those files that match the pattern given.
-I pattern Do not list files that match the given pattern.
-H baseHREF Prints out HTML format with baseHREF as top directory.
-T string Replace the default HTML title and H1 header with string.
-R Rerun tree when max dir level reached.
-o file Output to file instead of stdout.
--inodes Print inode number of each file.
--device Print device ID number to which each file belongs.
--noreport Turn off file/directory count at end of tree listing.
--nolinks Turn off hyperlinks in HTML output.
--dirsfirst List directories before files.
--charset X Use charset X for HTML and indentation line output.
--filelimit # Do not descend dirs with more than # files in them.
adsryen@home MINGW64 /g/sublime/test
# 原生 `tree.com` 帮助信息
$ winpty tree.com /?
卷 软件 的文件夹 PATH 列表
卷序列号为 000000CA 223E:7300
E:\GIT\?
无效的路径 - \GIT\?
没有子文件夹
虽然原生 cmd 自带的 tree.com 命令也能打印出目录结构树,但是和扩展的第三方 tree.exe 命令相比,可配置的选项实在太少,难怪固执少年会执意扩展 tree 命令.
懒人直达
如果想要在 git bash 命令行中调用 tree 命令,总结了下列几种方法.
注意: 从上到下逐渐复杂,根据自己的情况自行选择.
winpty tree.com : 直接调用 cmd 内置 tree.com 命令.
winpty cmd //c tree : 通知 cmd 调用 tree 命令.
回顾总结
Git 本身并不支持 Windows 系统,Git For Windows 团队为了移植到 Windows 平台,付出了很多努力,最直观的感受就是提供了 Git For Windows 软件.
由于 Git for Windows 的出色工作得到 Git 的官方认可,现在默认下载的 Git 就是来源于 Git for Windows.
但 Git 底层是运行在类 linux 系统的,这种差异必然需要 Windows 到 Linux 的中间处理层进行转换.
因此,Git for Window 背后的技术中涉及到大量的 GNU 自由软件操作系统和 WSL (Windows Subsystem for Linux) 的相关代码.
正是由于背后千丝万缕的联系,为我们扩展 git bash 命令行提供了一些思路.
基于 Cygwin 系统进行扩展,安装完整的操作系统,模拟出 linux 运行环境,在 Windows 上也能找到 linux 的感觉.