上一節是介紹把幾個 commit 合在一起,這一次要分享的是把一個 commit 拆成多個 commit 的狀況。
這個操作會比較複雜一點,原因在於他不是只有寫寫訊息那麼簡單而已。
需要實際改動這個 commit 操作的行為。我們需要回到要拆解的 commit 先把這個 commit 修改的檔案放回工作目錄,
接著在依照我們想要的方式分別做出新的 commit 這樣。
所以我們得先知道這個 commit 做了什麼,然後才知道該怎麼拆解 commit.
我先做了一個 commit 出來,裡面更改了一個檔案,又新增了另一個檔案,現在我們想把他拆開。
# 先來查看這個 commit 做了什麼事
$ git log -p 2a08291
c9175348e5ad240 (HEAD -> master)
Author: Logo Kuo <logo@forblind.org.tw>
Date: Sat Apr 4 19:51:54 2020 +0800
忘記做了什麼事
diff --git a/log.txt b/log.txt
index 440e1f8..7403fbd 100644
--- a/log.txt
+++ b/log.txt
@@ -27,3 +27,4 @@ index 0000000..65c6e5c
@@ -0,0 +1,2 @@
+你好
+我很好
+用不太習慣 windows 的 vim
diff --git a/test.py b/test.py
index 10ae50f..626aa3a 100644
--- a/test.py
+++ b/test.py
@@ -1,35 +1,73 @@
-class Bank:
- '''這是一個銀行類別,裡面有存款金額與存錢、提錢等功能'''
以下省略
簡單說,就是有兩個檔案有增刪,加號是增加的,減號是減掉的。
有興趣可以開一下原檔來比對一下。
好吧,那我們不旦覺得註解寫得很爛,也想把這兩個修改檔案事件拆開成兩個 commit.
# 看一下 log
$ git log --oneline -2
2a08291 (HEAD -> master) 忘記做了什麼事
316ffbc 在 hello.txt 加入第3行
# 把 rebase 設定在要拆解的 commit 的前一個 commit
$ git rebase -i 316ffbc
pick 2a08291 忘記做了什麼事
# 把 pick 改成 edit 並存檔離開
edit 2a08291 忘記做了什麼事
這時候 rebase 看到 edit 時就會停下來,我們看一下狀態
$ git status
interactive rebase in progress; onto 316ffbc
Last command done (1 command done):
edit 2a08291 忘記做了什麼事MINGW64 /d/practice (master)
No commands remaining.
You are currently editing a commit while rebasing branch 'master' on '316ffbc'.
(use "git commit --amend" to amend the current commit)
(use "git rebase --continue" once you are satisfied with your changes)
Administrator@DESKTOP-PI1KHDL MINGW64 /d/practice (master)
nothing to commit, working tree clean
hint: Waiting for your editor to close the file...
簡單說,就是我們還處在 rebase 這個狀態當中,接下來我們運用之前學過的 reset 先回到這個版本的前一個版本
$ git reset HEAD~
Unstaged changes after reset:
M log.txt
M test.py
因為這樣做以後,git 會把 edit 所在的 commit 做的事情丟回工作目錄,
然後依照我們自己的需求 add 檔案再提交,所以我們這樣做。
# 一樣看一下狀態就知道自己接下來該怎麼做
$ git status
interactive rebase in progress; onto 316ffbc
Last command done (1 command done):
edit 2a08291 忘記做了什麼事
No commands remaining.
You are currently splitting a commit while rebasing branch 'master' on '316ffbc'
.
(Once your working directory is clean, run "git rebase --continue")
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: log.txt
modified: test.py
no changes added to commit (use "git add" and/or "git commit -a")
前面告訴我們一樣處在 rebase 狀態,後面的資訊就很熟悉了,要我們把工作目錄的狀況寫到暫存區,
這時候當然不能 git add .
因為這樣就跟原本的 commit 沒兩樣囉。
所以我們加入一個檔案就 commit 一下,這樣就可以分別做出兩個 commit 了:
# 加入 lot.txt 到暫存區
$ git add log.txt
# 先做一次提交
$ git commit -m '在 log.txt 加了第三行'
[detached HEAD 6dd175d] 在 log.txt 加了第三行
1 file changed, 1 insertion(+)
# 再觀察一下狀態
$ git status
interactive rebase in progress; onto 316ffbc
Last command done (1 command done):
edit 2a08291 忘記做了什麼事
No commands remaining.
You are currently splitting a commit while rebasing branch 'master' on '316ffbc'
.
(Once your working directory is clean, run "git rebase --continue")
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: test.py
no changes added to commit (use "git add" and/or "git commit -a")
# 再次將工作目錄的結果存到暫存區,這次可以用小數點偷懶了
$ git add .
# 將結果提交吧
$ git commit -m '複製一個 test.py 蓋掉原本的同名檔案'
[detached HEAD 3d914e7] 複製一個 test.py 蓋掉原本的同名檔案
1 file changed, 73 insertions(+), 35 deletions(-)
rewrite test.py (99%)
事情還沒完喔,再看一下狀態:
$ git status
interactive rebase in progress; onto 316ffbc
Last command done (1 command done):
edit 2a08291 忘記做了什麼事
No commands remaining.
You are currently editing a commit while rebasing branch 'master' on '316ffbc'.
(use "git commit --amend" to amend the current commit)
(use "git rebase --continue" once you are satisfied with your changes)
nothing to commit, working tree clean
別忘了,我們還在 rebase 裡面呀,現在處理好了,就讓 rebase 繼續跑完就好:
# 讓 rebase 做完後面他該做的事情
$ git rebase --continue
Successfully rebased and updated refs/heads/master.
# 成功了,再次確認狀態
$ git status
On branch master
nothing to commit, working tree clean
# 已經結束離開 rebase 了且一切正常,那看一下 log 紀錄
$ git log --oneline -3
3d914e7 (HEAD -> master) 複製一個 test.py 蓋掉原本的同名檔案
6dd175d 在 log.txt 加了第三行
316ffbc 在 hello.txt 加入第3行
如果要更仔細點就分別再來看一下這兩個 commit 的提交內容來確定是否正確。
做到這裡大家有沒有覺得我很龜毛,一直看狀態看狀態的,我想表達的其實只有一個觀念,
其實狀態裡的訊息很清楚,也有告訴我們可以怎麼做或該怎麼做,講誇張點,只要狀態訊息看得懂再加上查清楚他提到的指令,
再處理 git 根本不需要去死背什麼步驟,他會帶領我們前進,學 git 要這樣才會玩得活,
不是被這些流程跟指令參數綁死在那,而是根據這些提示來依據我們實際的狀況完成操作就好
※最後更新時間:2020-09-14 15:37:41 From:211.23.21.202 By:特種兵