系列文章目录
分享一些 Git 使用心得,并不是一个完整教程
配置身份
git config --global user.name "张三"
git config --global user.email zhangsan@example.com
这里配置的 user.name
和 user.email
可以和 git服务器账号名不同。但是最好能保持一直,以便统一日志中的信息
在日志的身份信息会像如下显示
commit 123412341234123412341234
Author: 张三 <zhangsan@yilunjk.com>
Date: Mon Jun 12 16:34:13 2023 +0800
修复2333号bug
减少 merge
git config --global pull.rebase true
使用 git pull --rebase
可以避免在本地和远程仓库之间出现不必要的合并提交记录,使提交历史更加简洁干净,效果如下:
清理分支
git config --global fetch.prune true
git config --global fetch.pruneTags true
以上配置的作用是在 git fetch
时,删除本地已经不存在的远程分支或 tag 的引用。即清理本地已经失效的远程分支或 tag,使本地仓库与远程仓库保持同步。
美化日志
git log --graph --pretty=oneline
输出的日志其中一行如下:
59ae49bd18b0a992d1dd0d5b649c6b186596811a feature(ins) fix bugs
没有时间、提交者,commit id 太长也占用显示空间,使用下面的命令配置一个日志别名
git config --global alias lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%ci) %C(bold blue)<%an>%Creset'"
使用 git lg
输出的日志,每一行看起来如下:
59ae49bd1 - feature(ins) fix bugs (2023-06-09 15:02:16 +0800) <张三>
总结
git config --global fetch.prune true && \
git config --global fetch.pruneTags true && \
git config --global merge.ff no && \
git config --global pull.rebase true
可以使用这个命令一次配置好