如何修改一个过去的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重复的问题。

扫描二维码关注公众号,回复: 4062226 查看本文章

猜你喜欢

转载自www.cnblogs.com/leisurelylicht/p/ru-he-xiu-gai-yi-ge-guo-qu-deCommit.html