24

How to add the new changes to the commits which are in the middle, I mean not adding to the top commit.

git log
commit1 <--- HEAD
commit2
commit3

How to add the changes directly to commit3, without removing commit1 and commit2 and then adding changes to the commit3.

Do I need to use stash?

If possible, please provide the link.

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
viru
  • 241
  • 1
  • 2
  • 3
  • See also http://stackoverflow.com/questions/179123/how-do-i-edit-an-incorrect-commit-message-in-git – Josh Lee Aug 04 '11 at 14:25

4 Answers4

30

You can do this with git-rebase:

git rebase -i commit3^

In the file that pops up, change pick commit3 to edit commit3. Save and quit, then make your changes. Commit them as you would normally, then use git rebase --continue. When you’re done, you’ll see your new changes between commit3 and commit2.

(This will work the same as Sjoerd’s answer; it’s just a different way of thinking about it. If your changes conflict with commit2, you’ll have to handle that either way.)

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
  • You shouldn't do `git commit --amend`; the `git rebase --continue` is enough after adding the changes (unless you want to edit the message as well). – siride Aug 04 '11 at 14:24
  • 2
    This method doesn't work if the changes are already sitting in the work tree. Of course, he could stash the changes, do the rebase and then unstash when editing commit3 instead of doing a squash as in my answer. – siride Aug 04 '11 at 14:29
  • The article to guide step by step how to edit a specific commit in the middle of history by `git rebase -i ` https://confluence.atlassian.com/stashkb/how-do-you-make-changes-on-a-specific-commit-747831891.html Hope it will be helpful – Charlie VO Apr 04 '22 at 04:22
23

You have to do an interactive rebase. Read the help page for git rebase for the details. The short answer is that you'll go ahead and commit your "middle" changes as usual, then run git rebase -i HEAD~4. It'll bring up a list of the last 3 commits in a text editor. Simply reorder the commits where the newest commit is put in the middle, and then save and exit the editor. Git will then attempt to rebuild the history in the new order. It might stop with conflicts. If so, fix them like you would for a merge conflict and then run git rebase --continue after they are all fixed and added. It tells you all this when you have a conflict, so just read the error messages and you should be fine.

EDIT: actually, it looks like you want to edit an existing commit. In that case, when the editor comes up, move your new temporary commit to be next to commit3 and then change the command to "squash" from "pick":

pick 123456 commit3
squash 541343 tmpcommit
pick 654321 commit2
pick 431523 commit1

EDIT2: if the branch and commit1, commit2 and commit3 are already public, then you shouldn't be rebasing. Then again, you shouldn't be modifying commits anyway, so the whole question would be moot. I'm assuming that this is a private branch or one that is expected by others to be rebased and rewound.

siride
  • 200,666
  • 4
  • 41
  • 62
4

Create a new branch, which is a copy of commit3. Add your changes and commit. Then, merge commit2 and commit1 to that branch.

Sjoerd
  • 74,049
  • 16
  • 131
  • 175
1

You could use git reset --soft HEAD [commit3-hash]. this command remove the commit1 and commit2 from the history but its changes is still in there.