Sandeep
Sandeep

Reputation: 19452

git: How to refresh a commit that isn't on top

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?

Upvotes: 1

Views: 1448

Answers (1)

dan
dan

Reputation: 13262

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.

Upvotes: 1

Related Questions