[Solved] How to edit an existing commit in git

Spagliari Asks: How to edit an existing commit in git
I want to edit an old commit in git. How do I undo just a part of an old commit? (I am a bit of a git rookie, so sorry if I am confused about terms etc).

Example: in a commit, say three commits back, I deleted some code that should not have been deleted (yet). Now I want to go back and undo that particular change of that commit. It’s a local repo.

What I have tried is what google gives me: stash + rebase -i + amend, but these invariably end up in a mess that takes (me) hours to resolve. None of them lets me actually edit the commit, only to do new changes – which in case of deleted code means me to locate and re-enter the deleted lines again, by hand (cut and paste). Then, even if the rebase is successful, the stash pop always fails… with a merge mess on my hands..). There just must be an easier way!

When I do a git add -p, it allows me edit the commit, line-by-line, and confirm what goes in. Now I want to redo that process, but on a commit. A git unadd -p of sorts, allowing me to delete the lines in the .patch with ‘-‘ in front.

Sorry, if my frustration with this comes through.

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.