-1

Hello I am new in GitHub. And I have done the following process

1)first commit 2)I make lot of changes in code 3)second commit

Now I want to undo the second commit because mistakenly I have done different unwanted changes after the second commit. Can anyone tell me how can I go before the second commit. Basically I want to restore changes which I have made in step 2.

1 Answers1

1

You have different ways of do that. It depends on your case what is best for you.

Doing this, it will add a new commit on the commit history (you can see that using git log. If you run that command, based on what you described, it would be something like

commit <hash> (HEAD -> master, origin/master, origin/HEAD)
Author: Some Name <some-email@email.com>
Date:   some date

    Some message you wrote
    
commit <hash>
Author: Some Name <some-email@email.com>
Date:   some date

    Initial commit

once you run the git revert command, you would see a new commit if you run git log again

this other command will remove the commit from the log history. Using the --soft param, will put your files that where in that commit ready for being commited again. If you use --hard instead of --soft, the files will be discarded. Once you fix whatever you need and make the commit and want to make the push, the command will have to be executed with the param --force or --force-with-lease (in the docs is well explained how they work). If you run git status before doing the push, you would see a message saying that you have to make pull and also push due to some divergence. That's why you will need to add the param mentioned

Gastón Schabas
  • 2,153
  • 1
  • 10
  • 17
  • I am able to revert the commit – Mohammad Mustafa Raza May 28 '23 at 06:59
  • But I am unable to find the code changes which I have done in android studio. – Mohammad Mustafa Raza May 28 '23 at 07:00
  • not sure if I understand your issue. You were able to do the revert, which means, the commit was undone. So, your main question was solved, right? If you are having a different issue, please mark this answer as the solution and create a new question with the specific new issue you are having now. If your issue wasn't solved, try to edit your original post with more details – Gastón Schabas May 28 '23 at 07:03