1

I use GitHub in my project in Android Studio 3.3.1.

I write code in local master branch and push them to remote origin/master.

The Submit_3 Image is my latest work and submit result snapshot.

I find the code after Submit_1 Image snapshot are wrong, so I hope to return Submit_1 and write new code. I hope that I can do the new work still on master branch, how can I do?

BTW, at present I use Checkout Revision command to return to Submit_1 (see Current Image), and create a temp branch for Submit_1, then switch to master branch and merge temp branch. But I find the result just like Result Image is the same Submit_3 Image, why?

Submit_3 Image enter image description here

Submit_1 Image enter image description here

Current Image enter image description here

Result Image enter image description here

HelloCW
  • 843
  • 22
  • 125
  • 310

2 Answers2

1

The reason you have the same result after you merge temp is because you essentially merge what is in master (which includes the wrong thing) and the one in temp; thus, having both the wrong and correct code.

Solution to your initial problem:

  1. git checkout master => Goes to master branch
  2. git log -n [some number] => Shows you the list of commit messages; eg. git log -n 5 will show you last 5 commit messages.
  3. Find the commit number of the commit you want to return to. Copy it.
  4. git reset --hard [commit number here] => This will return master to be where you chose in your commit number. Be careful, this will erase everything that came after that commit.
  5. I don't recall if you need to git push or it does it for you. Check by doing git status
  6. Continue writing your code. If you have new stuff in temp, at this point, if you do git merge temp. It will get your new stuff and not have the wrong code that was in master before. If anything goes wrong, do what I described above to return to any commit.

Good luck!

Also, official doc here: https://git-scm.com/docs/git-reset

spinyBabbler
  • 392
  • 5
  • 19
  • Thanks! Do you mean that the `Checkout Revision` command in Android Studio can't do that? – HelloCW Mar 07 '19 at 23:36
  • I'm not familiar with Android Studio interface but if you go to the file directory where your git project is and do what I said, it should fix your problem. Also from the name, it sounds like checkout revision will merely go to that particular commit. Which is doing git checkout [commit number] in terminal terms. If there is any interface that says git reset hard, you can reset to where you want to be. Check this out too, if it helps: https://stackoverflow.com/questions/29616173/how-to-reset-to-my-last-commit-in-android-studio; except instead of HEAD, it would be the commit number. – spinyBabbler Mar 07 '19 at 23:48
  • @HelloCW I just noticed that in your "Current image", you need to "Reset Current Branch to here"; be careful with which commit you do it on. After that commit, everything goes away. Basically does what I told you above I'm sure. – spinyBabbler Mar 08 '19 at 21:02
1

I am not sure I understand your problem but I think what you want is Reset Current Branch to Here.... This will delete Submit 2 and Submit 3 and go back in time to Submit 1. You will have in your tree:

Submit 1           (master)
Initial Commit

Then, to make this real in GitHub you need to Push Force when you push your changes. And the final tree will be:

Submit 1           (origin & master)
Initial Commit

PLEASE do this carefully. After you push force, Submit 2 and Submit 3 will be permanently deleted.

josemigallas
  • 3,761
  • 1
  • 29
  • 68
  • 1
    What I understand from OP is they want to override or fix their GitHub history. Git reset brings you back to an older commit in your history, push force sends this change to your upstream. They are different actions. – josemigallas Mar 11 '19 at 10:13
  • 1
    Oops you're right, I have removed the comment so as not to confuse anyone. – spinyBabbler Mar 11 '19 at 15:35