Git: 如何将HEAD还原到最近的提交?

huangapple go评论92阅读模式
英文:

Git: How do I remove HEAD back to most recent commit?

问题

作为 Git 初学者,我不知何故搞糟了我的仓库,现在看起来是这样的:

  1. PS C:\GitRepositories> git log --oneline
  2. 158e4c0 (HEAD -> 24d309, origin/24d309) Comment6
  3. b4c156a Comment5
  4. 87bd801 Comment4
  5. b24d309 (master) Comment3
  6. f1ec7ee (origin/master, origin/HEAD) Comment2
  7. 6ce6a10 Comment1

现在,当我推送时,推送的是 Comment2 而不是最新的 Comment6。有人可以帮助我吗?

英文:

Being a beginner in git, I somehow managed to spoil my repo in the way that it looks like that now:

  1. PS C:\GitRepositories> git log --oneline
  2. 158e4c0 (HEAD -> 24d309, origin/24d309) Comment6
  3. b4c156a Comment5
  4. 87bd801 Comment4
  5. b24d309 (master) Comment3
  6. f1ec7ee (origin/master, origin/HEAD) Comment2
  7. 6ce6a10 Comment1

Now, when I push the version of Comment2 is pushed, not the most recent one (Comment6). Could somebody help me?

答案1

得分: 3

blind guess:

在某个时候,你尝试将你的 master 分支切换到提交 b24d309 的状态,
并且你尝试通过运行 git checkout -b b24d309 来实现这一目标(顺便说一句,你添加了一个拼写错误,但在这种情况下问题实际上是使用了 -b)。

如果你使用 -b <something>git 将理解你的意图是创建一个分支,使用 <something> 作为分支名称。
如果 <something> 看起来像一个提交哈希(比如:24d309b24d309),那么你将得到一个分支,其名称看起来像一个哈希值...


如果你的意图是:

  • master 作为你的活动分支
  • master 推进到 "Comment6"
  • 更新 origin/master 到相同的状态
  • 删除名为 24d309 的分支的存在(本地和远程)

你可以执行以下操作:

  1. git switch master
  2. git merge --ff-only 158e4c0
  3. git push origin master
  4. # 删除本地和远程分支:
  5. git branch -D 24d309 # -D(大写 D)是 -d 的 "强制" 选项
  6. # 有关更多详细信息,请参阅 'git help branch'
  7. git push origin -d 24d309
英文:

blind guess :

at some point, you tried to reach the state of your master branch (commit b24d309),
and you tried to do so by running git checkout -b b24d309 (incidentally, you added a typo, but the issue in this case is actually using -b).

If you use -b <something> : git will understand that your intention is to create a branch, using <something> as its branchname.
If <something> happens to look like a commit hash (say: 24d309 or b24d309), well, you will end up with a branch which has a name that looks like a hash ...


If your intention is :

  • to have master as your active branch
  • to move master forward to "Comment6"
  • to update origin/master to the same state
  • to remove the existence of the ill named 24d309 branch (both local and remote)

you can do the following:

  1. git switch master
  2. git merge --ff-only 158e4c0
  3. git push origin master
  4. # remove local and remote branches:
  5. git branch -D 24d309 # -D (upper D) is the "forced" option for -d
  6. # see 'git help branch' for more details
  7. git push origin -d 24d309

huangapple
  • 本文由 发表于 2023年2月16日 17:34:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75470295.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定