1. 程式人生 > >如何修改一個過去的Commit

如何修改一個過去的Commit

假設我的git log 如下:

commit 5511533dda6fee6982175fafca1f4bd5692e3d9c (HEAD -> trans, origin/trans)
Author: 暮晨 <[email protected]>
Date:   Tue Nov 13 23:57:05 2018 +0800

    EX.is is not what it is

commit eff078c67243a71a5ef645ddadfdaef2f374eff1
Author: 暮晨 <[email protected]>
Date:   Sat Nov 10 23:22:19 2018 +0800

    EX.Time for some hash brownies

commit eb69bff965ee57c5047afc4f615c10462c42566a
Author: 暮晨 <
[email protected]
> Date: Sat Nov 10 17:41:55 2018 +0800 EX.Strings can be tricky sometimes

我現在需要回到第一個commit eb69bff96 對檔案進行修改。

那麼需要進行如下操作:

  1. 將當前分支無關的工作狀態進行暫存

     git stash
  2. 將HEAD移動到需要修改的commit上

     git rebase [902341c9]^ --interactive
  3. 開始修改檔案內容

  4. 將改動檔案新增到殘存

     git add
  5. 追加改動到提交

     git commit --amend
  6. 移動HEAD 回到最新的commit

     git rebase --continue
  7. 恢復之前的工作狀態

     git stash pop

缺點

被修改分支後的所有commit都會被重新提交一遍,此時master分支merge這個分支的話會出現commit重複的問題。