I have two commits on the same branch, one right after another. I added changes to file A to the first commit, and then I made some changes to other files and then made another commit. Now I want the changes to file A to be on the second commit rather than the first. What's the most elegant way?
You can modify the most recent commit in the same branch by running git commit --amend. This command is convenient for adding new or updated files to the previous commit. It is also a simple way to edit or add comments to the previous commit. Use git commit --amend to modify the most recent commit.
What you can do is commit your current staged changes. Then stash the changes you don't want to commit using git stash and then revert the last commit using git reset HEAD~ . At this point you can make the modifications and recommit the changes (using git add . && git commit ).
I wrote a script to meet this purpose. You can checkout out it here.
Using the script it would be as simple as:
mv-changes HEAD~ HEAD fileA
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With