【问题标题】:What would be the difference between `git branch -f master HEAD~` and `git reset HEAD~`?`git branch -f master HEAD~` 和 `git reset HEAD~` 有什么区别?
【发布时间】:2018-06-25 09:51:09
【问题描述】:

真的看标题。

假设我们已经在 master 分支上:

git branch -f master HEAD~git reset HEAD~ 之间有什么区别?

据我所知,这两个命令都将分支/HEAD 指针在提交链中向上移动,但这两者之间还有其他值得注意的区别吗?

【问题讨论】:

  • 在第一种情况下,您将保留在当前分支上,而在第二种情况下,您将切换到master
  • 假设我们已经在 master 上。编辑 OP 以澄清。
  • fatal: Cannot force update the current branch.
  • git branch -f master HEAD~ 让我fatal: Cannot force update the current branch.

标签: git version-control git-branch git-checkout


【解决方案1】:

假设master是当前分支,git branch -f master HEAD~拒绝做任何事情并报告错误“致命:无法强制更新当前分支。”

如果您在不同的分支上,git branch -f master HEAD~ 会将分支 master 移动到当前分支的第一个父级上,并且不会影响当前分支、索引或工作树。

git reset HEAD~git reset --mixed HEAD~ 相同。它将当前分支移到其第一个父级上,更新索引以匹配分支的新位置,并且不影响工作树。

如果git branch -f master HEAD~master是当前分支时起作用,其效果与git reset --soft HEAD~相同。

如您所见,这两个命令有很多不同之处。


OP 在评论中询问:“我也知道混合是重置的默认设置,但也有软和硬,但我不知道区别。”

场景:

  1. git checkout master
  2. 修改file1
  3. git add file1
  4. git commit

在此步骤之后:

  • git reset --soft HEAD~1 仅移动上次提交之前的 master 分支;它将 repo 恢复到第 3 步之后的状态;
  • git reset --mixed HEAD~1 移动分支并更新索引以匹配它;它将 repo 带到第 2 步之后的状态;
  • git reset --hard HEAD~1 移动分支然后更新索引和工作树以匹配它;它将 repo 带到第 1 步之后的状态。

当然,这是一个简化的解释,“将 repo 带入状态” 部分仅适用于这个简化的场景。如果您 git reset 进行不同的提交,则只保留有关分支、索引和工作树发生情况的说明,并弄清楚在 git reset 命令的每种风格之后 repo 的外观。

【讨论】:

  • It moves the current branch on its first parent, updates the index to match the new position of the branch and does not affect the working tree. 我不明白这是什么意思。更新索引但不更新工作树?
  • 我也知道mixed 是重置的默认值,但也有softhard 但我不知道它们的区别。
  • 所以branch 隐含softreset 隐含mixed?
  • @user9225276 在git branch 上没有soft。你问有什么不同。 git reset --soft 只移动分支,不影响索引和工作树。 git checkout -f 也只移动分支,不影响索引和工作树。这就是他们的相似之处。 git reset 在当前分支上运行。 git branch -f 拒绝在当前分支上工作。而git branch 没有soft/hard/mixed。阅读git branchgit reset。他们做不同的事情。
猜你喜欢
  • 2021-07-04
  • 2011-09-06
  • 2023-03-23
  • 2014-08-25
  • 2021-10-28
  • 2017-12-10
  • 2020-04-07
  • 2011-01-14
相关资源
最近更新 更多