Git reset的三種模式

2021-10-10 20:41:58 字數 4398 閱讀 2074

重置stage區和工作目錄(慎用)。

reset後面加--hard引數時,stage區和工作目錄裡的內容會被完全重置為和指定head位置相同的內容。也就是沒有commit的修改會被全部擦掉。執行後工作區、暫存區、版本庫保持一致(指定head版本)

administrator@win-dtnf3grdh5r mingw64 /g/tmp/test (master)

$ git status

on branch master

changes not staged for commit:

(use "git add ..." to update what will be committed)

(use "git restore ..." to discard changes in working directory)

modified: aa.txt

no changes added to commit (use "git add" and/or "git commit -a")

administrator@win-dtnf3grdh5r mingw64 /g/tmp/test (master)

$ git log

commit 79ed69f7fdeb3474c36fa111ee82ef067575ad83 (head -> master)

author: zhangtao153 <[email protected]>

date: wed nov 25 21:19:27 2020 +0800

bbcommit d40163567818b2ac0b28743058ff9c68a7798390

author: zhangtao153 <[email protected]>

date: wed nov 25 21:19:03 2020 +0800

aaadministrator@win-dtnf3grdh5r mingw64 /g/tmp/test (master)

$ git reset --hard head^

head is now at d401635 aa

administrator@win-dtnf3grdh5r mingw64 /g/tmp/test (master)

$ git log

commit d40163567818b2ac0b28743058ff9c68a7798390 (head -> master)

author: zhangtao153 <[email protected]>

date: wed nov 25 21:19:03 2020 +0800

aaadministrator@win-dtnf3grdh5r mingw64 /g/tmp/test (master)

$ git status

on branch master

nothing to commit, working tree clean

保留工作目錄和暫存區中未提交的內容,並把重置head所帶來的新的差異(回滾掉的commit較回滾後最新commit的修改)放進暫存區。

可以使用soft reset合併「當前節點」與「reset目標節點」之間不具太大意義的commit記錄,讓commit演進線圖較為清晰點。

【示例】工作區新增新檔案後reset 

administrator@win-dtnf3grdh5r mingw64 /g/tmp/test (master)

$ git log

commit 82ad83fcfcfb366ab7a895053f9b8936aaabf6cf (head -> master)

author: zhangtao153 <[email protected]>

date: wed nov 25 21:39:37 2020 +0800

bbcommit d40163567818b2ac0b28743058ff9c68a7798390

author: zhangtao153 <[email protected]>

date: wed nov 25 21:19:03 2020 +0800

aaadministrator@win-dtnf3grdh5r mingw64 /g/tmp/test (master)

$ git status

on branch master

untracked files:

(use "git add ..." to include in what will be committed)

bb.txt

nothing added to commit but untracked files present (use "git add" to track)

administrator@win-dtnf3grdh5r mingw64 /g/tmp/test (master)

$ git reset --soft head^

administrator@win-dtnf3grdh5r mingw64 /g/tmp/test (master)

$ git log

commit d40163567818b2ac0b28743058ff9c68a7798390 (head -> master)

author: zhangtao153 <[email protected]>

date: wed nov 25 21:19:03 2020 +0800

aaadministrator@win-dtnf3grdh5r mingw64 /g/tmp/test (master)

$ git status

on branch master

changes to be committed:

(use "git restore --staged ..." to unstage)

modified: aa.txt

untracked files:

(use "git add ..." to include in what will be committed)

bb.txt

工作區、暫存區未提交的內容以及由reset所導致的新差異,都會被放進工作區。簡而言之,就是「把所有差異都混合(mixed)放在工作目錄中」。

【例】工作區修改後新增到暫存區,在工作區新增新檔案後reset

administrator@win-dtnf3grdh5r mingw64 /g/tmp/test (master)

$ git status

on branch master

changes to be committed:

(use "git restore --staged ..." to unstage)

modified: aa.txt

untracked files:

(use "git add ..." to include in what will be committed)

bb.txt

administrator@win-dtnf3grdh5r mingw64 /g/tmp/test (master)

$ git reset head^

unstaged changes after reset:

m aa.txt

administrator@win-dtnf3grdh5r mingw64 /g/tmp/test (master)

$ git status

on branch master

changes not staged for commit:

(use "git add ..." to update what will be committed)

(use "git restore ..." to discard changes in working directory)

modified: aa.txt

untracked files:

(use "git add ..." to include in what will be committed)

bb.txt

no changes added to commit (use "git add" and/or "git commit -a")

git reset三種模式

區別 hard 重置位置的同時,直接將working tree工作目錄 index 暫存區及repository都重置成目標reset節點的內容,所以效果看起來等同於清空暫存區和工作區。soft 重置位置的同時,保留working tree工作目錄和index暫存區的內容,只讓repository中...

Git Reset三種模式

前言 在最近的工作中使用到git遇到的一些問題,對git的了解加深了一點。有時候,當我們commit 後,發現這一次commit的內容是有錯誤的,這個時候有兩種處理方法 1 修改錯誤內容,再一次commit 2 使用git reset命令撤銷這一次錯誤的commit 這兩種方法,第一種比較直接,但會...

git reset三種模式詳解

使用git reset命令可回退到指定的commit,本質其實就是將head指向分支的commit移動到了目標的commit上面 git reset命令有三種模式可供使用,分別是 git reset mixed 不加引數預設為此模式 git reset sort git reset hard 下面分...