iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 9
0

Day night
使用git checkout
Use git checkout

Hello everyone, it’s Ray!
大家好,我是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.
沒錯,上次的git介紹我們從初始化開始,並且建立一個名為example1.html的檔案,然後完成了我們第一個存擋!

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.
如同之前提到的,我說git讓我們再存擋後,如果我們有需要的話,我們可以隨時地回到任何一個我們用git做的存擋點,今天我將跟大家分享如何回到存擋點,並且在存擋點之間自由的切換。

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:
現在,讓我們在檔案內加入下面highlight的一段

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

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:
現在我們可以看到我們的第二個commit如下圖:

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有更深的了解,我們明天見!


上一篇
如何使用Git-如何初始化git
下一篇
覺得每次都要git add 以及 git commit 太麻煩了嗎? 請服用 git commit -am
系列文
Experience of a backend novice30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言