git revert 和reset的區別

2021-07-28 19:52:04 字數 1959 閱讀 1725

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

git revert 是撤銷某次操作,此次操作之前的commit都會被保留

git reset 是撤銷某次提交,但是此次之後的修改都會被退回到暫存區

具體乙個例子,假設有三個commit, git st:

commit3: add test3.c

commit2: add test2.c

commit1: add test1.c

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

git log可以看到:

commit1:add test1.c

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進行重置:

git reset –mixed:此為預設方式,不帶任何引數的git reset,即時這種方式,它回退到某個版本,只保留原始碼,回退commit和index資訊

git reset –soft:回退到某個版本,只回退了commit的資訊,不會恢復到index file一級。如果還要提交,直接commit即可

git reset –hard:徹底回退到某個版本,本地的原始碼也會變為上乙個版本的內容

git reset -soft :取消了commit

git reset -mixed(預設) :取消了commit ,取消了add

git reset -hard:取消了commit ,取消了add,取消原始檔修改

Git revert使用和例子

撤銷前一次 commit git revert head 撤銷前前一次 commit git revert head 撤回指定commit id 比如 git revert 0818badf6882ea2664a205bc8ef3a85425bb2537 git revert commit idre...

git revert和git reset的區別

原文 git revert 是生成乙個新的提交來撤銷某次提交,此次提交之前的commit都會被保留 git reset 是回到某次提交,提交及之前的commit都會被保留,但是此次之後的修改都會被退回到暫存區 具體乙個例子,假設有三個commit,git st commit3 add test3.c...

git revert和git reset的區別

git revert 是撤銷某次操作,此次操作之前的commit都會被保留 git reset 是撤銷某次提交,但是此次之後的修改都會被退回到暫存區 具體乙個例子,假設有三個commit,git st commit3 add test3.c commit2 add test2.c commit1 a...