大部分的人都會犯錯。所以每VCS提供了一個功能,修正錯誤,直到特定的點。 Git提供功能使用,我們可以撤銷已作出的修改到本地資源庫。
假設用戶不小心做了一些更改,以他的本地的倉庫,現在他要扔掉這些變化。在這種情況下,恢復操作中起著重要的作用。
## 恢復未提交的更改
讓我們假設Byron 不小心修改文件從自己的本地倉庫。但他想扔掉他的修改。要處理這種情況,我們可以使用git checkout命令。我們可以使用這個命令來恢復文件的內容。
[byron@CentOS src]$ pwd
/home/byron/byron_repo/project/src
[byron@CentOS src]$ git status -s
M string_operations.c
[byron@CentOS src]$ git checkout string_operations.c
[byron@CentOS src]$ git status –s
甚至我們可以使用git checkout命令刪除的文件從本地庫。讓我們假設Sampson 刪除文件從本地存儲庫,我們希望這個文件。我們可以做到這一點,使用相同的命令。
[sampson@CentOS src]$ pwd
/home/sampson/top_repo/project/src
[sampson@CentOS src]$ ls -1
Makefile
string_operations.c
[sampson@CentOS src]$ rm string_operations.c
[sampson@CentOS src]$ ls -1
Makefile
[sampson@CentOS src]$ git status -s
D string_operations.c
Git是顯示文件名前的字母D。這標志著該文件已被刪除,從本地資源庫。
[sampson@CentOS src]$ git checkout string_operations.c
[sampson@CentOS src]$ ls -1
Makefile
string_operations.c
[sampson@CentOS src]$ git status -s
> 注意:我們可以執行所有這些操作之前提交操作。
## 刪除臨時區域變化
我們已經看到,當我們執行加法運算;文件移動從本地存儲庫,參數區域。如果用戶不小心修改一個文件,并把它添加到臨時區域,但他馬上意識到犯了錯誤。他希望恢復他的改變。我們可以處理這種情況下,通過使用git checkout命令。
在Git是一個HEAD指針始終指向最新提交。如果想撤銷變更分階段區域,那么可以使用git checkout命令,但checkout命令,必須提供額外的參數HEAD指針。額外提交指針參數指示的git checkout命令,重置工作樹,還能夠去除分階段。
讓我們假設Sampson 從自己的本地倉庫修改一個文件。如果我們查看這個文件的??狀態,它會顯示文件被修改,但不加入臨時區域。
sampson@CentOS src]$ pwd
/home/sampson/top_repo/project/src
# Unmodified file
[sampson@CentOS src]$ git status -s
# Modify file and view it’s status.
[sampson@CentOS src]$ git status -s
M string_operations.c
[sampson@CentOS src]$ git add string_operations.c
Git 的狀態顯示該文件是在分期區域,現在它恢復使用git checkout命令和視圖狀態恢復的文件。
[sampson@CentOS src]$ git checkout HEAD -- string_operations.c
[sampson@CentOS src]$ git status -s
## 將HEAD指針與GIT復位
做一些改變后,可能會決定刪除這些更改。 Git 復位命令是用來重置或恢復一些變化。我們可以執行三種不同類型的復位操作。
下圖顯示圖形表示 Git復位命令。
<center>


</center>
## SOFT
每個分支都有HEAD 指針指向最新提交。如果我們使用git --soft復位命令選項,隨后提交ID,然后將只有頭指針復位,不破壞任何東西。
.git/refs/heads/master 文件存儲的提交ID 的HEAD 指針。我們可以驗證它通過使用git log -1 命令。
[byron@CentOS project]$ cat .git/refs/heads/master
577647211ed44fe2ae479427a0668a4f12ed71a1
現在查看最新提交的ID,這將配合上述提交ID。
[byron@CentOS project]$ git log -2
上面的命令會產生以下結果。
commit 577647211ed44fe2ae479427a0668a4f12ed71a1
Author: Sampson <sampson@12xue.com>
Date: Wed Sep 11 10:21:20 2015 +0530
Removed executable binary
commit 29af9d45947dc044e33d69b9141d8d2dad37cc62
Author: Byron <xiaobosun@gridinfo.com.cn>
Date: Wed Sep 11 10:16:25 2015 +0530
Added compiled binary
讓我們重設HEAD 指針。
[byron@CentOS project]$ git reset --soft HEAD~
現在我們只重設HEAD 指針回到一個位置。讓我們檢查內容.git/refs/heads/master 文件.
[byron@CentOS project]$ cat .git/refs/heads/master
29af9d45947dc044e33d69b9141d8d2dad37cc62
從文件提交ID改變,現在驗證通過查看提交的信息。
byron@CentOS project]$ git log -2
上面的命令會產生以下結果。
commit 29af9d45947dc044e33d69b9141d8d2dad37cc62
Author: Byron <xiaobosun@gridinfo.com.cn>
Date: Wed Sep 11 10:16:25 2015 +0530
Added compiled binary
commit 94f7b26005f856f1a1b733ad438e97a0cd509c1a
Author: Byron <xiaobosun@gridinfo.com.cn>
Date: Wed Sep 11 10:08:01 2015 +0530
Added Makefile and renamed strings.c to string_operations.c
## 混合
Git的復位 -- mixed選項從分段區域尚未提交還原更改。它僅恢復變化形成暫存區。實際所做的更改到工作副本的文件不受影響。默認的Git的復位相當于git的復位 -- mixed。
有關更多詳細信息,請參閱部分刪除同一章臨時區域變化。
## HARD
如果使用 - hard選項用 Git復位命令,它會清除暫存區域,它會重設HEAD 指針,以最后一次提交的具體提交ID,也刪除本地文件的變化。
讓我們檢查提交的ID
[byron@CentOS src]$ pwd
/home/byron/byron_repo/project/src
[byron@CentOS src]$ git log -1
上面的命令會產生以下結果。
commit 577647211ed44fe2ae479427a0668a4f12ed71a1
Author: Sampson <sampson@12xue.com>
Date: Wed Sep 11 10:21:20 2015 +0530
Removed executable binary
Byron 修改過的文件,在文件的開始,加入單行注釋。
[byron@CentOS src]$ head -2 string_operations.c
/* This line be removed by git reset operation */
#include <stdio.h>
他使用git status命令驗證。
[byron@CentOS src]$ git status -s
M string_operations.c
Byron 修改后的文件添加到分期區域,并驗證它與git的狀態運行。
[byron@CentOS src]$ git add string_operations.c
[byron@CentOS src]$ git status
上面的命令會產生以下結果。
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
#
modified: string_operations.c
#
Git的狀態,顯示該文件是在臨時區域。現在重置HEAD 用 --hard選項。
[byron@CentOS src]$ git reset --hard 577647211ed44fe2ae479427a0668a4f12ed71a1
HEAD is now at 5776472 Removed executable binary
Git 復位命令成功,這將恢復從分段區的文件,以及刪除本地對文件所做的更改。
[byron@CentOS src]$ git status -s
Git 狀態顯示,文件恢復從分段區。
[byron@CentOS src]$ head -2 string_operations.c
#include <stdio.h>
Head 命令還顯示,復位操作刪除局部變化。