1

In my working branch, I have three dependent patches. All of them not merged.

<commit id -1>
<Commit id -2>
<Commit id -3>

How can i add new changes to <Commit id -2>?

Currently here is how I am doing it.

git stash
git reset --hard HEAD~1
git stash apply
git add .
git commit --amend
git cherry-pick <commit id -1>

This is working without any problem. But is there a better way where I can commit the staged files to whichever commit I want?

Martin G
  • 17,357
  • 9
  • 82
  • 98
Sandeep
  • 18,356
  • 16
  • 68
  • 108
  • Not an answer, but I use [stgit](http://www.procode.org/stgit/) for this sort of thing. It deals with patch stacks (similarly to quilt) and allows you to refresh a patch that isn't on top. It will also happily "uncommit" git commits into stgit patches – Hasturkun Oct 17 '14 at 07:44
  • possible duplicate of [How to modify a specified commit?](http://stackoverflow.com/questions/1186535/how-to-modify-a-specified-commit) – Andrew C Oct 20 '14 at 04:53

1 Answers1

1

You can use the interactive rebase and edit the desired commit. Something like:

git rebase --interactive abc4321d^

In the editor change pick to edit or e. Apply the changes (git stash pop) and then commit them.

This way you can modify multiple old commits in the same run. To continue the rebase, and address the next commit, you will use:

git rebase --continue

At any time, if you sense that you did something wrong, you can cancel the rebase using:

git rebase --abort

Please note that rewriting the public history (a history on a repo shared with others) is a bad habit. You should rewrite the previous commits only if they are not yet published.

dan
  • 13,132
  • 3
  • 38
  • 49
  • This may not be relevent to this question , I always had this doubt regarding rebase. In intreactive rebase(or while doing rebase in general ) , are there cases where I have to do rebase --continue multiple times? – Ajay Oct 17 '14 at 10:25
  • @Ajay Yes, if the rebase is stopped by an edit/reword or a merge conflict. – dan Oct 17 '14 at 15:31