6

在 git 文档(和许多 SO 线程)中,建议使用这种重置方法:

$ git reset --soft HEAD^ ;# go back to WIP state  <2>
$ git reset                                       <3>

.2. 这会从提交历史记录中删除 WIP 提交,并将工作树设置为创建快照之前的状态。

.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的行为?

4

1 回答 1

5

不,似乎没有任何区别。
更多的是说明git reset --soft(即仅移动HEAD,它可以有其他更实际的用途

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")
于 2013-07-20T14:21:45.327 回答