Reputation: 2255
I commit and push my code three times, they are Submit 1, Submit 2 and Submit 3.
I hope to rollback my code to the point Submit 2 and push them to remote repository.
So I do Revert operation on the point Submit 2 (See the image Do Revert), a Files Merged with Conflicts dialog box display (See the image Files Merged), and child dialog box of Merge display just like the image Child of Merge.
A: I can't understand why the following code is displayed when I do Revert operation? It seem that the system ready all history commit records to let me merge.
Code
<<<<<<< HEAD
//Submit 3
=======
//Submit 1
>>>>>>> parent of 86821fc... Submit 2
B: And more, I can't get the correct result (rollback my code to the point Submit 2) no matter I choice Accept Yours, Accept Theirs or Merge... command, why?
C: What will the system do if I launch Merge... operation?
Upvotes: 5
Views: 1177
Reputation: 21938
1) Why did this happen?
You've confused revert
and reset
here.
Resetting gets the current branch to a previous point in commit history.
Reverting a specific commit creates a new one, containing the exact reverse of the one you picked. This does not modify the old commit nor gets you back to that past point.
This is also why you got a merge that you didn't expect.
2) How to fix the situation at hand?
Now try resetting to the point before all this mess and it should be all right again.
First of all, if the merge is still ongoing, abort it
git merge --abort
Then check your log
and spot the commit you initially wanted to "revert to" (and I suggest changing your colloquial use of the term here, like you saw it hurts) and reset it :
git reset --hard <commitHash>
(Since you've (I guess?) not pushed yet to the remote, no harm is to be expected. If you in fact have pushed anything during the process, comment and I'll edit my answer to address it.)
3) How to avoid the same problem in the future?
Look at your screenshot named "Do revert", this is where you should have chosen just above "Reset current branch to here" rather than "Revert".
Upvotes: 6
Reputation: 5214
Realized that this seems a question next to What are different about Revert command among of popup menu, git menu and Show History in Android Studio 3.3?, I am not sure if I had something wrong with my answer there.
A: I can't understand why the following code is displayed when I do Revert operation? It seem that the system ready all history commit records to let me merge.
This is called a Merge Conflict. It happens when two commits are going to merge but they have found editing the same place with different idea, which makes Git unable to decide which one to keep. Then Git keeps those both, shown with the format:
<<<<<<< Branch1
Content A
=======
Content B
>>>>>>> Branch2
And let you manually decide which one to keep.
B: And more, I can't get the correct result (rollback my code to the point Submit 2) no matter I choice Accept Yours, Accept Theirs or Merge... command, why?
Then I found my answer there is wrong. I am going to modify it later.
When you want to revert the repository to the time when you made Submit 2, you should select the child commit of Submit 2, which is Submit 3 in your case, then use "Theirs" to merge.
Why Submit 3? That is because, the Revert
"Produce a new commit, which reverts the changes made in the original commit", then when you revert Submit 3, it will clear what you have done after (and include) Submit 3, which is just what the repository looks like when you made Submit 2.
Why "Theirs"? Here, if you select Submit 2, then the commit is at Submit 2, "Our changes" is Submit 3 (what we have done after Submit 2), and "Their changes" is Submit 1 (why? we want to revert (cancel) what we have done in Submit 2, then the repository is becoming what it looks like before Submit 2, that is Submit 1).
Why conflict? Submit 3 and Submit 1 have changed the same line with different idea, then a conflict occurs.
...Wait, at the time I am testing, I think I have found a bug of Local Changes
or Changelist
in IntelliJ IDEA... Yes, this have been reported many times, with different reproduction method, for example IDEA-124412, IDEA-67036 and IDEA-20326, that a file modified twice with different method has shown in the changelist thought the second modification have reverted the first modification.
C: What will the system do if I launch Merge... operation?
When you click Merge...
, then you can see the window shown in the last picture of your question. This window allows you to do an interactive merging. There are "X" and ">>" or "<<" icon shown near the line numbers that have conflict in the both sides of window. Click "X" means you reject that piece of code in that side, click ">>" or "<<" means you accept that piece of code in that side. After that, you can still edit the middle "Result".
Upvotes: 2