目錄
常言道桐早,君子一言駟馬難追活逆,世上沒(méi)有后悔的藥。但是在git的世界,你卻有一架時(shí)光機(jī),可以自由地在過(guò)去和未來(lái)之間穿梭。本文主要講解git撤回方法,將從原理進(jìn)行分析,以及仔細(xì)分析多種撤回方法的區(qū)別焚刺。
1. 工作區(qū)和暫存區(qū)
當(dāng)我們修改一個(gè)文件時(shí),通常第一步需要先git add命令把文件添加進(jìn)去门烂,然后再git commit提交更改乳愉。那么這些命令究竟做了什么事情呢?
git作為一個(gè)版本控制系統(tǒng),通過(guò)隱藏的.git文件構(gòu)建了一個(gè)版本庫(kù)屯远。工作區(qū)是指電腦里面能看到的本地工作目錄蔓姚,版本庫(kù)是由.git文件構(gòu)建,這個(gè)文件通常隱藏在工作區(qū)慨丐。版本庫(kù)中有個(gè)很重要的暫存區(qū)(stage)和分支坡脐。
我們?cè)诒镜仉娔X工作目錄修改一個(gè)readme.txt文件,新增一個(gè)LiCENSE文件房揭,使用git status查看一下?tīng)顟B(tài)备闲,代碼有一個(gè)文件修改了晌端,有一個(gè)文件增加等待加到staged。
admindeMacBook-Pro-5:learngit hzjdemac$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: readme.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
LICENSE
no changes added to commit (use "git add" and/or "git commit -a")
使用git add命令恬砂,實(shí)際上就是把文件修改從本地工作區(qū)添加到暫存區(qū)咧纠。使用git status查看一下?tīng)顟B(tài), 如圖所示stage里面多了兩個(gè)文件等待commit。
admindeMacBook-Pro-5:learngit hzjdemac$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: LICENSE
modified: readme.txt
第二步是用git commit提交更改泻骤,實(shí)際上就是把暫存區(qū)的所有內(nèi)容提交到當(dāng)前分支,此時(shí)用git status查看漆羔,則stage空空如也。使用git log命令則多了一條日志
admindeMacBook-Pro-5:learngit hzjdemac$ git status
On branch master
nothing to commit, working tree clean
admindeMacBook-Pro-5:learngit hzjdemac$ git log
commit dfe491b3581352276e5985f2b4a2057e2b3974f8 (HEAD -> master)
Author: wong
Date: Wed Nov 14 11:21:52 2018 +0800
add file
commit 367dce4c74da9a81a370a24b0655598796c25181
Author: wong
Date: Wed Nov 14 11:14:19 2018 +0800
wrote a readme file

2. Git撤回方法
reset命令能夠?qū)EAD(當(dāng)前分支的版本頂端)移動(dòng)到另外一個(gè)commit,reset在一條分支上前后移動(dòng)狱掂, 與checkout 移到到其他分支是不同的演痒。本文主要講解撤回,所以討論的是往后移動(dòng) 符欠。下面將解釋幾個(gè)參數(shù)的不同嫡霞。

2.1. git reset --soft
使用--soft參數(shù),則會(huì)將HEAD到另外一個(gè)commit之間提交的修改返回到stage希柿,說(shuō)白了就是與git commit -m 命令功能剛剛相反。
admindeMacBook-Pro-5:learngit hzjdemac$ git reset --soft HEAD~1
admindeMacBook-Pro-5:learngit hzjdemac$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: LICENSE
modified: readme.txt
使用git log命令可以發(fā)現(xiàn)剛才的提交沒(méi)有了养筒,一切回到了沒(méi)有發(fā)生過(guò)的狀態(tài)曾撤。但是這是錯(cuò)覺(jué),使用git reflog還是可以找到提交的痕跡晕粪,并回到最初的起點(diǎn)挤悉。
2.2. git reset --mix(default)
使用--mix參數(shù),則會(huì)將HEAD到另外一個(gè)commit之間提交的修改和stage內(nèi)容返回到本地工作區(qū)巫湘,說(shuō)白了就是git commit -m, git add 命令功能剛剛相反装悲。
代碼塊
admindeMacBook-Pro-5:learngit hzjdemac$ git reset 367dce4c74da9a81
Unstaged changes after reset:
M readme.txt
admindeMacBook-Pro-5:learngit hzjdemac$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: readme.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
LICENSE
no changes added to commit (use "git add" and/or "git commit -a")
2.3. git reset --hard
使用hard命令,移動(dòng)HEAD到另外一個(gè)commit時(shí)尚氛,會(huì)強(qiáng)制本地工作區(qū)進(jìn)行同步诀诊,清空stage,此時(shí)本地工作區(qū)數(shù)據(jù)和版本庫(kù)中分支數(shù)據(jù)保持一致阅嘶。這是一個(gè)比較危險(xiǎn)的動(dòng)作属瓣,如果本地新加的修改沒(méi)有保存的話,數(shù)據(jù)會(huì)丟失讯柔,永遠(yuǎn)找不回來(lái)了抡蛙。使用此命令最好先把最近修改commit一下,以后通過(guò)git reflog還能夠找回魂迄。
2.4. git checkout --
使用此命名會(huì)撤銷本地工作區(qū)的修改粗截。當(dāng)你修改了一個(gè)文件,準(zhǔn)備提交時(shí)捣炬,猛然發(fā)現(xiàn)錯(cuò)誤熊昌。幸虧發(fā)現(xiàn)得及時(shí)怠晴,使用git checkout --撤銷剛才的修改,回到上一次git commit或者git add的狀態(tài)浴捆。
2.5.git revert
要撤回中間的某次提交蒜田,此次操作之前和之后的commit和history都會(huì)保留,并且把這次撤銷作為一次最新的提交选泻。注意撤回的提交真的被拋棄了冲粤,本地工作區(qū)和stage都不會(huì)有記錄,使用git revert有可能會(huì)引起沖突页眯。
代碼塊
admindeMacBook-Pro-5:learngit hzjdemac$ git log
commit 105262a03ca8a99081669260f795021fa1fd73ec (HEAD -> master)
Author: wong
Date: Wed Nov 14 13:23:15 2018 +0800
Revert "add test"
This reverts commit f03b82e7628cc88fa025f82782e06bdd55d97948.
commit c7ed83816bb856a992009c548e30694fc185dafd
Author: wong
Date: Wed Nov 14 13:22:59 2018 +0800
stage:
commit f03b82e7628cc88fa025f82782e06bdd55d97948
Author: wong
Date: Wed Nov 14 13:21:42 2018 +0800
add test
commit dfe491b3581352276e5985f2b4a2057e2b3974f8
Author: wong
Date: Wed Nov 14 11:21:52 2018 +0800
add file
commit 367dce4c74da9a81a370a24b0655598796c25181
Author: wong
Date: Wed Nov 14 11:14:19 2018 +0800
wrote a readme file
3. 總結(jié)
修改保存在本地工作區(qū)梯捕,使用git checkout --撤銷修改
已保存到了stage, 使用git reset [--mix] HEAD 將修改文件打回本地工作區(qū);
已保存到了分支窝撵,使用git reset [--mix] HEAD~1將修改文件打回本地工作區(qū)傀顾;
要拋棄最近提交,使用git reset [--hard] HEAD~1將上一次提交同步到本地工作區(qū)碌奉;