Reputation: 20127
In my project, I had two commits; A, and then B, both of which were pushed to the remote.
However I realised that my changes in commit B were a bad idea and reverted back to commit A using git checkout A
.
I then made some changes to commit A, making commit C, which is what I want to then push to my remote as the latest commit. However when I do this, it only pushes the differences between A and C onto the current version of the remote, which is B; in other words, it applies commit C to commit B.
How can I get my local copy onto the remote as a new commit, so that the commit history looks like: A, B, C (i.e. without reverting B - I still want B in my commit history)
Upvotes: 0
Views: 28
Reputation: 1689
You could create a patch with differences between A and C and apply it on top of B to create a brand new commit, like this:
git diff A C > patch.git
git checkout B
...
At this point make sure that your working directory is clean.
git apply patch.git
...
The command above will change only the working directory. So, you need to add the files by yourself.
git add ...
git commit
Upvotes: 1
Reputation: 24060
You can use an interactive rebase which allows you to shuffle around the order of commits. By running git rebase -i HEAD^^^
you can go back the last three commits; you'll be given a file with commits by hash. Simply edit the file by moving lines up/down to arrange them in the desired order, then save the file and git will apply those changes in that order for you.
Upvotes: 0
Reputation: 179552
If you don't want to rewrite history, you should simply commit a revert for commit B:
git revert B
git commit
Upvotes: 1