iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 20
0
自我挑戰組

Experience of a backend novice系列 第 20

git branch - 回到過去開個分支,開始新的人生

Day twenty

如何在過去的某一點建立commit分支?
How could I make a branch in previous commit?

We shared how to use 'git branch' yesterday. Today I'm going to share with you guys how to make a git branch in the previous commit?
Try to visualize what it looks like?
在昨天的文章我們介紹了如何使用git分支,今天要跟大家分享,如何在過去的某個commit上建立一個分支,並且請大家先思考一下,如果我們將它視覺化之後,它長什麼樣子呢?

If you've been following this series of article, you log should be like the image below:
如果你有從一開始就跟著我們的進度,那你們git歷史應該如下圖:

Now let's add a few more commits, and go back to v2.0 and make a new branch.
現在讓我們增加幾個commit,並且回到tag v2.0的地方再重新建立一個分支。

Add a new line of description in example2.html
在example2.html新增以下敘述'It's the first commit beyond v2.0':

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<p>This is the experimental file created after reversion v1.0</p>
<p>This is a wrong commit</p>
<p>This is a example description for further git revert explanation</p>
<p>It's the first commit beyond v2.0</p>
</body>
</html>

And then commit it
然後commit

git commit -am "The first commit beyond v2.0"

Add another line of description in the same file.
在新增另一段敘述'It's the second commit beyond v2.0'

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<p>This is the experimental file created after reversion v1.0</p>
<p>This is a wrong commit</p>
<p>This is a example description for further git revert explanation</p>
<p>It's the first commit beyond v2.0</p>
<p>It's the second commit beyond v2.0</p>
</body>
</html>

And then commit again
然後再commit

git commit -am "The second commit beyond v2.0"
git log --oneline

Now our log should look like image above, there are two new commits after v2.0
現在我們的歷史應該如上圖,在v2.0之後新增了兩個commit

Now let's go back to v2.0, and make a new branch called 'test'
現在我們將回到v2.0,並在v2.0新增一個分支'test'

git checkout v2.0
git branch test
git checkout test
git log --oneline

As photo above, now we are already at v2.0, and at a new branch called 'test', let's check if the description we added after v2.0 still exists
如上圖,我們現在已經回到v2.0,並且在v2.0的位置建立一個分支
讓我們確認下在v2.0之後新增的敘述還在嗎

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<p>This is the experimental file created after reversion v1.0</p>
<p>This is a wrong commit</p>
<p>This is a example description for further git revert explanation</p>
</body>
</html>

Apparently, the description we added is gone, because now we are at a new branch from v2.0
如上,我們剛剛在v2.0之後新增的敘述已經不在了,因為我們現在已經回到v2.0,並且位於新建的一個分支上。

And what will it look like if we commit on this branch?
那如果我們在此分支上commit的話,那該會是什麼樣子呢?

Add new description on the example2.html
在example2.html的內容裡新增以下敘述:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<p>This is the experimental file created after reversion v1.0</p>
<p>This is a wrong commit</p>
<p>This is a example description for further git revert explanation</p>
<p>It's newly added description on new branch test</p>
</body>
</html>
git commit -am "Add new descrioption on new branch test"
git log --oneline

As image above:
如上圖:

Try to visualize what it would look like?
試想,如果把git視覺化的話,該是什麼樣子呢?

It looks like image below
會是像以下的圖片:

Let's go back to master branch and delete the branch we added, and go back to v2.0 tag
讓我們回到master並把剛剛的分支都刪除掉,回到v2.0的commit

git checkout master
git reset --hard HEAD~2
git branch -D test
git branch
git log --oneline

As above image, we've deleted the branch we just made, and make the HEAD on v2.0
如上圖,我們已經刪除了剛剛建立的分支,以及讓HEAD回到v2.0的地方。

Do you like my article today? See you tomorrow.
喜歡今天的文章嗎? 我們明天見。


上一篇
git分支(git branch)技能書
下一篇
Git的物件,當我們commit時,究竟發生了什麼事
系列文
Experience of a backend novice30

尚未有邦友留言

立即登入留言