0%

Mosh的Git课程笔记(6)--Rewriting History

Mosh的课程网址

Why Rewrite History?

1
2
3
4
------ Bad History ------
👉 Poor commit messages
👉 Large commits
👉 Small commits

我们想要 clean history, 知道我们改了啥

1
2
3
4
5
6
------------- Tools -------------
👉 Squash small, related commits
👉 Split large commits
👉 Reword commit messages
👉 Drop unwanted commits
👉 Modify commits

The Golden Rule of Rewriting History

1
DON'T REWRITE PUBLIC HISTORY!

Example of a Bad History

1
2
3
4
5
6
7
8
9
10
(master)$ git log --oneline --all --graph
* 475ea30 (HEAD -> master) .
* 688a4d0 WIP
* b0201fc Update terms of service and Google Map SDK version.
* db94b15 WIP
* 175d394 Add a reference to Google Map SDK.
* 24f3009 Change the color of restaurant icons.
* 127cf78 Fix a typo
* 17de14d Render restaurants the map.
* 00315a1 Initial commit
1
2
3
4
5
6
7
8
17de14d 应该是Render restaurants on the map.(少了一个on)
127cf78 Fix a typo是一个noise of history,应该合到17de14d去
24f3009 也应该合到17de14d去,形成一个单一逻辑的commit
175d394 应该在17de14d之前commit,没有reference怎么使用这个map?
db94b15 noise of history,扔掉或者更改提交信息
b0201fc 应该分成两个commit
688a4d0 同db94b15
475ea30 不知道干了啥,扔掉吧

Undoing Commits

soft:相当于回到git commit之前的状态→Removes the commit only

6_1

mixed:相当于回到git add之前的状态,即还没放进staging area→Unstages files

6_2

hard:最初的起点,啥都还没干→Discards local changes

6_3

1
2
3
4
5
6
7
8
9
10
11
12
(master)$ git reset --hard HEAD~1
HEAD is now at 688a4d0 WIP

(master)$ git log --oneline --all --graph
* 688a4d0 (HEAD -> master) WIP
* b0201fc Update terms of service and Google Map SDK version.
* db94b15 WIP
* 175d394 Add a reference to Google Map SDK.
* 24f3009 Change the color of restaurant icons.
* 127cf78 Fix a typo
* 17de14d Render restaurants the map.
* 00315a1 Initial commit

好的,最近的commit就扔掉了,仿佛没有存在过。

Reverting Commits

如果commit已经share with others,就不能直接reset了,而是用revert,保留log中的记录。

将最近的三条commit撤销(反做),其中 HEAD~3 指的是175d394,是不含在内的(是不会被反做的):

1
2
3
4
5
6
7
(master)$ git revert HEAD~3..HEAD
[master 12ad6f2] Revert "WIP"
1 file changed, 1 deletion(-)
[master 83527ab] Revert "Update terms of service and Google Map SDK version."
1 file changed, 1 deletion(-)
[master 667bde8] Revert "WIP"
1 file changed, 1 deletion(-)

但是这就有三条commit history了,一点都不clean:

1
2
3
4
5
6
7
8
9
10
11
12
(master)$ git log --oneline --all --graph
* 667bde8 (HEAD -> master) Revert "WIP"
* 83527ab Revert "Update terms of service and Google Map SDK version."
* 12ad6f2 Revert "WIP"
* 688a4d0 WIP
* b0201fc Update terms of service and Google Map SDK version.
* db94b15 WIP
* 175d394 Add a reference to Google Map SDK.
* 24f3009 Change the color of restaurant icons.
* 127cf78 Fix a typo
* 17de14d Render restaurants the map.
* 00315a1 Initial commit

所以可以不commit,反做的记录都在staging area里:

1
2
3
4
(master)$ git revert --no-commit HEAD~3..

(master|REVERTING)$ git status -s
M file1.txt

用—continue来继续,并手动将commit message改为”Revert bad code.“:

1
2
3
(master|REVERTING)$ git revert --continue
[master 5322dae] Revert bad code.
1 file changed, 3 deletions(-)

这样就只有一条history啦:

1
2
3
4
5
6
7
8
9
10
(master)$ git log --oneline --all --graph
* 5322dae (HEAD -> master) Revert bad code.
* 688a4d0 WIP
* b0201fc Update terms of service and Google Map SDK version.
* db94b15 WIP
* 175d394 Add a reference to Google Map SDK.
* 24f3009 Change the color of restaurant icons.
* 127cf78 Fix a typo
* 17de14d Render restaurants the map.
* 00315a1 Initial commit

Recovering Lost Commits

不小心reset了HEAD:

1
2
(master)$ git reset --hard HEAD~6
HEAD is now at 127cf78 Fix a typo

log都没了咋办:

1
2
3
4
(master)$ git log --oneline --all --graph
* 127cf78 (HEAD -> master) Fix a typo
* 17de14d Render restaurants the map.
* 00315a1 Initial commit

其实人家Git给你存得好好的呢:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
(master)$ git reflog
127cf78 (HEAD -> master) HEAD@{0}: reset: moving to HEAD~6
5322dae HEAD@{1}: commit: Revert bad code.
688a4d0 HEAD@{2}: reset: moving to HEAD~3
667bde8 HEAD@{3}: revert: Revert "WIP"
83527ab HEAD@{4}: revert: Revert "Update terms of service and Google Map SDK version."
12ad6f2 HEAD@{5}: revert: Revert "WIP"
688a4d0 HEAD@{6}: reset: moving to 688a4d0
fd52d6e HEAD@{7}: revert: Revert "Update terms of service and Google Map SDK version."
f2139a9 HEAD@{8}: revert: Revert "WIP"
688a4d0 HEAD@{9}: reset: moving to HEAD~1
475ea30 HEAD@{10}: commit: .
688a4d0 HEAD@{11}: commit: WIP
b0201fc HEAD@{12}: commit: Update terms of service and Google Map SDK version.
db94b15 HEAD@{13}: commit: WIP
175d394 HEAD@{14}: commit: Add a reference to Google Map SDK.
24f3009 HEAD@{15}: commit: Change the color of restaurant icons.
127cf78 (HEAD -> master) HEAD@{16}: commit: Fix a typo
17de14d HEAD@{17}: commit: Render restaurants the map.
00315a1 HEAD@{18}: commit (initial): Initial commit

用 commit ID 或者 entry ID 都可以恢复(此处用entry ID):

1
2
3
4
5
6
7
8
9
10
11
12
13
(master)$ git reset --hard HEAD@{1}
HEAD is now at 5322dae Revert bad code.

(master)$ git log --oneline --all --graph
* 5322dae (HEAD -> master) Revert bad code.
* 688a4d0 WIP
* b0201fc Update terms of service and Google Map SDK version.
* db94b15 WIP
* 175d394 Add a reference to Google Map SDK.
* 24f3009 Change the color of restaurant icons.
* 127cf78 Fix a typo
* 17de14d Render restaurants the map.
* 00315a1 Initial commit

假如有个Pointer是feature:

1
$ git reflog show feature

可以看到这个pointer的所有历史。

Amending Commit

Amending the Last Commit

假如现在有个 commit:

1
2
3
4
5
6
7
(master)$ echo cafes >> file1.txt

(master)$ git commit -am "Render cafes on the map"
warning: LF will be replaced by CRLF in file1.txt.
The file will have its original line endings in your working directory
[master b4c943f] Render cafes on the map
1 file changed, 1 insertion(+)

交了之后发现我们不是想写 cafes,而是想写 blue cafes 啊,但是这是个小错,能不能小修补?当然可以。

先打开 file1.txt 编辑,然后存入 staging area,最后使用 —amend 选项 commit,commit message 不变:

1
2
3
4
5
6
7
8
(master)$ code file1.txt

(master)$ git add .

(master)$ git commit --amend
[master a60e09a] Render cafes on the map
Date: Fri Apr 30 14:38:19 2021 +0800
1 file changed, 1 insertion(+)

可以看到还是这条commit,但是file1.txt里的内容已经改过来了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
(master)$ git log --oneline --all --graph
* a60e09a (HEAD -> master) Render cafes on the map
* 5322dae Revert bad code.
* 688a4d0 WIP
* b0201fc Update terms of service and Google Map SDK version.
* db94b15 WIP
* 175d394 Add a reference to Google Map SDK.
* 24f3009 Change the color of restaurant icons.
* 127cf78 Fix a typo
* 17de14d Render restaurants the map.
* 00315a1 Initial commit

(master)$ git show HEAD
commit a60e09a0bff95afbb3c83ca20591cec3c9afe638 (HEAD -> master)
Author: Stone <masaike@qq.com>
Date: Fri Apr 30 14:38:19 2021 +0800

Render cafes on the map

diff --git a/file1.txt b/file1.txt
index cb224a0..c44f4be 100644
--- a/file1.txt
+++ b/file1.txt
@@ -3,3 +3,4 @@ restaurants
fix a typo
color-red
ref-of-sdk
+blue cafes //-------看这里-----------

假如是想增加一个新文件,还是一样的操作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
(master)$ echo hello > file2.txt

(master)$ git add .
warning: LF will be replaced by CRLF in file2.txt.
The file will have its original line endings in your working directory

(master)$ git commit --amend
[master 5bbe8a7] Render cafes on the map
Date: Fri Apr 30 14:38:19 2021 +0800
2 files changed, 2 insertions(+)
create mode 100644 file2.txt

(master)$ git log --oneline --all --graph
* 5bbe8a7 (HEAD -> master) Render cafes on the map
* 5322dae Revert bad code.
* 688a4d0 WIP
* b0201fc Update terms of service and Google Map SDK version.
* db94b15 WIP
* 175d394 Add a reference to Google Map SDK.
* 24f3009 Change the color of restaurant icons.
* 127cf78 Fix a typo
* 17de14d Render restaurants the map.
* 00315a1 Initial commit

看到在HEAD指向的commit里,不仅有file1.txt的内容修改,还能看到新增了一个file2.txt的文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
(master)$ git show HEAD
commit 5bbe8a7f4cabf9011ff28c32e97e0c4a79f10f8f (HEAD -> master)
Author: Stone <masaike@qq.com>
Date: Fri Apr 30 14:38:19 2021 +0800

Render cafes on the map

diff --git a/file1.txt b/file1.txt
index cb224a0..c44f4be 100644
--- a/file1.txt
+++ b/file1.txt
@@ -3,3 +3,4 @@ restaurants
fix a typo
color-red
ref-of-sdk
+blue cafes
diff --git a/file2.txt b/file2.txt
new file mode 100644
index 0000000..ce01362
--- /dev/null
+++ b/file2.txt
@@ -0,0 +1 @@
+hello

如果这个file2.txt是不小心加上去的,其实不要,就先回到前一个状态,此时修改在本地目录,但不在staging area(用git status查看是红色的):

1
2
3
4
5
6
7
(master)$ git reset --mixed HEAD~1
Unstaged changes after reset:
M file1.txt

(master)$ git status -s
M file1.txt
?? file2.txt

删去untracked file,再 add 到 staging area,最后commit就行了。此时commit的就只有file1.txt的内容修改,而没有新增file2.txt:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
(master)$ git clean -fd
Removing file2.txt

(master)$ git add .

(master)$ git commit -m "Render cafes on the map."
[master e47642a] Render cafes on the map.
1 file changed, 1 insertion(+)

(master)$ git log --oneline --all --graph
* e47642a (HEAD -> master) Render cafes on the map.
* 5322dae Revert bad code.
* 688a4d0 WIP
* b0201fc Update terms of service and Google Map SDK version.
* db94b15 WIP
* 175d394 Add a reference to Google Map SDK.
* 24f3009 Change the color of restaurant icons.
* 127cf78 Fix a typo
* 17de14d Render restaurants the map.
* 00315a1 Initial commit

(master)$ git show HEAD
commit e47642a4cd59cfec5697d82acbbfa7372547823e (HEAD -> master)
Author: Stone <masaike@qq.com>
Date: Fri Apr 30 14:42:11 2021 +0800

Render cafes on the map.

diff --git a/file1.txt b/file1.txt
index cb224a0..c44f4be 100644
--- a/file1.txt
+++ b/file1.txt
@@ -3,3 +3,4 @@ restaurants
fix a typo
color-red
ref-of-sdk
+blue cafes

Amending an Ealier Commit

需要rebase到想要修改的commit。


REBASING REWRITES HISTORY.

原始的history:

6_5

假如现在想修改commit B,就变成B*了。而history是不可变的,commit C不能以commit B*为parent commit,只能创建一个与C一摸一样的commit C*,然后以commit B*为parent commit,commit D同理。所以,虽然只有B要改,但实际上C和D都要重新创建。

6_6


我们现在想修改commit ID为 175d394 的commit:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
(master)$ git log --oneline --all --graph
* e47642a (HEAD -> master) Render cafes on the map.
* 5322dae Revert bad code.
* 688a4d0 WIP
* b0201fc Update terms of service and Google Map SDK version.
* db94b15 WIP
* 175d394 Add a reference to Google Map SDK.
* 24f3009 Change the color of restaurant icons.
* 127cf78 Fix a typo
* 17de14d Render restaurants the map.
* 00315a1 Initial commit

(master)$ git show 175d394
commit 175d3944118590daf26ba25c46da4061c0ad641b
Author: Stone <1140525895@qq.com>
Date: Fri Apr 30 10:01:56 2021 +0800

Add a reference to Google Map SDK.

diff --git a/file1.txt b/file1.txt
index 1cd7c63..cb224a0 100644
--- a/file1.txt
+++ b/file1.txt
@@ -2,3 +2,4 @@ hello
restaurants
fix a typo
color-red
+ref-of-sdk //----------看这里--------------

首先要rebase到它的上一个commit,即ID为24f3009的commit,在弹出的窗口改pick为edit:

6_4

1
2
3
4
5
6
7
8
9
(master)$ git rebase -i 24f3009
Stopped at 175d394... Add a reference to Google Map SDK.
You can amend the commit now, with

git commit --amend

Once you are satisfied with your changes, run

git rebase --continue

所以会需要你edit完这个commit用—amend修正了再continue。

修改内容是增加一个文件license.txt:

1
2
3
4
5
6
7
8
9
(master|REBASE 1/6)$ echo license > license.txt

(master|REBASE 1/6)$ git add .

(master|REBASE 1/6)$ git commit --amend
[detached HEAD 0eb205c] Add a reference to Google Map SDK.
Date: Fri Apr 30 10:01:56 2021 +0800
2 files changed, 2 insertions(+)
create mode 100644 license.txt

可以看到分了支了:

1
2
3
4
5
6
7
8
9
10
11
12
13
(master|REBASE 1/6)$ git log --oneline --all --graph
* 0eb205c (HEAD) Add a reference to Google Map SDK.
| * e47642a (master) Render cafes on the map.
| * 5322dae Revert bad code.
| * 688a4d0 WIP
| * b0201fc Update terms of service and Google Map SDK version.
| * db94b15 WIP
| * 175d394 Add a reference to Google Map SDK.
|/
* 24f3009 Change the color of restaurant icons.
* 127cf78 Fix a typo
* 17de14d Render restaurants the map.
* 00315a1 Initial commit

继续rebase,把后面的commit调整完成(简单的pick),可以看到从修改的那条commit开始,ID全变了,尽管操作为pick的commit内容未变(其实是重新创建了这些commit):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
(master|REBASE 1/6)$ git rebase --continue
Successfully rebased and updated refs/heads/master.

(master)$ git log --oneline --all --graph
* 0d35c4a (HEAD -> master) Render cafes on the map.
* 6f2b578 Revert bad code.
* fce1354 WIP
* a3f2e62 Update terms of service and Google Map SDK version.
* e67e73d WIP
* 0eb205c Add a reference to Google Map SDK.
* 24f3009 Change the color of restaurant icons.
* 127cf78 Fix a typo
* 17de14d Render restaurants the map.
* 00315a1 Initial commit

检查一下,确实修改成功了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
(master)$ git show 0eb205c
commit 0eb205cde3b4c6d852681d59d2821914f3a37a69
Author: Stone <1140525895@qq.com>
Date: Fri Apr 30 10:01:56 2021 +0800

Add a reference to Google Map SDK.

diff --git a/file1.txt b/file1.txt
index 1cd7c63..cb224a0 100644
--- a/file1.txt
+++ b/file1.txt
@@ -2,3 +2,4 @@ hello
restaurants
fix a typo
color-red
+ref-of-sdk
diff --git a/license.txt b/license.txt //----新增license.txt---
new file mode 100644
index 0000000..8da8489
--- /dev/null
+++ b/license.txt
@@ -0,0 +1 @@
+license

Dropping a Commit

比如我们想删掉 6f2b578,fce1354 和 e67e73d 的commit:

1
2
3
4
5
6
7
8
9
10
11
(master)$ git log --oneline --all --graph
* 0d35c4a (HEAD -> master) Render cafes on the map.
* 6f2b578 Revert bad code. //-------------
* fce1354 WIP //-------------
* a3f2e62 Update terms of service and Google Map SDK version.
* e67e73d WIP //-------------
* 0eb205c Add a reference to Google Map SDK.
* 24f3009 Change the color of restaurant icons.
* 127cf78 Fix a typo
* 17de14d Render restaurants the map.
* 00315a1 Initial commit

rebase到0eb205c,在弹出的窗口直接删掉第1,3,4行就行了:

6_7

1
2
3
4
5
6
7
8
9
(master)$ git rebase -i 0eb205c
error: could not apply a3f2e62... Update terms of service and Google Map SDK version.
Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply a3f2e62... Update terms of service and Google Map SDK version.
Auto-merging file1.txt
CONFLICT (content): Merge conflict in file1.txt

有冲突就解决冲突:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
(master|REBASE 1/2)$ git mergetool
Merging:
file1.txt

Normal merge conflict for 'file1.txt':
{local}: modified file
{remote}: modified file

(master|REBASE 1/2)$ git rebase --continue
[detached HEAD b153f53] Update terms of service and Google Map SDK version.
1 file changed, 1 insertion(+)
error: could not apply 0d35c4a... Render cafes on the map.
Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply 0d35c4a... Render cafes on the map.
Auto-merging file1.txt
CONFLICT (content): Merge conflict in file1.txt

(master|REBASE 2/2)$ git mergetool
Merging:
file1.txt

Normal merge conflict for 'file1.txt':
{local}: modified file
{remote}: modified file

好了:

1
2
3
4
5
6
7
8
9
10
11
12
13
(master|REBASE 2/2)$ git rebase --continue
[detached HEAD 3b55d73] Render cafes on the map.
1 file changed, 1 insertion(+), 1 deletion(-)
Successfully rebased and updated refs/heads/master.

(master)$ git log --oneline --all --graph
* 3b55d73 (HEAD -> master) Render cafes on the map.
* b153f53 Update terms of service and Google Map SDK version.
* 0eb205c Add a reference to Google Map SDK.
* 24f3009 Change the color of restaurant icons.
* 127cf78 Fix a typo
* 17de14d Render restaurants the map.
* 00315a1 Initial commit

Rewording Commit Messages

比如要修改17de14d和b153f53的commit message:

1
2
3
4
5
6
7
8
(master)$ git log --oneline --all --graph
* 3b55d73 (HEAD -> master) Render cafes on the map.
* b153f53 Update terms of service and Google Map SDK version.
* 0eb205c Add a reference to Google Map SDK.
* 24f3009 Change the color of restaurant icons.
* 127cf78 Fix a typo
* 17de14d Render restaurants the map.
* 00315a1 Initial commit

首先rebase到17de14d的parent commit(用^表示),在弹出的窗口将对应的这两条的command由pick改为reword(此处不展示),关闭此窗口。然后又有两次弹窗,直接修改message即可。

1
2
3
4
5
6
7
8
9
(master)$ git rebase -i 17de14d^
[detached HEAD 38b49e0] Render restaurants on the map.
Date: Fri Apr 30 09:59:03 2021 +0800
1 file changed, 1 insertion(+)
[detached HEAD dd32e6e] Add a reference to Google Map SDK v1.0.0.
Date: Fri Apr 30 10:01:56 2021 +0800
2 files changed, 2 insertions(+)
create mode 100644 license.txt
Successfully rebased and updated refs/heads/master.

看看,改好了:

1
2
3
4
5
6
7
8
(master)$ git log --oneline --all --graph
* 526429c (HEAD -> master) Render cafes on the map.
* e3efdfd Update terms of service and Google Map SDK version.
* dd32e6e Add a reference to Google Map SDK v1.0.0. //-----v1.0.0------
* 1ca0331 Change the color of restaurant icons.
* cacde15 Fix a typo
* 38b49e0 Render restaurants on the map. //------on--------
* 00315a1 Initial commit

依然,00315a1之后的全部commit,ID都变了。

Re-ordering Commits

1
2
3
4
5
6
7
8
(master)$ git log --oneline --all --graph
* 2879b9f (HEAD -> master) Render cafes on the map.
* 95cd9e7 Update terms of service and Google Map SDK version.
* 398859b Add a reference to Google Map SDK v1.0.0.
* 1ca0331 Change the color of restaurant icons.
* cacde15 Fix a typo
* 38b49e0 Render restaurants on the map.
* 00315a1 Initial commit

要将398859b调到38b49e0前面去:

git rebase后弹出的窗口将398859b对应行移到最上边(选中该行,Atl+↑ 调整):

6_8

1
2
3
4
5
6
7
8
9
10
11
(master)$ git rebase -i 00315a1
Successfully rebased and updated refs/heads/master.

(master)$ git log --oneline --all --graph
* 3a8e00e (HEAD -> master) Render cafes on the map.
* 7a97000 Update terms of service and Google Map SDK version.
* 031bd2e Change the color of restaurant icons.
* f8bce71 Fix a typo
* 8eb9241 Render restaurants on the map. //---------------
* 2e926d2 Add a reference to Google Map SDK v1.0.0. //---------------
* 00315a1 Initial commit

Squashing Commits

1
2
3
4
5
6
7
8
(master)$ git log --oneline --all --graph
* 3a8e00e (HEAD -> master) Render cafes on the map.
* 7a97000 Update terms of service and Google Map SDK version.
* 031bd2e Change the color of restaurant icons.
* f8bce71 Fix a typo
* 8eb9241 Render restaurants on the map.
* 2e926d2 Add a reference to Google Map SDK v1.0.0.
* 00315a1 Initial commit

希望将8eb9241,f8bce71,031bd2e合并成一条commit。

Method 1: squash

git rebase之后弹出的窗口改pick为squash表示该条commit与前一条commit合并:

6_9

关闭该窗口,之后弹出的窗口,删除f8bce71,031bd2e的commit message,只留下8eb9241的commit message。

1
2
3
4
5
(master)$ git rebase -i 2e926d2
[detached HEAD dffab6e] Render restaurants on the map.
Date: Fri Apr 30 09:59:03 2021 +0800
1 file changed, 3 insertions(+)
Successfully rebased and updated refs/heads/master.
1
2
3
4
5
6
(master)$ git log --oneline --all --graph
* 42732b9 (HEAD -> master) Render cafes on the map.
* 56eb52d Update terms of service and Google Map SDK version.
* dffab6e Render restaurants on the map.
* 2e926d2 Add a reference to Google Map SDK v1.0.0.
* 00315a1 Initial commit

Nice.

Method 2: fixup

git rebase -i 弹出窗口,用fixup,就不需要手动删改commit message(Git不会pick用了fixup的那一条的message)。而是直接就用8eb9241的commit message:

6_10

log同上。

Splitting a Commit

将549d310拆成两个:

1
2
3
4
5
6
(master)$ git log --oneline --all --graph
* de27a35 (HEAD -> master) Render cafes on the map.
* 549d310 Update terms of service and Google Map SDK version.
* 8014c85 Render restaurants on the map.
* 2e926d2 Add a reference to Google Map SDK v1.0.0.
* 00315a1 Initial commit

pick改为edit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
(master)$ git rebase -i 549d310^
Stopped at 549d310... Update terms of service and Google Map SDK version.
You can amend the commit now, with

git commit --amend

Once you are satisfied with your changes, run

git rebase --continue

(master|REBASE 1/2)$ git log --oneline --all --graph
* de27a35 (master) Render cafes on the map.
* 549d310 (HEAD) Update terms of service and Google Map SDK version.
* 8014c85 Render restaurants on the map.
* 2e926d2 Add a reference to Google Map SDK v1.0.0.
* 00315a1 Initial commit

(master|REBASE 1/2)$ git reset HEAD^ //---default:mixed-------

(master|REBASE 1/2)$ git status -s //------都是红色的状态-------
M file1.txt
?? sdkversion.txt

分两次提交两个不同的修改:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
(master|REBASE 1/2)$ git add file1.txt

(master|REBASE 1/2)$ git commit -m "Add terms of service."
[detached HEAD 1d17606] Add terms of service.
1 file changed, 4 insertions(+)

(master|REBASE 1/2)$ git add .

(master|REBASE 1/2)$ git commit -m "Update Google Map SDK version."
[detached HEAD 55d127c] Update Google Map SDK version.
1 file changed, 1 insertion(+)
create mode 100644 sdkversion.txt

(master|REBASE 1/2)$ git log --oneline --all --graph
* ed8737e (HEAD) Update Google Map SDK version.
* 44220a0 Add terms of service.
| * de27a35 (master) Render cafes on the map.
| * 549d310 Update terms of service and Google Map SDK version.
|/
* 8014c85 Render restaurants on the map.
* 2e926d2 Add a reference to Google Map SDK v1.0.0.
* 00315a1 Initial commit

(master|REBASE 1/2)$ git rebase --continue
Successfully rebased and updated refs/heads/master.

整挺好:

1
2
3
4
5
6
7
(master)$ git log --oneline --all --graph
* a6ea6fd (HEAD -> master) Render cafes on the map.
* ed8737e Update Google Map SDK version. //---------------
* 44220a0 Add terms of service. //---------------
* 8014c85 Render restaurants on the map.
* 2e926d2 Add a reference to Google Map SDK v1.0.0.
* 00315a1 Initial commit