之前使用 Git 版本控制工具的时候,基本都是使用图形化工具,如 SourceTree,TortoiseGit (小乌龟),对 GIt 命令也不是很熟悉。后面由于工作的原因,切换到 Ubuntu 开发,不得已使用 Git 命令,而每次在提交代码的时候,都要敲一堆 Git 命令,有时候会忘了,效率相对来说比较低。查了相关的资料,了解dao Git 提供了别名 (alias) 功能,方便我们对常用的 Git 命令进行自定义封装。
配置 GIt 别名是使用 git config --global alias 命令来配置,比如 我想用 st 代表 status,name我们可以这样设置。
git config --global alias.st status
以后只需要输入git st,即可查看当前 git 仓库的状态
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.pl pull --rebase
git config --global alias.lg “log --graph --pretty=format:‘%Cred%h%Creset -%C(yellow)%d%Creset %C(bold blue)%s%Creset %Cgreen(%cr) <%an>%Creset’ --abbrev-commit --date=relative”
git config --global alias.lga “log --graph --pretty=format:‘%Cred%h%Creset -%C(yellow)%d%Creset %C(bold blue)%s%Creset %Cgreen(%cr) <%an>%Creset’ --abbrev-commit --date=relative --author xujun”
git config --global alias.last “log -1”
git config 文件的存放位置主要有三个位置。
1)./etc/gitconfig 文件:包含了适用于系统所有用户和所有库的值。如果你传递参数选项’–system’ 给 git config,它将明确的读和写这个文件。
git config --system user.name “xujun”
git config --system user.email “gdutxiaoxu@163.com"
2)~/.gitconfig 文件 :具体到你的用户。你可以通过传递 --global 选项使 Git 读或写这个特定的文件。
git config --global user.name “xujun”
git config --global user.email “gdutxiaoxu@163.com"
- 位于 git 目录的 config 文件 (也就是 .git/config) :无论你当前在用的库是什么,特定指向该单一的库。每个级别重写前一个级别的值。因此,在.git/config中的值覆盖了在/etc/gitconfig中的同一个值。
在 Windows 系统上,Git 会找寻用户主目录下的 .gitconfig 文件。主目录即 $HOME 变量指定的目录,一般都是 C:\Users{UserName} 。此外,Git 还会尝试找寻 /mingw64/etc/gitconfig 文件,只不过看当初 Git 装在什么目录,就以此作为根目录来定位。 比如我的 git 安装目录是 C:\Program Files\Git, 那么相应的文件位置是C:\Program Files\Git\mingw64\etc 。
因此在配置别名的时候,如果指定 --system ,将会对所有的用户生效。
指定 --global 的时候,会对当前用户生效。 没有指定 --system 或者 --global 的时候,只在当前仓库生效