From be7348551652a6b5bcb3cfc6d08b230d0bcb40dc Mon Sep 17 00:00:00 2001 From: jaywcjlove <398188662@qq.com> Date: Wed, 16 Aug 2023 21:52:40 +0800 Subject: [PATCH] doc: update docs/git.md --- docs/git.md | 284 ++++++++++++++++++++++++++++------------------------ 1 file changed, 151 insertions(+), 133 deletions(-) diff --git a/docs/git.md b/docs/git.md index 9655e73..79171f4 100644 --- a/docs/git.md +++ b/docs/git.md @@ -435,6 +435,134 @@ ProxyCommand nc -X 5 -x 127.0.0.1:1080 %h %p ``` +Commit +--- + +### 改写历史 + + +重写最后的提交消息 + +```shell +$ git commit --amend -m "new message" +``` + +修改最新的提交而不更改提交消息 + +```shell +$ git commit --amend --no-edit +``` + +### 在 commit log 中显示 GPG 签名 + +```bash +$ git log --show-signature +``` + +### 修改远程 Commit 记录 + + +```shell +$ git rebase -i HEAD~3 +# 表示要修改当前版本的倒数第三次状态 +# 将要更改的记录行首单词 pick 改为 edit +pick 96dc3f9 提交 commit 描述内容 1 +pick f1cce8a 提交 commit 描述内容 2 +pick 6293516 提交 commit 描述内容 3 +# Rebase eeb03a4..6293516 onto eeb03a4 +# (3 commands) +# +# Commands: +# p, pick = 使用提交 +# r, reword = 使用提交,但编辑提交消息 +# e, edit = 使用提交,但停止修改 +# s, squash = 使用提交,但融合到先前的提交中 +# f, fixup = 像 squash,但丢弃此提交的日志消息 +# x, exec = 使用 shell 运行命令(该行的其余部分) +# d, drop = 删除提交 +``` + +保存并退出,会弹出下面提示 + +```shell +# 您现在可以修改提交,使用 +# +# git commit --amend +# +# 对更改感到满意后,运行 +# +# git rebase --continue +# +# 1. 通过这条命令进入编辑更改 commit,保存退出 +$ git commit --amend +# 2. 保存退出确认修改,继续执行下面命令, +$ git rebase --continue +# 如果修改多条记录反复执行上面两条命令直到完成所有修改 + +# 最后,确保没有人提交进行推送,最好不要加 -f 强制推送 +$ git push -f origin master +``` + +### Commit + +```shell +$ git commit -v --amend +``` + +重写最后的提交信息 + +### 撤销远程记录 + +```shell +# 撤销一条记录 +$ git reset --hard HEAD~1 +# 强制同步到远程仓库 +$ git push -f origin HEAD:master +``` + +### 放弃本地修改内容 + +```shell +# 如果有的修改以及加入暂存区的话 +$ git reset --hard +# 还原所有修改,不会删除新增的文件 +$ git checkout . +# 下面命令会删除新增的文件 +$ git clean -xdf +``` + +### 把 A 分支的某一个 commit,放到 B 分支上 + +```shell +# 切换到 B 分支 +$ git checkout +# 将 A 分支 的内容 pick 到 B 分支 +$ git cherry-pick +``` + +### 重设第一个 commit + +```bash +$ git update-ref -d HEAD +``` + +把所有的改动都重新放回工作区,并**清空所有的 commit**,这样就可以重新提交第一个 `commit` 了 + +### 回到远程仓库的状态 + +```bash +$ git fetch --all && git reset --hard origin/master +``` + + +抛弃本地所有的修改,回到远程仓库的状态 + +### commit 历史中显示 Branch1 有的但是 Branch2 没有 commit + +```bash +$ git log Branch1 ^Branch2 +``` + Git 技巧 ------ @@ -515,14 +643,6 @@ $ git checkout -- git remote prune origin ``` -### Commit - -```shell -$ git commit -v --amend -``` - -重写最后的提交信息 - ### 忽略文件的权限变化 ```shell @@ -553,70 +673,6 @@ $ git config core.ignorecase false $ git rm -r --cached <目录/文件> ``` -### 修改远程 Commit 记录 - - -```shell -$ git rebase -i HEAD~3 -# 表示要修改当前版本的倒数第三次状态 -# 将要更改的记录行首单词 pick 改为 edit -pick 96dc3f9 提交 commit 描述内容 1 -pick f1cce8a 提交 commit 描述内容 2 -pick 6293516 提交 commit 描述内容 3 -# Rebase eeb03a4..6293516 onto eeb03a4 -# (3 commands) -# -# Commands: -# p, pick = 使用提交 -# r, reword = 使用提交,但编辑提交消息 -# e, edit = 使用提交,但停止修改 -# s, squash = 使用提交,但融合到先前的提交中 -# f, fixup = 像 squash,但丢弃此提交的日志消息 -# x, exec = 使用 shell 运行命令(该行的其余部分) -# d, drop = 删除提交 -``` - -保存并退出,会弹出下面提示 - -```shell -# 您现在可以修改提交,使用 -# -# git commit --amend -# -# 对更改感到满意后,运行 -# -# git rebase --continue -# -# 1. 通过这条命令进入编辑更改 commit,保存退出 -$ git commit --amend -# 2. 保存退出确认修改,继续执行下面命令, -$ git rebase --continue -# 如果修改多条记录反复执行上面两条命令直到完成所有修改 - -# 最后,确保没有人提交进行推送,最好不要加 -f 强制推送 -$ git push -f origin master -``` - -### 撤销远程记录 - -```shell -# 撤销一条记录 -$ git reset --hard HEAD~1 -# 强制同步到远程仓库 -$ git push -f origin HEAD:master -``` - -### 放弃本地修改内容 - -```shell -# 如果有的修改以及加入暂存区的话 -$ git reset --hard -# 还原所有修改,不会删除新增的文件 -$ git checkout . -# 下面命令会删除新增的文件 -$ git clean -xdf -``` - ### 获取最近一次提交的 Hash ```shell @@ -632,32 +688,6 @@ $ git branch --merged master | grep -v '^\*\| master' | xargs -n 1 git branch - ``` -### 把 A 分支的某一个 commit,放到 B 分支上 - -```shell -# 切换到 B 分支 -$ git checkout -# 将 A 分支 的内容 pick 到 B 分支 -$ git cherry-pick -``` - -### 回到远程仓库的状态 - -```bash -$ git fetch --all && git reset --hard origin/master -``` - - -抛弃本地所有的修改,回到远程仓库的状态 - -### 重设第一个 commit - -```bash -$ git update-ref -d HEAD -``` - -把所有的改动都重新放回工作区,并**清空所有的 commit**,这样就可以重新提交第一个 `commit` 了 - ### 查看冲突文件列表 ```bash @@ -733,14 +763,6 @@ $ git remote show origin $ git describe --tags --abbrev=0 ``` -### 查看某段代码是谁写的 - -```bash -$ git blame -``` - -`blame` 的意思为`责怪`,你懂的。 - ### 修改作者名 ```bash @@ -767,12 +789,6 @@ $ git remote add origin $ git remote -v ``` -### 查看两个星期内的改动 - -```bash -$ git whatchanged --since='2 weeks ago' -``` - ### 从 stash 中拿出某个文件的修改 ```bash @@ -846,18 +862,6 @@ $ git clean -X -f $ git status --ignored ``` -### commit 历史中显示 Branch1 有的但是 Branch2 没有 commit - -```bash -$ git log Branch1 ^Branch2 -``` - -### 在 commit log 中显示 GPG 签名 - -```bash -$ git log --show-signature -``` - ### 新建并切换到新分支上,同时这个分支没有任何 commit ```bash @@ -934,14 +938,6 @@ git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads 最新的放在最上面 -### 在 commit log 中查找相关内容 - -```bash -git log --all --grep='' -``` - -通过 grep 查找,given-text: 所需要查找的字段 - ### 把暂存区的指定 file 放到工作区中 ```bash @@ -960,7 +956,7 @@ Host github.com ``` -git 代码统计 +统计查询 --- ### 查看 git 上的个人代码量 @@ -1000,6 +996,28 @@ git log --pretty='%aN' | sort | uniq -c | sort -k1 -n -r | head -n 10 git log --oneline | wc -l ``` +### 查看某段代码是谁写的 + +```bash +$ git blame +``` + +`blame` 的意思为`责怪`,你懂的。 + +### 查看两个星期内的改动 + +```bash +$ git whatchanged --since='2 weeks ago' +``` + +### 在 commit log 中查找相关内容 + +```bash +git log --all --grep='' +``` + +通过 grep 查找,given-text: 所需要查找的字段 + Conventional Commmits ----