2019 iT 邦幫忙鐵人賽


Day night
使用git checkout
Use git 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
然後我們到command line,輸入git status

You will see what looks like the following image, which indicating that the example1.html file has been modified.
你應該會看到如下圖,如下圖所示,git 顯示example1.html已經被修改過了。

As mentioned in the last article, before making any commit, we need to use git add to specify what we want to commit.
如上一篇提到的,再做存擋之前,我們必須要先使用git add 來指定我們想要存擋的進度,所以

So let’s type git add example1.html
輸入git add example1.html

and type git status
輸入git status

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

Now type git commit
現在輸入git commit

And leave the message “New paragraph added in example1.html file” for this commit.
並記錄訊息”New paragraph added in example1.html file”

After that we type git status to check it
完成後輸入git status確認一下狀態

And then git log
然後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
git log 的功能是顯示我們所有記錄點的歷史,我們可以經由log裡面提供的資料自由的切換於不同的紀錄點。

Type git checkout b45934852da471efbbbc52b5a119e8723fb01866
輸入 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.
現在可以打開我editor查看,我們新增加的We add a new paragraph on the first example 已經不見了,此時版本恢復到我們第一個記錄點的狀態,不管我們是否有另外在editor做任何的紀錄。

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

Type git checkout master
輸入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!
現在打開我們的editor做確認,登登! 原本消失的new paragraph 又出現啦!

Isn’t it a magic?

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

覺得每次都要git add 以及 git commit 太麻煩了嗎? 請服用 git commit -am
Experience of a backend novice30