簡述
git提交歷史中有一次提交的內容是有問題,因為每隔一段時間就要發一次版本,所以必須修改這次提交的內容,以便其不影響已經發布的版本。
大概是這樣子的
A --- B ---- C ---- D ---- E ----- F ------
| \ \
有問題 \-----發布 \---- 發布
所以這里需要修改C這次提交的內容。
解決過程
相關的操作可以參考7.6 Git 工具 - 重寫歷史
這里我創建了一個新的倉庫,用來描述解決這個問題的過程。
1、先看一下提交記錄
$ git log
commit aa3f6b723abf030b1692f9b573092ec782600d91
Author: solym <solym@sohu.com>
Date: Sat Sep 29 14:34:36 2018 +0800
第三次提交
commit e186c75c5431a6eb683d4640ac30c4b8900ba0c1
Author: solym <solym@sohu.com>
Date: Sat Sep 29 14:34:11 2018 +0800
第二次提交
commit ebcd3120d30c52125593601f296607c5dcc520a3
Author: solym <solym@sohu.com>
Date: Sat Sep 29 14:33:48 2018 +0800
第一次提交
這里假設是第二次提交的內容有問題,所以需要會到e186c75c5431a6eb683d4640ac30c4b8900ba0c1這個提交記錄之前一次提交的位置來解決這個問題。
2、先將當前的修改用stash存儲一下,后面解決完之后再釋放出來
$ git stash
Saved working directory and index state WIP on master: aa3f6b7 第三次提交
HEAD is now at aa3f6b7 第三次提交
3、將 HEAD 通過rebase回退到有問題的位置前
git rebase e186c75c5431a6eb683d4640ac30c4b8900ba0c1^ --interactive
warning: stopped at e186c75... 第二次提交
You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run
git rebase --continue
4、在出來的編輯界面,將有問題的提交前的pick改為edit,然后保存退出

# 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 類似於squash,但是丟棄此提交的日志消息
# x, exec = run command (the rest of the line) using shell 運行shell命令
# d,drop = remove commit 刪除提交
5、修改有問題的文件,解決后重新提交。注意提交使用的參數是--amend
$ vim A.cpp
$ git add A.cpp
$ git commit --amend
[detached HEAD 8b4daa5] 第二次提交
Date: Sat Sep 29 14:34:11 2018 +0800
2 files changed, 8 insertions(+), 1 deletion(-)
create mode 100644 B.cpp
6、使用git rebase --continue逐步前進到最新的提交位置。
$ git rebase --continue
error: could not apply aa3f6b7... 第三次提交
When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".
Could not apply aa3f6b7... 第三次提交
Auto-merging A.cpp
CONFLICT (content): Merge conflict in A.cpp
上面執行后因為有兩處都有修改,需要解決沖突。
$ git status
interactive rebase in progress; onto ebcd312
Last commands done (2 commands done):
edit e186c75 第二次提交
pick aa3f6b7 第三次提交
No commands remaining.
You are currently rebasing branch 'master' on 'ebcd312'.
(fix conflicts and then run "git rebase --continue")
(use "git rebase --skip" to skip this patch)
(use "git rebase --abort" to check out the original branch)
Unmerged paths:
(use "git reset HEAD <file>..." to unstage)
(use "git add <file>..." to mark resolution)
both modified: A.cpp
no changes added to commit (use "git add" and/or "git commit -a")
修改后再次提交即可
$ vim A.cpp
$ git add A.cpp
$ git commit -a
[detached HEAD 8070ac2] 第三次提交
1 file changed, 6 insertions(+), 1 deletion(-)
然后重新執行
$ git rebase --continue
Successfully rebased and updated refs/heads/master.
如果還有沖突,則重復執行上面兩步驟。
7、最后將stash存儲的內容釋放出來,繼續工作
$ git stash pop
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: B.cpp
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (fc32de3118386c30047df86670371c8ab049e0e0)
