You've seen how a commit is a snapshot of every file in
the repository at the time the commit was made.
We can use git to view the difference between any two commits.
For example the difference between these two commits,
would show what code I added to add more color to the game.
Being able to view the difference between any two commits is pretty convenient.
But we can also temporarily change our files back to how they were at
the time of any commit.
This is called a git checkout, and
it's sort of like restoring a previous version.
If you're coming from SVN, this is not the same thing as a SVN checkout.
In git, checking out a commit means resetting all of your files to
how they were at the time that commit was made.
Why would you want to check out a previous version of your code?
One reason might be,
if a bug was introduced, but you're not sure which commit introduced it.
You can test wether a commit has the bug by checking out that commit and
running the code.
To run the code in the Asteroids Repo,
I'll need to open up the index.html file on a web browser.
If I'm on Windows I'll first open up this file navigation GUI.
Next I'll need to navigate to the directory where the Asteroids Repository is.
For me that's in my home directory, so under C, users, Seabucky.
And then I put it in a version control directory and
then in the Asteroids folder.
Now I want to open this index.html file.
So I'll right-click on it and select Open with Google Chrome.
Now, sadly, this game will not work in Internet Explorer.
So you'll need to use either Chrome or
Firefox to open this file, unless the process is the same.
But you should navigate to the file using Finder.
If you've stored your repository in your home directory and you're
having trouble finding it, just press CMD+SHIFT+H to find your home directory.
For me the files are in the version control directory and
then the Asteroids directory.
So I'll right-click on index.html and open with Google Chrome.
So you can see the game looks a little different now.
The asteroids are solid and gray.
And my spaceship is blue.
I'll just go ahead and play for a little bit.
Whoa, what just happened?
I can shoot an endless stream of bullets.
This isn't how I remember Asteroids working.
Okay, as fun as that was I don't think it was the intended behavior.
One of the recent commits must have introduced this bug.
But which one?
It's going to be hard to tell just by looking at the git log.
So.
Let's check out an older commit and see whether it has the bug.
I'm pretty sure the code was working when I reverted the controls.
So I'll start by checking out that commit to double check.
I'll type git checkout.
And then copy and paste the commit ID.
If you're on Mac you can copy and
paste the commit ID by just using Cmd+C and Cmd+V.
If you're using Linux you'll need to copy and
paste with Ctrl+Shift+C and Ctrl+Shift+V.
And if you're using Windows, then you'll need to enable QuickEdit mode.
Which you can find instructions for how to do in the instructor's notes.
And then you can copy and paste by right-clicking.
So now I'll go ahead and check out this commit.
Now I get this strange warning we mentioned before.
You are in detached head state.
Like we mentioned, head is what git calls the commit that you're currently
working on, and you've detached it here by checking out an older commit.
Now here's the reason git thinks this is worth learning about.
What do you think would happen if we made changes while we
had this older commit checked out?
Where would the commit fit in our history?
It gets a little complicated so git includes some instructions for
how you can deal with this.
Don't worry if you don't understand these instructions yet.
We'll be going over them in detail in lesson two.
For now, we're not going to make any changes, we're just going to run the code,
so you won't need to worry about the rest of this message.
Now I'm going to run the code and see if it has a bug.
Even when I hold down space, I can only shoot one bullet at a time.
It looks like the bug hasn't been introduced yet.
If it had we could have just checked out an even older commit and tried again.
Now an interesting thing happens if I type git log while I
have this older commit checked out.
Revert controls shows up as the most recent commit, and the commits from
before like Doug touching up the iPad version of the game and me adding color.
Aren't showed in git log at all.
Now can still get back to the most recent commit by typing git checkout,
followed by its ID.
But what if you don't remember the ID?
For now we've put it into the instructor's notes for you, so
don't worry about it.
In lesson two, you'll also learn another way to switch between commits without
knowing their IDs.
Now I've narrowed down the commit that introduced the bug to one of five.
Go ahead and
do the rest of the work and find the exact commit that introduced the bug.
Paste the ID of the first commit with the bug into this box.
If you run into any issues, check out the troubleshooting guide we've added to
the downloadable section, and see whether your problem is covered.
If it's not, please ask about your problem on the forms.
Since this is your first time using git to check out previous commits,
this process might take a little longer than you'd like.
You might even find it faster to track down the bug without using git.
Or you might want to throw your computer out the window.
However the time you're investing now, will keep paying off in your projects for
years to come.
Eventually you'll barely be able to remember how hard using git was at first.