Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking out previous commit with Github and VS Code

I accidentally made a commit that I should not have to the master of my project in GitHub. I am now trying to checkout to a previous commit using VS Code. I got the URL of that commit. I do have the remote set up in VS Code but cannot figure out how to do this.

My thought was to go to checkout to and enter the URL of the commit. Then I would switch to the master branch and commit this. But that gives me an error "Can't push refs to remote".

like image 543
Alexander Avatar asked Oct 30 '25 11:10

Alexander


1 Answers

1- Open the terminal in your VS (make sure terminal openned in your project)

2- write :

$ git log

It will show you list of recent commits copy the the commit ID you want to back to it. (Any commit above will be reset)

$ git reset [commitID]

Now you have your changes not committed.

You can checkout now to the branch your work is in and stage your changes and commit them.


If the issue is in your remote (as you mentioned in comments).

There is more than one way. First sync your local with you remote Then the same approach with the git log to get the commit id. Use git revert to fix your unwanted commit

$ git revert [commitID] 

...

like image 77
Tawfik Nasser Avatar answered Nov 02 '25 01:11

Tawfik Nasser