- So by now you should be pretty awesome
at moving things between the work environment
and staging.
So let's take a look at what we can do
with the different commits that are already logged.
Now every time you make a commit,
it creates a logged entry
and you can look at it by using
the git log command.
So you can see here
that I have three different commits
and they're identified in two ways.
First by this commit message that I put in myself
but then also each one of these gets
this really long hash.
This is just a bunch of hexadecimal numbers
and it's extremely long.
So you can actually use these different hashes
to pull the commit
from the log
into the working environment
and we do that by using the checkout commit.
So we do a git checkout
and then we would type in one of these hashes.
Obviously you don't want to type it all in.
You can copy any part of this hash,
it has to be enough characters
so that it identifies as being different
than one of these other ones
but honestly, it's just easier double click right here
and then just copy them
or just grab, like the whole thing,
and copy it
and then you can issue a git checkout command
and then you paste that hash.
This is going to take me to the point in my environment
where I had made the first commit
but none of these other two things.
So let me go ahead and hit return
and you'll see that it gives you this pretty long message,
let me go ahead and make this a little bigger
so we can take a look at it.
So it's telling you that you're in this weird thing
called a 'detached HEAD' state,
you can look around, make experimental changes,
et cetera, et cetera,
so this is sort of a playground
where you can mess around with your project
and do whatever you want
and then you can go back
into the original set of commits
without messing anything up.
So this is sort a great place to mess around with
maybe a feature that you're thinking about implementing.
Think of it as an alternate reality
that starts at the commit that you checked out.
Now it's also telling you right here
that if you want to,
you can retain what you're doing in this area
as another branch.
We'll talk about branches a little bit later.
Let's just kind of play around with what's in here.
So let's go ahead and clear this out
and let's do a git log.
So if you look at the log right now
it only has one entry.
It doesn't know anything about the other two log entries.
That's in our original timeline.
Remember this is our alternate reality.
So from here we can do any sort of work just like before.
So let's go ahead and modify
this index at HTML to say
Alternate Home.
I'm going to save this
into a git status command.
It shows that
still, my HEAD is detached,
it's always going to be telling you this right here.
Just ignore it for right now
but just like before,
it notices that we have saved something.
It's in the working environment
and we can, if we want to, stage this.
So let's go ahead and git add .
to move that into the staging environment
and we'll do a git commit
and a message here, we'll call it
alternate one.
Right so let's go ahead and clear this out,
take a look at our log.
You can see that here is the first commit message
that I based my alternate reality on
and here we've committed a second log entry
and it has the message called 'alternate one'.
So we can keep on going in here,
make whatever changes we want,
keep on editing files,
and then whenever we're done fooling around
and we want to go back to our normal universe
where all those other commits existed,
we can do that by going back
to our different branch.
Now you haven't learned about branches before
but git stores all of its alternate realities
in something called branches.
The main branch is called the master branch.
So we can take a look at all the branches
by issuing the git branch command
and notice that there are two quote end quote branches here.
There's one called master,
this is our original universe,
and then we've got this other thing
called HEAD detached
from this hash right here.
So notice that this is actually matching the hash
in this commit right here.
Now this star to the left of the branch names
identifies the current branch.
So it's showing us that we're in this HEAD detached state.
Let's say that we don't really care
about any of these changes,
we just want to go back into our normal branch.
So what I'm going to do is,
I'm going to use the git checkout command
and this time I'm going to checkout
this branch called master.
So now it's telling me right here that I'm in
a git branch 'master'
It's also telling me that
if, by any chance, you wanted to keep this alternate reality
you can go ahead and create a new branch.
We'll talk about new branches later on
but just know that
if you wanted to keep all the stuff that you did
in a separate branch,
you can do that by using this command:
git branch and then you put in
whatever you want to call that branch
and then you refer to this piece of the hash right here
and so it would be able to save everything
that you had done while you were messing around
in this alternate reality.
Now on a Mac, you're always going to be able
to scroll back to this area
but if you're on a PC,
you're going to want to write down this text,
especially this part that, on my screen,
is bc27278.
On your computer, this hash will be a little bit different,
but just write that down
because you're going to need it later on.
Now let's go ahead and clear
and I'm going to show you that, if we do git log,
we're going to have all the original logs
that we had done before.
So let's go ahead
and assume that we wanted to save that version
of our branch that we created,
that version in the alternate universe, that we created.
So let's say that we wanted to go to that earlier branch.
Now on a Mac, even though we cleared the screen,
you're going to be able to scroll up
and see any previous commands,
including that branch command from earlier.
Now if you're on a PC,
you won't be able to scroll back
but if you remember or wrote down the reference
from the previous screen
you're going to be able to paste that back
and add the branch later.
So the nice thing about the terminal
is that you can usually scroll up
and it doesn't matter that the next command
that we issued was a clear and a git log.
We can actually just grab this right here.
Now on a Mac, I'll be able to select this text
from the previous screen
and do a command seed copy this text
and then, I'm going to scroll down here,
and just paste it
and I'm going to change the name of this,
because I don't want it to be called new branch like that,
I'll just call it alternate.
Now remember on a PC,
you're going to have to remember that hash
and yours is going to look different than mine
so just remember what that piece of text is,
write it and--
as much as possible, try to just create the branch
as soon as you know that you're going to want it later
and then I'm going to hit return.
So now if we,
let's go ahead and clear,
if we use the git branch command
to list our branches,
you'll notice that we have two branches:
the master branch as well as the alternate branch.
And if we wanted to switch out to our other universe,
let's go ahead and go back here,
actually I wanted to show you that,
if we click back on the text editor,
this should look like in the original master branch.
But if we want to, we can go back
into this alternate branch.
So let's do a git checkout
and we'll do alternate,
so now we're in the alternate universe,
we click back here,
we should see the changes that we had made before,
this Alternate Home is now our alternate universe.
If we do a git log,
you can see the two commits that I had done
and git branch shows you that
the starred branch is this alternate branch
and that's where we are.
Now, just so we are in the same spot
that we were before,
let's go ahead and switch to the master branch again.
And once again, we're in our original universe
in the project that we were working on
and a lot of times you'll use this
when you figured out that, after a few commits,
you made a mistake somewhere.
You want to go to a log entry
that you know was working fine
and then you want to play around
and try to recreate what you were trying to accomplish
and see if you can
get it to work
and then, what you can do is,
save that as a branch
and then bring that branch back in to master.
There's a lot more to branches
and we'll cover some of that in the next video.