Very similar to the approach mentioned in drop-commit.
Say this time you want to edit a particular commit while working on a git branch.
gitGraph
commit id: "update deps"
commit id: "docs v1"
branch feature
checkout feature
commit id: "add feature"
commit id: "bug fix"
commit id: "wrong commit"
commit id: "enhancement"
commit id: "docs v2"
Now say you realize commit id wrong commit needs some editing, we can do it in the following manner usng interactive rebase:
git rebase -i <wrong-commit-hash>^Running this command should open an editor and you’ll have the following todo list:
pick 940b012 # bug fix
pick fa61a6e # wrong commit
pick e588d5e # enhancement
pick b1cd783 # docs v2Just edit this document and replace pick with edit for the commit id wrong commit. Save it and close it.
pick 940b012 # bug fix
edit fa61a6e # wrong commit
pick e588d5e # enhancement
pick b1cd783 # docs v2This will checkout the codebase on the commit bug fix with changes from wrong commit, rebase will be paused.
Now you make changes to edit this commmit, add the related files. Then continue the rebase:
git rebase --continueGit will ask you for commit message as well, if it needs editing, else it will go with the older commit message.
And now you’ll see the edited commit in the branch history and git will rewrite the history with new commit hash.