How to use checkout

Hello everyone, it’s Ray!

Remember where we were in my last article? I hope that the image above would do some help.

You are right, last time we introduced how to initialise Git, and create a file called example1.html, and then complete our first commit.

As mentioned previously, Git allows us to go back to whatever commit whenever we want. Today I’m going to share how to freely switch among different commits.

Now let’s add some experimental code <p>We add a new paragraph on the first example</p> in the example1.html file as follows:

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<title>First example</title>
<p>This is the first example</p>
<p>We add a new paragraph on the first example</p>

And then we go to command line, and type git status

You will see what looks like the following image, which indicating that the example1.html file has been modified.

As mentioned in the last article, before making any commit, we need to use git add to specify what we want to commit.

So let’s type git add example1.html

and type git status

As the image above, we’ve specified what we are going to commit

Now type git commit

And leave the message “New paragraph added in example1.html file” for this commit.

After that we type git status to check it

And then git log

We will see the second commit that we just made as follows:

Okay, now let’s go back to our first commit.

The git log shows the history of all of the commits, and therefore we could use the information within to switch among different commits

Type git checkout b45934852da471efbbbc52b5a119e8723fb01866

This checksum is my version, and it varies upon different time, author, even the data. In other words, it’s unique, so yours will be different from mine.

As image shown below, now we are already at our first commit

Now let’s open editor to check. The experimental code <p>We add a new paragraph on the first example</p> is gone. Now we go back to our first commit, and it doesn’t matter if we’ve saved it with our editor or IDE.

So now how could we go back to our latest commit?

Type git checkout master

As image shown above, now we are at our latest commit.

Now go to editor to check. See! The disappeared “new paragraph” appears again!

Isn’t it a magic?

Hope that it will do some help on the understanding of git of yours. See you guys!

How to skip git add? Why git is so much required?


Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now