Exploring History

Overview

Teaching: 25 min
Exercises: 0 min
Questions
  • How can I access old versions of files?

  • How do I review my changes?

Objectives
  • Use the GitHub website to look back in time.

  • Compare various versions of tracked files.

  • Restore old versions of files.

The main advantage of Git is that it allows us to look back in time, revert to previous versions, and see what has changed. There are couple different things we might want to do with this ability.

Reverting To An Old Version of the Repository

If you want to roll back all the changes you made in the most recent commit, and just revert to the previous state of the repository, you can do this in GitHub Desktop.

Start by navigating to the “History” tab. Right-click on the previous commit, and you’ll see the option to revert this commit.

Revert commit

 

If you click on Revert This Commit, two things will happen.

The first is that the files in your repository will revert to their previous state.

The second thing that happens when you successfully revert a commit is that you’ll see a new commit appear in the History tab, while no changes have appeared in the Changes tab. What Git has done behind the scenes is to calculate all the changes it needs to make to the current files in order to get the old files back, implement those changes, and then commit them. What this means is that the state you reverted from still exists in the repository’s history, and you can revert the revert to get them back.

Reverting When You Have Changes

If you had unsaved changes in those files you will lose them. If you had saved, but uncommitted changes, GitHub Desktop will ask you to commit those changes first.

Stash

 

Reverting Multiple Commits

Reverting does not take you back to a specific committed state. Rather, it undoes the changes of a specific commit action. What this means is that in order to revert back multiple commits, you must revert them one at a time, starting with the most recent. Reverting a commit in the middle of the history might lead to incoherence in your repository and is a Very Bad Idea.

Exploring Previous States

If you want to look at the files from previous commits without actually reverting the changes, that is best done on the GitHub website. Right-click on the commit you’re interested in, and choose View on GitHub.

View Previous

 

This will take you to that commit’s “diff” page on GitHub.

GitHub Diff

 

The “diff” page shows the difference between this commit and the previous one. This can be informative, but may not be what you’re looking for. To view the state of the repository and all its files at that point in time, click on the “Browse Files” button in the top right corner. This will show you a list of all the files and directories. You can then click on the files you’re interested in, look at their contents, and even download them.

To download mars.txt, click on it, then click on the Raw button and File >> Save the resulting plain text page.

View Raw

 

Save it outside your repository to make sure you aren’t accidentally overwriting your existing files. Then you can make your decisions about how to manually revert your changes.

Save Raw

Key Points

  • The GitHub website will show a list of changes, and show the differences between commits.

  • We can download or copy from old versions of files.