如何修改之前commit的内容?

问题描述 投票:0回答:1

我正在修改先前提交“commit2”的内容,步骤如下:

    #1 checkout the commit we want to modify
    git checkout ad4fe51
    
    #2 amend the commit with new changes
    git add . && git commit --amend --no-edit
    
    #3 remove files from folder
    git rm -rf .
    
    #4 return
    git checkout -

    #5 rebase
    git rebase --onto @{-1} ad4fe51

但是,在第 5 步,它会推回提交中修改的内容 (#2) 如果我调用 Push origin,commit2 会正确包含所有修改,但是,

“commit7”将包含

{ commit7.txt, newfile1.txt, newfile2.txt }

如何避免这种情况? 我按照以下步骤录制 gif:https://i.imgur.com/qc9fVeA.gif

git git-commit
1个回答
0
投票

我认为如果您从git rebase开始

,您的生活会更轻松:

  1. 标识您要修改的提交。

  2. 运行:

    git rebase -i COMMIT^
    例如,如果您想编辑提交

    ad4fe51

    ,请运行:

    git rebase -i ad4fe51^
    
    
  3. 在选择列表中,将该提交的

    pick

     更改为 
    edit
     并保存列表。

  4. 进行所需的更改并

    git add

    文件。

  5. 运行

    git rebase --continue

  6. 如有必要,更新您的提交消息并保存。

你已经完成了!

© www.soinside.com 2019 - 2024. All rights reserved.