英文:
Git: How do I remove HEAD back to most recent commit?
问题
作为 Git 初学者,我不知何故搞糟了我的仓库,现在看起来是这样的:
PS C:\GitRepositories> git log --oneline
158e4c0 (HEAD -> 24d309, origin/24d309) Comment6
b4c156a Comment5
87bd801 Comment4
b24d309 (master) Comment3
f1ec7ee (origin/master, origin/HEAD) Comment2
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:
PS C:\GitRepositories> git log --oneline
158e4c0 (HEAD -> 24d309, origin/24d309) Comment6
b4c156a Comment5
87bd801 Comment4
b24d309 (master) Comment3
f1ec7ee (origin/master, origin/HEAD) Comment2
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> 看起来像一个提交哈希(比如:24d309 或 b24d309),那么你将得到一个分支,其名称看起来像一个哈希值...
如果你的意图是:
- 将 
master作为你的活动分支 - 将 
master推进到 "Comment6" - 更新 
origin/master到相同的状态 - 删除名为 
24d309的分支的存在(本地和远程) 
你可以执行以下操作:
git switch master
git merge --ff-only 158e4c0
git push origin master
# 删除本地和远程分支:
git branch -D 24d309    # -D(大写 D)是 -d 的 "强制" 选项
                        # 有关更多详细信息,请参阅 'git help branch'
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 
masteras your active branch - to move 
masterforward to "Comment6" - to update 
origin/masterto the same state - to remove the existence of the ill named 
24d309branch (both local and remote) 
you can do the following:
git switch master
git merge --ff-only 158e4c0
git push origin master
# remove local and remote branches:
git branch -D 24d309    # -D (upper D) is the "forced" option for -d
                        # see 'git help branch' for more details
git push origin -d 24d309
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论