运行 (git reset head~2) 等于运行 (git reset head^) 两次吗?
Is running (git reset head~2) equivalent to running (git reset head^) twice?
如果我在本地分支上有两次提交,我 运行 git reset head^
两次。它只会撤消这两个提交,还是我 运行 会遇到问题?
是的,这是同一件事,您只需撤消这两个提交(但保留更改)。
请注意,您还可以 运行 git reset HEAD^^
git reset
默认为 --mixed
,它确实会撤消提交,将更改保留在本地树中 ("the changed files are preserved but not marked for commit")。
至于改版。虽然在您描述的场景中您会得到相同的行为,但您可能想说:HEAD~
和 运行 两次……或者更好 HEAD~~
或只是 HEAD~2
。
这并不少见,但简而言之:
~
遍历世代(沿着第一个 parents 的线更深)
^
遍历了上一代的parents(助记:回旋符看起来像一个小叉子,可以访问历史分叉的节点)
只要你不带任何数字使用它们,每次跳跃你总是得到最左边的祖先,所以HEAD~~~
和HEAD^^^
会让你落在同一个地方,HEAD~2
(最左边的两个 parent 后面)与 HEAD^2
不同(左起第二个 parent)。
您可以在 git rev-parse
描述中看到更多详细信息,包括此视觉插图:
Here is an illustration, by Jon Loeliger. Both commit nodes B and C are parents of commit node A. Parent commits are ordered left-to-right.
G H I J
\ / \ /
D E F
\ | / \
\ | / |
\|/ |
B C
\ /
\ /
A
A = = A^0
B = A^ = A^1 = A~1
C = A^2 = A^2
D = A^^ = A^1^1 = A~2
E = B^2 = A^^2
F = B^3 = A^^3
G = A^^^ = A^1^1^1 = A~3
H = D^2 = B^^2 = A^^^2 = A~2^2
I = F^ = B^3^ = A^^3^
J = F^2 = B^3^2 = A^^3^2
此外,如果您担心执行某项操作,您可以随时分支(例如 git checkout -b temp
),尝试您的命令,当您满意时切换回来,删除临时分支并在那里执行它。
如果我在本地分支上有两次提交,我 运行 git reset head^
两次。它只会撤消这两个提交,还是我 运行 会遇到问题?
是的,这是同一件事,您只需撤消这两个提交(但保留更改)。
请注意,您还可以 运行 git reset HEAD^^
git reset
默认为 --mixed
,它确实会撤消提交,将更改保留在本地树中 ("the changed files are preserved but not marked for commit")。
至于改版。虽然在您描述的场景中您会得到相同的行为,但您可能想说:HEAD~
和 运行 两次……或者更好 HEAD~~
或只是 HEAD~2
。
这并不少见,但简而言之:
~
遍历世代(沿着第一个 parents 的线更深)^
遍历了上一代的parents(助记:回旋符看起来像一个小叉子,可以访问历史分叉的节点)
只要你不带任何数字使用它们,每次跳跃你总是得到最左边的祖先,所以HEAD~~~
和HEAD^^^
会让你落在同一个地方,HEAD~2
(最左边的两个 parent 后面)与 HEAD^2
不同(左起第二个 parent)。
您可以在 git rev-parse
描述中看到更多详细信息,包括此视觉插图:
Here is an illustration, by Jon Loeliger. Both commit nodes B and C are parents of commit node A. Parent commits are ordered left-to-right.
G H I J \ / \ / D E F \ | / \ \ | / | \|/ | B C \ / \ / A A = = A^0 B = A^ = A^1 = A~1 C = A^2 = A^2 D = A^^ = A^1^1 = A~2 E = B^2 = A^^2 F = B^3 = A^^3 G = A^^^ = A^1^1^1 = A~3 H = D^2 = B^^2 = A^^^2 = A~2^2 I = F^ = B^3^ = A^^3^ J = F^2 = B^3^2 = A^^3^2
此外,如果您担心执行某项操作,您可以随时分支(例如 git checkout -b temp
),尝试您的命令,当您满意时切换回来,删除临时分支并在那里执行它。