LinkXSystem / learn-guide

有趣的学习笔记 (*^_^*)
https://linkxsystem.github.io/
3 stars 1 forks source link

Git 的常用技巧 =͟͟͞͞ ٩( ๑╹ ꇴ╹)۶ #6

Open LinkXSystem opened 4 years ago

LinkXSystem commented 4 years ago

记录 git 的常用技巧,代码的托管平台为 github / gitlab

git 相关的项目

LinkXSystem commented 4 years ago

更新最新的 commit 的 comment (注意:代码尚未推送到远程)

git commit --amend
LinkXSystem commented 4 years ago

使用 graph 显示 git 的 log (PS: 文档地址

git log --graph

通常我们可以结合 --pretty 参数一起使用, 能够更便捷的查看 log 的信息。默认的情况下,上面的命令等同于下面命令:

git log --pretty=full --graph

--pretty 参数以下通用的模式:

<sha1> <title line>
This is designed to be as compact as possible.
commit <sha1>
Author: <author>
<title line>
commit <sha1>
Author: <author>
Date:   <author date>
<title line>
<full commit message>
commit <sha1>
Author: <author>
Commit: <committer>
<title line>
<full commit message>
commit <sha1>
Author:     <author>
AuthorDate: <author date>
Commit:     <committer>
CommitDate: <committer date>
<title line>
<full commit message>
From <sha1> <date>
From: <author>
Date: <author date>
Subject: [PATCH] <title line>
<full commit message> 

那么如果上述的模式无法满足你的需要,那么你可以在 --pretty 中使用 format 来实现格式的定制,下面这是 format 的示例:

# %h 代表缩写的 commit 的 ID
# %s 代表 commit 的简述
git log --pretty=format:'%h %s' --graph

而详细的格式参数则详见此文档,地址

LinkXSystem commented 4 years ago

统计 log 的 commit 的文件改动

git log --stat

对比暂存区与上一次 commit 之间的差异

git diff --staged
LinkXSystem commented 3 years ago

开启 Git 命令的自动更更正

git config --global help.autocorrect 1
LinkXSystem commented 3 years ago

统计代码提交量

git rev-list --count branch-name
LinkXSystem commented 3 years ago

Git 的垃圾收集功能

git gc --prune=now --aggressive
LinkXSystem commented 3 years ago

备份未被跟踪的文件

git ls-files --others --exclude-standard -z |\
xargs -0 tar rvf ~/backup-untracked.zip
LinkXSystem commented 3 years ago

查看另一个分支的文件

git show main:README.md
LinkXSystem commented 3 years ago

在 Git 中搜索

不需要明确指定分支

git rev-list --all | xargs git grep -F 'string'