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:
And then we go to command line, and type
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
As the image above, we’ve specified what we are going to commit
And leave the message “New paragraph added in example1.html file” for this commit.
After that we type
git status to check it
We will see the second commit that we just made as follows:
Okay, now let’s go back to our first commit.
git log shows the history of all of the commits, and therefore we could use the information within to switch among different commits
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?
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!