Skip to content

Switch to a previous state of the repository

jarmanso7 edited this page Feb 6, 2020 · 3 revisions

This is an example of how to checkout a previous version of a current repository.

Scenario: We have problem with the code. We would like to test whether the problem was generated at a certain commit. We would like to checkout the repository before that commit was made.

Hereafter we assume that you normally work from fork of the repository, commit and push changes there before pulling into the upstream master.

To do so. We will go into the forked repository on github. In the [Commits](https://github.com/frakkopesto/vistasoft/commits/master).

Find the commit you think it is problematic and you would like to test code before that.

Get the the Sha-1 (hash) identifier for the commit right before the bad one. You can find these sha-1 number son the right-hand portion of the screen for each commit you can see a letter/number combination (a hex number). As you roll over that hex number with the mouse a copy icon will appear.

Then in a terminal do a git checkout with the number now in your clipboard:

   $git checkout 9193513c5b1bc063b35b830101fe7cf0d9ecffca 
   Note: checking out '9193513c5b1bc063b35b830101fe7cf0d9ecffca'.
    
   You are in 'detached HEAD' state. You can look around, make experimental
   changes and commit them, and you can discard any commits you make in this
   state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example:

   git checkout -b new_branch_name
   
   HEAD is now at 9193513... Merge pull request #30 from vistalab/jon
   

So if the command worked now you will be sitting on OLD CODE. That is what the Warning above says. Any change to the code here, if committed will generated a weird state in the upstream repository. You will be out of sync with the current newest repository. DO not commit changes while in this state.

To revert to the latest repository do the following:

   $git checkout master

This will bring you back from the temporary branch created above into the current master branch from your forked repository.