英文:
Git cancel everything made today
问题
我本想执行git rebase
,但因需要先检查某事,所以取消了。不知何故,我的Git历史搞乱了。
我尝试使用git reset --hard HEAD@{2}
来取消,但(在使用git reflog
检查后)它没有生效。于是我决定查找解决方法,按照以下步骤操作:
-
git fetch origin master
-
git checkout -f master origin/master
这个命令失败了,所以我没有继续执行下去。现在我的git历史更加混乱,我不知道如何修复它。我只是想回到2小时前的状态。
英文:
I wanted to do a git rebase
, but I canceled it because I needed to check something first. Somehow I messed up my Git history.
I tried to cancel that using git reset --hard HEAD@{2}
(after checking with git reflog
, but it didn't work, so I decided to google it and I followed the instructions:
-
git fetch origin master
-
git checkout -f master origin/master
The command failed so I didn't go further. Now my git history is even more messed up and I have no idea how to fix it. I just want to get back to work.
How do I go back to how it was 2h ago?
答案1
得分: 1
Sure, here is the translated text:
尽管 `git reset --hard` 可能会使你回到你想要的地方,请注意这是一个“破坏性”的命令,可能会让你丢失你想要保留的内容。这是一个应该受到高度尊重的命令,只有在绝对必要的情况下才应该使用。
我建议采取不同的方法。使用reflog并为最新的N个条目创建一些临时标签,然后检查它们,找到实际匹配你想要的内容的标签。这是一种非破坏性的方法,它使你能够选择检出任何潜在的候选项并进行验证。
要检查最近的20个ref-log条目,请运行
```shell
git reflog | head -20 | sort -u | awk '{print "git tag reflog-tag-" $1 " " $1}' | bash
# 例如
# git tag reflog-tag-0defc84 0defc84
# git tag reflog-tag-2100d5e 2100d5e
# git tag reflog-tag-287e250 287e250
# ...
然后启动 gitk --all &
查看提交记录。
当你找到了你想要的分支末端的提交时,你可以对该提交运行一个硬重置。要移除所有临时的reflog标签,请运行 git tag -l | grep ^reflog-tag- | xargs git tag -d
。
<details>
<summary>英文:</summary>
While `git reset --hard` **might** get you back to where you want to be notice that this is a "destructive" command that potentially makes you lose stuff you want. It is a command that should be treated with great respect and only used when strictly necessary.
I would suggest a different approach. Take the reflog and just create some temporary tags for the latest N entries, and then examine them and find then one that actually matches what you want. This is a non-destructive approach that leaves you the option to check out any of the potential candidates and verify.
To examine the last 20 ref-log entries run
```shell
git reflog | head -20 | sort -u | awk '{print "git tag reflog-tag-" $1 " " $1}' | bash
# E.g.
# git tag reflog-tag-0defc84 0defc84
# git tag reflog-tag-2100d5e 2100d5e
# git tag reflog-tag-287e250 287e250
# ...
and then start gitk --all &
to look at the commits.
When you have found the commit that is the tip of where you want your branch to be, then you can run a hard reset against that commit. To remove all the temporary reflog tags run git tag -l | grep ^reflog-tag- | xargs git tag -d
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论