iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 15
0
自我挑戰組

Experience of a backend novice系列 第 15

git revert: 你在看我嗎? 你可以在近一點!

Day fifteen
git revert: 你在看我嗎? 你可以在近一點!
Git revert: Are you looking at me? You could come closer!

大家好,我是Ray!
Hello! It’s Ray.

In my last article I shared a very practical and useful code, git revert.
上一篇我們介紹了一個非常非常實用的功能,git revert。

Maybe you still have some confusion about this code. Today I’m going to make further explanation about the detail.
大家很可能對他還有一些疑惑,所以今天我將針對細節部分特別說明!

Some might ask, Ray, the example you made previously only reverted the current commit. What if I want to revert the one with several commits between the current one.
有些人會問,Ray,你之前的示範都只revert上一個commit,那如果我想要revert一個遠一點之前的commit,會怎麼樣?

Talking is cheap, and action is always louder than speaking. Let’s find out the answer through hand-on experiment.
在疑惑之霧前,實踐是通往明晰的火炬,讓我們通過實踐來找出答案。

Let’s add some description on the file ‘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>
</body>
</html>

Enter:
輸入:

Git commit -am “a series experiment for git revert in example2”

As image below:
如下圖

Add some description on the file ‘example1.html’
新增以下code到example1.html

<!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>
<p>This is the example commit for git commit -am</p>
<p>This is the example1 for git configuration</p>
<p>This is the example after git configuration</p>
<p>This is the first experiment description for git revert</p>
</body>
</html>

Enter:
輸入:

git commit -am "the first experiment description in example1.html"

As image below:
如下圖:

Add some description again on the file ‘example1.html’
新增以下code到example1.html

<!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>
<p>This is the example commit for git commit -am</p>
<p>This is the example1 for git configuration</p>
<p>This is the example after git configuration</p>
<p>This is the first experiment description for git revert</p>
<p>This is the second experiment description for git revert</p>
</body>
</html>

git commit -am "the second experiment description in example1.html"

As photo shown below:
如下圖:

Okay. Now we’ve had the required experimental files, let’s start the experiment.
前置作業準備完成,現在開始做實驗。

Git revert 8e29807

As expected, we’ve revert this commit.
沒有任何阻礙,我們已經revert這個commit

Now let’s go back to the revert.
現在讓我們回到revert前

If you don’t understand the code below, don’t worry. I will explain it further in later article.
(看不懂這個指令沒關係,之後的章節會針對這部份做介紹)

Git reset --hard HEAD~1
Git log --oneline

We are back as image below:
回到上一步,如下圖:

Now let’s revert a more previous commit
現在讓我們revert 更之前的commit

Git revert ee7259f

You should see some error notification as follows:
你應該會看到錯誤提示,如下:

Git status

As shown on above image, there is some conflict in example1.html file as follows:
如上圖所示,在example1 .html中有衝突發生了!如下

<!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>
<p>This is the example commit for git commit -am</p>
<p>This is the example1 for git configuration</p>
<p>This is the example after git configuration</p>
<<<<<<< HEAD
<p>This is the first experiment description for git revert</p>
<p>This is the second experiment description for git revert</p>
=======
>>>>>>> parent of ee7259f... the first experiment description in example1.html
</body>
</html>

Take a closer look on the code above. The code between ===== and <<<<< denotes the current commit, those between ===== and >>>>> means the ee7259f commit.
仔細看一下上面的code,=====以上,<<<<<<以下的代表當前的commit,=====以下,>>>>>>以上代表上一層的commit

Before going any further, let’s fix this conflict.
讓我們先來解衝突完成這次的commit

We delete all but those we want to keep as follows:
把衝突的code都刪掉,只留下我們想要保留的部分,如下:

<!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>
<p>This is the example commit for git commit -am</p>
<p>This is the example1 for git configuration</p>
<p>This is the example after git configuration</p>
<p>This is the second experiment description for git revert</p>
</body>
</html>

Enter
輸入

git add *
Git revert --continue

Now the revert is completed.
已經revert完成。

Per that rule, we could judge that when it comes to coding revising, if there is no any commits that revise the content of the file related to the commit we would like to commit, basally a conflict would not occur.
由這個規則可以判斷,如果commit 是code有所修改的話,如果指定revert的commit與當前commit的中間沒有其他commit有修改到同一個檔案的話,基本上就不會有衝突。

Let’s do another experiment to testify it.
讓我們來做另外一個實驗來驗證。

Go back to last step
先回到上一步

Git reset --hard HEAD~1
Git revert 084aea6

Now do you have a better understanding on revert?
是不是更懂revert的規則了呢?

There are no any commits that revise the content of the file related to the commit we want to revert, so a conflict would not occur, and, therefore, we do not have to fix it. That said, 084aea6 is the last file that revises the file example2, and with this condition, there will not be a conflict.
我們在當前commit與084aea6的commit之間並沒有對這個檔案做任何的修改,所以就不會發生衝突也並不需要解衝突,簡單來說,084aea6這個commit就是有修改過example2這個檔案的“最後”一個commit,在這個條件之下,我們就不會與該檔案發生衝突。

After reading through this article, do you have a better understanding on Git?
看完今天的文章,是不是對git revert有更深一層的了解了呢?


上一篇
痾.. commit錯了怎麼辦?別緊張,我們有`git revert`
下一篇
哎呀,不小心加錯檔案到追蹤清單了。別擔心,使出'git reset'!
系列文
Experience of a backend novice30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言