gpt4 book ai didi

git - 为什么在 git reset (--mixed) 之前显式调用 git reset --soft

转载 作者:太空狗 更新时间:2023-10-29 13:12:19 43 4
gpt4 key购买 nike

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

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

.2. This removes the WIP commit from the commit history, and sets your working tree to the state just before you made that snapshot.

.3. At this point the index file still has all the WIP changes you committed as snapshot WIP. This updates the index to show your WIP files as uncommitted.

https://www.kernel.org/pub/software/scm/git/docs/git-reset.html

显然这很好,但似乎这两个命令可以替换为

$ git reset HEAD^

(相当于)

$ git reset --mixed HEAD^

这应该将 HEAD 指针和索引重置为之前的提交。这个命令的结果与前两个命令的结果实际上有区别吗?如果不是,是否有理由更喜欢两阶段过程?或者只是在文档中以这种方式明确说明 --soft 的行为?

最佳答案

不对,好像没什么区别。
更多的是为了说明 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>

您的索引仍会反射(reflect)为 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")

关于git - 为什么在 git reset (--mixed) 之前显式调用 git reset --soft,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17761896/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com