iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 16
1
自我挑戰組

Experience of a backend novice系列 第 16

哎呀,不小心加錯檔案到追蹤清單了。別擔心,使出'git reset'!

Day sixteen

What if I accidentally add an unwanted file? Let's welcome 'git reset'!
哎呀,不小心加錯檔案到追蹤清單了。別擔心,使出'git reset'!

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

Today I'm going to share a very useful and practical git function.
今天要跟大家分享一個非常實用的git指令!

As mentioned before, before we make any commits, we need to add files into tracking list. What if we accidentally add unwanted files?
如同之前的文章跟大家做的介紹,我們在commit之前都需要先將檔案加到追蹤清單內,並且建立快照,那萬一我們不小心加到我們不想要的檔案了,那該怎麼辦?

Talking is cheap! Let's have some hand-on experiment to turn knowledge into part of us.
坐而言不如起而行,讓我們通過實踐把它變成我們的東西!

If you follow our tutorial from the very beginning, you log should look like image below:
如果有從一開始就照著我們進度做的話,你們的log應該會長這樣

git log --oneline

Now let's add some more.
現在讓我們再新增幾個檔案

touch example4.html
touch example5.html
git status

As photo below:
如下圖:

Now type:
現在輸入:

git add example4.html
git status

It's supposed to look like image below:
應該如下圖:

Okay! How could we cancel this adding movement?
好,現在我們該怎麼把已經加入追蹤清單的example4.html取消呢?

Type:
輸入:

git reset example4.html
git status

As image below, those we've added now is not in the tracking list anymore.
如下,剛剛加進去的檔案example4.html現在已經不在追蹤清單內了!

What if we add several files at a time?
那如果一次多個檔案呢?

type:
輸入:

git add *
git status

The function of 'git add *' is to add all of the untracking files into tracking list, we will write some article for this later.
As image below, we've added all of untracking files.
'git add *' 的作用是把所有為追蹤的檔案都加到追蹤清單內,之後的章節如果有機會會特別跟大家介紹!
如下圖,我們將所有檔案的加進去了

So now how could we cancel all of them?
type:
現在該怎麼把全部檔案都取消追蹤呢?
輸入

git reset
git status

That's amazing Jack! All those added into tracking list becomes untracked.
太神奇了Jack! 所有原本已追入追蹤的檔案現在都變成未追蹤了!

What if not only we want to untrack them, also remove them from our working directory?
type:
那如果我不只要把他們取消追蹤,我還要把他們從我的工作資料夾中刪除呢?
輸入:

git add *
git status

Again, we've added those files into tracking list as follows:
現在檔案我們又把檔案加入追蹤清單內了了,如下。

Now we are going to cancel the tracking and remove them from our tracking list.
Type:
現在我們準備要把他們取消追蹤並從工作資料夾中移除了!
輸入:

git reset --hard
git status

See! Now our working directory is clean. All those not in the coverage of our current commit are gone.
看!現在我們的工作資料夾乾淨了!所有不在當前commit追蹤範圍之內的檔案都移除了!

Please note that 'git reset' will only be effective on working directory, that said, it will not affect your commit.
這邊要特別跟大家說明一下,git reset作用的範圍只在工作資料夾,換句話說,它不會對你的commit記錄做任何的更改。

Although it looks like 'git reset' is to remove files, in practice, 'git reset' is to reset the tracking list according to the current commit. That said, what actually 'git reset' does is to reset the tracking list based on the current commit, so any incorresponding to current commit in tracking list will be untracked. What actually '--hard' does is go further, removing the file directly when reseting.
雖然實際動作上,看起來是檔案被移除了,但是'git reset'實際上的功能,是根據當前commit的內容來重設追蹤清單。'git reset'實際上的功能,是將追蹤清單裡面的檔案,依據當前commit的內容來做重設,所以任何跟當前commit內容不符的都會被從追蹤清單裡面移除,變成未追蹤。而git reset --hard不只將檔案從追蹤清單內移除,更直接將檔案從工作資料夾中移除。

Do you like my sharing today? See you guys!
喜歡今天的文章嗎?我們明天見!


上一篇
git revert: 你在看我嗎? 你可以在近一點!
下一篇
如何移除未在追蹤清單內的檔案呢? 試試看 'git clean'
系列文
Experience of a backend novice30

尚未有邦友留言

立即登入留言