在git docs(以及众多SO线程)中,建议使用此重置方法:
$ git reset --soft HEAD^ ;# go back to WIP state <2>
$ git reset <3>
0.2。这将从提交历史记录中删除WIP提交,并将工作树设置为创建快照之前的状态。
0.3。此时,索引文件仍包含您作为快照WIP提交的所有WIP更改。这将更新索引以将您的WIP文件显示为未提交。
https://www.kernel.org/pub/software/scm/git/docs/git-reset.html
显然这很好,但似乎这两个命令可以替换为
$ git reset HEAD^
(相当于)
$ git reset --mixed HEAD^
应该将HEAD指针和索引重置为先前的提交。实际上这个命令和前两个命令的结果有什么区别吗?如果没有,是否有理由更喜欢两阶段流程?或者它只是在文档中以这种方式完成,以明确说明--soft?
的行为答案 0 :(得分:5)
不,似乎没有任何区别。
它更多地用于说明git reset --soft
(即仅移动HEAD,可以have other more practical uses)
git reset HEAD
用于“取消暂停”,简单的git reset HEAD^
同时执行(移动HEAD和非舞台,不需要--mixed
,因为它是默认选项)
这是一个快速测试,看看它是什么样的:
之前(您只需切换回功能,您提交了“wip
” - 正在进行中的工作):
C:\Users\VonC\prog\git\test\r\r3>gl
* 6ac95bd - (origin/master, origin/HEAD, master) fix in master (2 minutes ago) <VonC>
| * fd8d97d - (HEAD, origin/feature, feature) snap WIP (3 minutes ago) <VonC>
| * 16066dd - f1 (3 minutes ago) <VonC>
|/
* e8ad96f - f1 (3 minutes ago) <VonC>
重置本身:
C:\Users\VonC\prog\git\test\r\r3>git reset "HEAD^"
Unstaged changes after reset:
M f
这会给你状态:
C:\Users\VonC\prog\git\test\r\r3>git st
# On branch feature
# Your branch is behind 'origin/feature' by 1 commit, and can be fast-forwarded.
# (use "git pull" to update your local branch)
#
# 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: f
#
no changes added to commit (use "git add" and/or "git commit -a")
在git reset HEAD^
C:\Users\VonC\prog\git\test\r\r3>gl
* 6ac95bd - (origin/master, origin/HEAD, master) fix in master (6 minutes ago) <VonC>
| * fd8d97d - (origin/feature) snap WIP (7 minutes ago) <VonC>
| * 16066dd - (HEAD, feature) f1 (7 minutes ago) <VonC>
|/
* e8ad96f - f1 (8 minutes ago) <VonC>
在两个步骤中,您会在git reset --soft HEAD^
:
C:\Users\VonC\prog\git\test\r\r2>gl
* 6ac95bd - (origin/master, origin/HEAD, master) fix in master (65 seconds ago) <VonC>
| * fd8d97d - (origin/feature) snap WIP (89 seconds ago) <VonC>
| * 16066dd - (HEAD, feature) f1 (2 minutes ago) <VonC>
|/
* e8ad96f - f1 (2 minutes ago) <VonC>
您的索引仍然会反映wip
的演出内容:
C:\Users\VonC\prog\git\test\r\r2>git st
# On branch feature
# Your branch is behind 'origin/feature' by 1 commit, and can be fast-forwarded.
# (use "git pull" to update your local branch)
#
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: f
#
然后重置会被取消,让你回到同一阶段而不是git reset HEAD^
一步到位:
C:\Users\VonC\prog\git\test\r\r2>git reset
Unstaged changes after reset:
M f
C:\Users\VonC\prog\git\test\r\r2>git st
# On branch feature
# Your branch is behind 'origin/feature' by 1 commit, and can be fast-forwarded.
# (use "git pull" to update your local branch)
#
# 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: f
#
no changes added to commit (use "git add" and/or "git commit -a")