Git 个人开发使用场景
删除不需要的分支
gitk --all
git branch -d [branch_name] // 删除本地分支
git branch -D [branch_name] // 强制删除本地分支
git branch -a // 显示全部分支
git branch -v // 本地分支 详细信息
修改最新 commit 的 message
git commit --amend // 允许修改最新 commit 的 message
修改旧的 commit 的 message
git rebase -i [commit parent hash]
git rebase -i 50234f8e848555b4995849e1c7564scd6b32b506d
// 团队协作时禁止使用,如果代码已经提交到远程分支,代码合并时会产生新的提交记录
pick bb4b896 docs: update words
pick 1ae4b2e perf: 二分查找的实现及特性
pick 6e3f289 perf: git 修改最新提交信息
# Rebase 50234f8..6e3f289 onto 50234f8 (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup [-C | -c] <commit> = like "squash" but keep only the previous
# commit's log message, unless -C is used, in which case
# keep only this commit's message; -c is same as -C but
# opens the editor
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified); use -c <commit> to reword the commit message
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
->
r bb4b896 docs: update words
pick 1ae4b2e perf: 二分查找的实现及特性
pick 6e3f289 perf: git 修改最新提交信息
docs: update words
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Mon Jan 17 08:00:10 2022 +0800
#
# interactive rebase in progress; onto 50234f8
# Last command done (1 command done):
# reword bb4b896 docs: update words
# Next commands to do (4 remaining commands):
# pick 1ae4b2e perf: 二分查找的实现及特性
# pick 6e3f289 perf: git 修改最新提交信息
# You are currently editing a commit while rebasing branch 'master' on '50234f8'.
#
# Changes to be committed:
# modified: alg/training/README.md
#
->
docs: update word # 保存即可
变基操作实际上也使用分离头指针操作。
合并连续多个 commit
git rebase -i [parent_commit]
使用 s,squash,meld into pervious commit。
pick 992e00c docs: update docs
pick d94225d perf: merge two update:
pick 442ccb0 perf: update
# Rebase 1d556bc..442ccb0 onto 1d556bc (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup [-C | -c] <commit> = like "squash" but keep only the previous
# commit's log message, unless -C is used, in which case
# keep only this commit's message; -c is same as -C but
# opens the editor
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified); use -c <commit> to reword the commit message
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
=>
pick 992e00c docs: update docs
s d94225d perf: merge two update:
s 442ccb0 perf: update
# Rebase 1d556bc..442ccb0 onto 1d556bc (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup [-C | -c] <commit> = like "squash" but keep only the previous
# commit's log message, unless -C is used, in which case
# keep only this commit's message; -c is same as -C but
# opens the editor
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified); use -c <commit> to reword the commit message
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
=>
# This is a combination of 3 commits.
perf: git merge commit test
# This is the 1st commit message:
docs: update docs
# This is the commit message #2:
perf: merge two update:
perf: update
perf: update
# This is the commit message #3:
perf: update
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Mon Jan 17 22:02:12 2022 +0800
#
# interactive rebase in progress; onto 1d556bc
# Last commands done (3 commands done):
# squash d94225d perf: merge two update:
# squash 442ccb0 perf: update
# No commands remaining.
# You are currently rebasing branch 'master' on '1d556bc'.
#
# Changes to be committed:
# modified: git/README.md
#
=>
[detached HEAD c3657c8] perf: git merge commit test
Date: Mon Jan 17 22:02:12 2022 +0800
1 file changed, 39 insertions(+), 1 deletion(-)
Successfully rebased and updated refs/heads/master.
合并间隔的 commit
git log --graph // 查看 Git 提交日志
git rebase -i [commit_hash]
// 如果是根 commit 不显示,可以自己添加 commit_hash,或者使用 git rebase -i --root
pick [commit_hash] //
pick [commit_hash]
s [commit_hash_123]
=>
pick [commit_hash]
s [commit_hash_123] // 可以自主移动位置
pick [commit_hash]
比较暂存区和 HEAD 文件差异
git diff --cached
=>
diff --git a/git/README.md b/git/README.md
index fbb6644..ff14cc2 100644
--- a/git/README.md
+++ b/git/README.md
@@ -604,6 +604,37 @@ Successfully rebased and updated refs/heads/master.
### 合并间隔的 commit
+```js
+git log --graph // 查看 Git 提交日志
+```
+
+```js
+git rebase -i [commit_hash]
+
+// 如果是根 commit 不显示,可以自己添加 commit_hash,或者使用 git rebase -i --root
+```
+
+```js
+pick [commit_hash] //
+pick [commit_hash]
+s [commit_hash_123]
+
+=>
+
+pick [commit_hash]
+s [commit_hash_123] // 可以自主移动位置
+pick [commit_hash]
+```
+
+### 比较暂存区和 HEAD 文件差异
+
+```js
+git diff --cached // 比较暂存区和 HEAD
+```
比较工作区和暂存区文件差异
git diff
diff --git a/git/README.md b/git/README.md
index 17423f7..a146534 100644
--- a/git/README.md
+++ b/git/README.md
@@ -675,6 +675,7 @@ index fbb6644..ff14cc2 100644
### 比较工作区和暂存区文件差异
```js
+git diff
git diff -- .\git\README.md // 可以指定文件进行对比
将暂存区恢复到 HEAD
应用场景:已经确定不想保留暂存区的变更,其次工作区还没有改好,这时又想把暂存区恢复到 HEAD。
git reset HEAD // 重置暂存区内容
git reset --soft // 把 HEAD 指向的 commit 恢复到你指定的 commit,暂存区、工作区不变
git reset --hard // 把 HEAD,暂存区,工作区 都修改为 你指定的 commit 的时候的文件状态
git reset --mixed // 默认参数,把 HEAD,暂存区 修改为 你指定的 commit 的时候的文件状态,工作区保持不变
将工作区恢复为暂存区
git checkout ./
Git 2.23 之后用 git switch 和 git restore 来替代 git checkout 功能。
git switch 替换 git checkout 切换分支的功能,git restore 替换对工作区文件进行恢复的功能。
取消暂存区部分文件更改
git reset HEAD ./styles/style.css
消除最近的几次提交
git reset --hard [commit_hash] // 强制回退
比较不同提交文件差异
# 比较 master、develop 分支 README 文件的差异
git diff master develop ./README.md
# 比较不同提交记录文件的差异,可以指定不同分支的 commit
git diff [commit_hash] [commit_hash]
master、develop 其实也是一种 commit,指向最近的一次提交。
正确删除文件的方法
git rm ./README.md
开发中临时加塞紧急任务
# 保存当前工作区和暂存区内容,此时工作区和暂存区会恢复到 HEAD
git stash
# 查看栈中的存储列表
git stash list
# 弹出保存内容,不移除栈中信息
git stash apply
# 弹出保存内容,移除栈中信息
git stash pop
指定不需要 Git 管理文件
.gitignore
# 不管理 js 及其子目录
*.js
# 不管理 js 子目录
*.js/
提交 commit 后,想再忽略一些已经提交的文件,可以这样处理:
把想忽略的文件添加到 .gitignore ;然后通过git rm -- cached name_of_file
的方式删除掉 git 仓库里面无需跟踪的文件。
将 git 仓库备份到本地
# 备份仓库(哑协议,不存在进度条)
git clone --bare xxxxxxx/.git test.git
# 备份仓库(智能协议,存在进度条)
git clone --bare file:///xxxxxxx/.git test2.git
# 将本仓库推送到备份仓库
git remote add test2 file:///xxxxxxx/.test2.git
git push --set-upstream test2
–bare 克隆裸仓库。
备份仓库只克隆版本库(.git)就可以了,节约空间,如果以后想恢复备份的仓库,用版本库就可以恢复出工作目录。