evantianx / Bloooooooog

Place to record what I thought and learned
0 stars 0 forks source link

关于 Git 的一切 #40

Open evantianx opened 7 years ago

evantianx commented 7 years ago

参考文章:

Little Things I Like to Do with Git Git from the inside out

evantianx commented 7 years ago

查看团队成员提交量

# -s 代表不显示提交信息而只显示提交量
# -n 代表用提交量排序而不是按照提交人 id 的首字母排序
# 注意: 这里的提交量为整个项目周期内所有的提交量
$ git shortlog -sn

the -s flag will suppress commit description and provide a commit count summary only. the -n flag will sort output according to the number of commits per author instead of author alphabetic order.

当我们想比较确切时间范围内的提交量时,可以使用--since--until:

$ git shortlog -sn --since="10 weeks" --until="2 weeks"

blame or praise

使用 git blame 可以轻易查找到某行代码的“责任人”

# 查找 _component.buttons.scss 文件中的 5 到 10 行代码是谁写的
$ git blame -L5,10 _components.buttons.scss

我们也可以将其改为称赞某人的功能:

$ git config --global alias.praise blame

这样就将 blame 改为 praise,看到好的代码不妨 git praise 下,当然最好是请他喝杯咖啡啦!☕️

隐藏空格修改提示

当我们 diff 或者 show 一个文件时,会有很多空格修改的提示,对于寻找真正的目标造成了很大的干扰。 此时可以添加 -w 来避免。

$ git diff -w
$ git show -w

直接显示增删

默认情况下, git diff在某行修改时,会在原始行后显示被修改后的样子:

-My friend Tom recently gave an excellent talk
+My good friend Tom gave an excellent talk

我们如果只关注修改的单词字段,完全没有必要这样查看,加上 --word-diff 后:

My {+good+} friend Tom [-recently-] gave an excellent talk

查看最近修改的分支

可以方便地查看最近在哪些分支上工作过:

# --count=10 最近的10个分支
# --sort=-committerdate 按照提交时间排序
# refs/heads/ 只显示本地分支
# --format="%(refname:short)" 格式化
$ git for-each-ref --count=10 --sort=-committerdate refs/heads/ --format="%(refname:short)"

快速检出上一个分支

$ git checkout -

去除空白

# 去掉行尾空白符
# 多个空行缩成一行
$ git stripspace < README.md

美化 Git Status

# 可以省略很多不必要的提示
$ git status -sb
evantianx commented 6 years ago

git init

该命令会在根目录下创建一个 .git 文件夹,用来存放关于 git 配置及版本历史相关信息。

git add

该命令分两个步骤完成:

evantianx commented 6 years ago

Git hash-object

echo 'Hello, World!' | git hash-object --stdin

等价于:

echo 'blob 14\0Hello, World!' | openssl sha1
evantianx commented 5 years ago

Git alias

Git aliases I can't live without