[toc]
引用 && 概述
这45个场景在我看来有点多了,故总结实际你我在使用git的时候,最容易遇到的场景。
并且它里面并没有实际演示,我给加上
场景总结
1、git commit --amend 修改commit
amend可以修改commit 信息和内容
commit信息填错了,可以直接 git commit --amend 修改再保存
当你commit一次过后,别人给你review代码,提了issue。
修改这些问题之后,你不想增加一个commit点,就可以使用 amend
原文件test1.c,The Master Chief
已经被commit了,在MR里面
The Master Chief
现在加一句话当作修改
The Master Chief
Spartan 117
这个时候如果需要先 git add 这个文件
add后使用 git commit --amend
first commit
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Thu Apr 28 15:40:33 2022 +0800
#
# On branch master
#
# Initial commit
#
# Changes to be committed:
# new file: test1.c
这里还可以顺便修改下commit 信息,最后保存下
这里我改成了 1.5 commit
commit f138c526a9633af8d2e744197df6879ed00ac8fa
Author:
Date: Thu Apr 28 15:40:33 2022 +0800
1.5 commit
amend过后你的commit 哈希值会改变,所以再往自己的远端仓推送,需要强推 -f
2、git rebase -i head~N 合并commit
上面的大N代表commit数量
-i 代表git rebase --interactive
,具体解释看这个链接。https://blog.csdn.net/u013826918/article/details/120173645
修改下 test1.c,然后再提交一次 second commit
The Master Chief
Spartan 117
John
修改下 test1.c,然后再提交一次 third commit
The Master Chief
Spartan 117
John
Cortana
再提交两次是因为不能rebase首节点
git log看下
commit ca3103dc53ee7aca71f776bc872fac0c23ad6a1c
Author:
Date: Thu Apr 28 16:10:59 2022 +0800
third commit
commit 0c94e5bf6fbbbe0c4cb580d3725d2ab841887c7f
Author:
Date: Thu Apr 28 16:08:50 2022 +0800
second commit
commit f138c526a9633af8d2e744197df6879ed00ac8fa
Author:
Date: Thu Apr 28 15:40:33 2022 +0800
1.5 commit
想要合并这三个commit,我们用 git rebase -i head~3
接下来,你要把除了第一行的pick,都改成s (squash)
pick 58ac02b 1.5 commit
s b9f5ad0 second commit
s 534f3bb third commit
# Rebase 534f3bb onto af2f78d (3 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# 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.
#
# Note that empty commits are commented out
保存后,会看到如下
# This is a combination of 3 commits.
# The first commit's message is:
1.5 commit
# This is the 2nd commit message:
second commit
# This is the 3rd commit message:
third commit
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Thu Apr 28 15:40:33 2022 +0800
#
# interactive rebase in progress; onto af2f78d
# Last commands done (3 commands done):
# s b9f5ad0 second commit
# s 534f3bb third commit
# No commands remaining.
# You are currently editing a commit while rebasing branch 'Zach' on 'af2f78d'.
#
#
# Initial commit
#
# Changes to be committed:
# new file: test1.c
你需要删除掉那些冗余的commit的信息,然后写你自己想写的
比如这里我写的是 fourth commit
fourth commit
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Thu Apr 28 15:40:33 2022 +0800
#
# interactive rebase in progress; onto af2f78d
# Last commands done (3 commands done):
# s b9f5ad0 second commit
# s 534f3bb third commit
# No commands remaining.
# You are currently editing a commit while rebasing branch 'Zach' on 'af2f78d'.
#
#
# Initial commit
#
# Changes to be committed:
# new file: test1.c
再保存就完事了
git log看到只剩最后一次commit
commit 137ce1f60cf9e5db3a66795b360cd8ae87e20dac
Author:
Date: Thu Apr 28 15:40:33 2022 +0800
fourth commit
3、git reset 硬软方式 适用不同场景
-
git reset + commit哈希
软重置
只会弹出你填的这个commit点之前的所有commit,但是不影响内容。应用场景:如果你一个MR连着commit了10次,那么等哪天CIE cherry-pick的时候,估计想打死你(我见过最多的commit点是一个MR 81个,西天取经了解一下)
遇到这种情况,想要合并成一个,你要么 rebase 10个,改9个squash。
或者直接reset之前的节点,弹出所有,然后从新commit一次 -
git reset + commit哈希 --hard
硬重置会直接消除掉你之前的那些commit + 代码内容,如果你没有远端保存,那么请一定慎用。
4、reset --hard
之后怎么找回之前的commit
使用git reflog
这个东西回保存你近期所有的操作。
git reflog
137ce1f HEAD@{0}: rebase -i (finish): returning to refs/heads/Zach
137ce1f HEAD@{1}: rebase -i (squash): fourth commit
e089082 HEAD@{2}: rebase -i (squash): # This is a combination of 2 commits.
c055f67 HEAD@{3}: rebase -i (pick): 1.5 commit
0b62948 HEAD@{4}: rebase -i (pick): 1.5 commit
af2f78d HEAD@{5}: rebase -i (start): checkout af2f78d4c4946885f5690ca4203f4909087d41aa
534f3bb HEAD@{6}: rebase -i (finish): returning to refs/heads/Zach
534f3bb HEAD@{7}: rebase -i (pick): third commit
b9f5ad0 HEAD@{8}: rebase -i (pick): second commit
58ac02b HEAD@{9}: rebase -i (pick): 1.5 commit
063c7ec HEAD@{10}: rebase -i (pick): 1.5 commit
26a72b7 HEAD@{11}: rebase -i (start): checkout 26a72b7daada928968577c4afb20e4ff551cc3e5
ca3103d HEAD@{12}: checkout: moving from master to Zach
ca3103d HEAD@{13}: commit: third commit
0c94e5b HEAD@{14}: commit: second commit
f138c52 HEAD@{15}: commit (amend): 1.5 commit
dabb63b HEAD@{16}: commit (initial): first commit
如果你reset --hard
完了,就在里面找到那个最近的 reset,复制他前面的哈希值,再写一下 git reset --hard +哈希值
,就会发现又复原了。
这样子可行的原理可以看我的另一篇博客
http://3ms.huawei.com/km/blogs/details/12150701?l=zh-cn
实际上,你reset --hard
后,那些保存的commit点信息虽然git log里面看不到了,实际上并没有被丢弃,所以只要你哈希值输入对了,就能找回去。
5、切换到一个远端有,本地没有的分支
正确示范
假设远端那个分支叫 zach_test
git fetch + 你的远端仓
git checkout --track 你的远端仓/zach_test
错误示范
没看这个文档之前,我的操作会是直接checkout,没有--track
git fetch + 你的远端仓
git checkout 你的远端仓/zach_test
这样子你git status
会发现这个分支的头节点是没有detached,所以没办法push
6、宏观观察项目整体合入内容
git log --graph --oneline