iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 14
0
自我挑戰組

Experience of a backend novice系列 第 14

痾.. commit錯了怎麼辦?別緊張,我們有`git revert`

Day fourteen

What if I make a wrong commit? Don’t panic, git revert is at your disposal.
痾.. commit錯了怎麼辦?別緊張,我們有git revert

Sometimes after pushing new commit, we realise that there seems to be something wrong.
有時我們把功能做好並且推上公共資料夾之後才發現,靠...我commit好像推錯了...

Don’t panic. In this case we could use git revert to cancel the commit.
別緊張,這時候我們可以使用git revert 來取消我們的commit。

Now let me illustrate it as follows:
現在讓我來為各位做個示範:

Please enter the code below:
請輸入以下指令:

We add a new line of description as follows:
在example2.html檔案裡增加新一行code,如下:

<!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>
</body>
</html>

And then
接著

Git add example2.html

Git commit -am “A wrong commit”

Git log --oneline

As image shown above, we accidentally push a wrong commit, what should we do?
如上圖,我們不小心上傳了一個錯了commit,那該怎麼辦呢?

Enter the code below:(yours will be different from mine. Please enter yours)
輸入以下指令:(你們的commit名稱跟我的不一樣,請記得輸入你們自己的)

Git revert c93359a

A window will pop up like image above
接著你們會看到畫面如上

On the left-top corner, you could leave a specific message for this commit, and if you don’t want to specify it, just enter :wq to leave and complete this revert.
在圖片上的左上角可以留下針對這段revert的訊息記錄,如果沒有要特別加註的話,輸入:wa離開即可。

Enter
輸入
git log --oneline

As image above, an additional occurs with a message of ‘A wrong commit’
如上圖,多了一個commit寫著Revert “A wrong commit”

Now let’s go back to check the file if the description we added in the beginning still exists.
現在讓我們回到我們的檔案裡面去看,一開始新增的那段code是否還在。

`

You could see that the description we added in the beginning has gone, and which denotes that we’ve successfully revert the designated commit.
可以看到,我們一開始加的那段已經不在了,這代表我們已經成功的revert指定的commit

Some git novices might have the same confusion as mine when I learnt this part. Why don’t we just eliminate the commit? instead, we would we add one more?
有些剛接觸git的人可能會跟我當初有同樣的疑問,那為啥不要整個git的log紀錄都抹掉就好,為啥要多一個commit?

Here I would like to make a further explanation.
Normally, after pushing our commit to mutual repository, I strongly urge you not to revise the existing history. Because once you revise the existing history and push it to mutual repository, it could cause a huge impact to the history on everyone’s repository. After revising, every collaborator’s history will be different from yours, which would cause a lot of confusion and conflict.
這邊跟大家解釋一下,如果今天我們已經把我們完成的進度推到共同資料夾了,我們就不建議去修改歷史了,因為你一但修改了歷史再往上推,整個共同資料夾的歷史就會改變,共同資料夾的紀錄相當於所有協作者的紀錄,所以如果你單方面變動了歷史,很可能會造成所有協作者的歷史都跟你的不一致,甚至在commit的過程中會有衝突,這在多人協作是相當不建議的。

What we want to take out is a code existing in the file, so realistically, we want to cancel the code, not history. In multi-collaboration, you could add new history, and not recommended revising old one. You could add a new commit specifying what you’ve done, but not to revise the history on your side, because only you know what you’ve done, and other people know nothing on your side.
我們要拿掉的,是我們檔案內的一段有commit紀錄的code,所以實際上我們要取消的是code,不是歷史,而在多人協作中,歷史是可以增加,不建議修改的。你可以新增一個commit明確說明我這段commit是新增或者拿掉了什麼東西,但是不建議單方面地把東西拿掉並且去修改你的歷史紀錄,因為你改的東西只有你自己知道,對於其他的協作者來說他們並不知道在你的電腦上發生了什麼事。

In other words, before you push your part to mutual repository, you could do whatever you want (only to what you haven’t pushed. Don’t revise anything you’ve pushed), however, after pushing, don’t revise the history. If you want to do some revising on the file, just make a new commit explaining what you’ve done and push it, and then you could avoid possible confusion and conflict.
簡單來說,在你上傳到共同資料夾之前,你可以對你的歷史做任何事(這邊僅限於還未上傳的部分,已經上傳的不建議去修改),但是一但上傳之後,就不建議去修改歷史。如果你要對檔案內容做任何修改,請新增一個commit說明修改的內容,這樣才不會造成其他協作者的疑惑以及大家的git歷史有衝突。

It’s my sharing today. See you guys.
以上就是今天的分享,我們明天見!


上一篇
Git tag, 標注一個版本號碼
下一篇
git revert: 你在看我嗎? 你可以在近一點!
系列文
Experience of a backend novice30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言