5

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?

Submit 1 enter image description here

Submit 2 enter image description here

Submit 3 enter image description here

Do Revert enter image description here

Files Merged enter image description here

Child of Merge enter image description here

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
HelloCW
  • 843
  • 22
  • 125
  • 310

2 Answers2

6

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".

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
2

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".

Geno Chen
  • 4,916
  • 6
  • 21
  • 39
  • Thanks! Do you mean that I should to launch rever command in the point Submit 3 in order to return the point Submit 2 ? – HelloCW Mar 06 '19 at 07:30
  • @HelloCW As of my test, yes, and select "Accpet Theirs" when having a merge conflict. But some of the internals are out of my knowledge, still needs more insight answer. – Geno Chen Mar 06 '19 at 07:52
  • Thanks! "In order to return the point Submit 2, I must launch rever command in the point Submit 3 ", is it a bug of Android Studio? – HelloCW Mar 08 '19 at 08:42
  • @HelloCW I think no, there maybe some word game there, if we recognize "revert" as "cancel that commit", for example "revert S2" means "cancel all operations that constructs S2", then "revert S2" will make S2 "didn't happened", or we say "destroy" the code that builds up S2, that means the repository will be look like the previous commit of S2, that is S1. For the same reason, we may understand why return to S2 should do revert on S3. – Geno Chen Mar 08 '19 at 08:48
  • Another thought is, if we "revert" the last (newest) commit, then the repository will looks like the second last commit, not the last commit (otherwise "revert last commit" will have nothing to do). – Geno Chen Mar 08 '19 at 08:49