如何修改之前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

观察:我避免使用 --interative 来跳过编辑器,因为我正在自动化此过程。

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. 如有必要,更新您的提交消息并保存。

你已经完成了!


如果我将以下脚本放入

$HOME/bin/git-edit

(或我的
$PATH
中的其他位置)...

#!/bin/sh CID=$(git rev-parse "$1") GIT_SEQUENCE_EDITOR="sed -i '1 s/pick/edit/'" \ git rebase -i "${CID}^"
...然后我可以运行 

git edit COMMIT

 并立即开始进行更改(这会处理上面列表中的步骤 2 和 3)。

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