Git - How to edit old (not previous) commit with some of the unstaged changes from current index (current state)?

前端 未结 4 1224
[愿得一人]
[愿得一人] 2020-12-17 01:19

I\'ve taken a look at these previous questions already:

  • Howto add a changed file to an older (not last) commit in Git
  • How do I edit a previous git comm
4条回答
  •  伪装坚强ぢ
    2020-12-17 01:51

    If I understand what you want to do, it's not much different than the answer on your first link, you just need to stash the changes you don't want to add to the older commit. You can do it like this:

    1. Commit the changes you want added to your penultimate commit. Just give it a simple commit message like "add this," as it won't be around for long.
    2. Stash the remaining changes that you don't want added to the commit.
    3. Look up the parent of the commit you're adding to. For example, if the commit you want to modify has an SHA1 of aaaaaaa and its parent is bbbbbbb, you want bbbbbbb.
    4. Do git rebase -i bbbbbbb (substituting the correct commit as determined in step 3). Move the most recent commit ("add this") up to just below the commit you're modifying and change it from pick to fixup. This will add it to that commit without change that commit's message.
    5. Unstash the changes from step 1, and carry one from there.

提交回复
热议问题