git revert 和reset的區別

這裏講一下git revert和git reset的區別: html

git revert 是撤銷某次操做,這次操做以前的commit都會被保留 git

git reset 是撤銷某次提交,可是這次以後的修改都會被退回到暫存區 spa

具體一個例子,假設有三個commit, git st: 3d

commit3: add test3.c htm

commit2: add test2.c blog

commit1: add test1.c 源碼

當執行git revert HEAD~1時, commit2被撤銷了 it

git log能夠看到: test

commit1:add test1.c file

commit3:add test3.c

git st 沒有任何變化

若是換作執行git reset --soft(默認) HEAD~1後,運行git log

commit2: add test2.c

commit1: add test1.c

運行git st, 則test3.c處於暫存區,準備提交。

若是換作執行git reset --hard HEAD~1後,

顯示:HEAD is now at commit2,運行git log

commit2: add test2.c

commit1: add test1.c

運行git st, 沒有任何變化

----------------------------------------------------------------------------------------------

另外,說一下git revert ,git reset –hard和 –soft的區別

git reset –mixed id ,是將git的HEAD變了(也就是提交記錄變了),但文件並無改變,(也就是working tree並無改變)。
git reset –soft id. 實際上,是git reset –mixed id 後,又作了一次git add
git reset –herd id.是將git的HEAD變了,文件也變了。

git revert與git reset最大的不一樣是,git revert 僅僅是撤銷某次提交。
好比git revert HEAD~1  ,那麼會撤銷倒數第二次的提交結果。而倒數第一次的提交記錄,仍然在。
若是git reset –hard HEAD~1,那麼,commit退回到倒數第三次的狀態中。
整體來說,仍是git revert 好啊,雁過留聲嘛。

其實,經過git reset –soft id的方法,能夠將原來屢次的git提交記錄合併爲一個。

git reset是指將當前head的內容重置,不會留log信息。

  • git reset HEAD filename  從暫存區中移除文件
  • git reset –hard HEAD~3  會將最新的3次提交所有重置,就像沒有提交過同樣。
  • git reset –hard commit (38679ed709fd0a3767b79b93d0fba5bb8dd235f8) 回退到 38679ed709fd0a3767b79b93d0fba5bb8dd235f8 版本

----------------------------------------------------------------------------------------------

根據–soft –mixed –hard,會對working tree和index和HEAD進行重置:

  1. git reset –mixed:此爲默認方式,不帶任何參數的git reset,即時這種方式,它回退到某個版本,只保留源碼,回退commit和index信息
  2. git reset –soft:回退到某個版本,只回退了commit的信息,不會恢復到index file一級。若是還要提交,直接commit便可
  3. git reset –hard:完全回退到某個版本,本地的源碼也會變爲上一個版本的內容

git reset -soft :取消了commit  

git reset -mixed(默認) :取消了commit ,取消了add

git reset -hard :取消了commit ,取消了add,取消源文件修改

====

http://hi.baidu.com/lingzhixu/blog/item/569f45c4cbf39925f8dc619e.html

相關文章
相關標籤/搜索