新的提交不在头上
长话短说*,我用我的 git 存储库做了一些我不知道该怎么做的事情,并最终分离了我的头,也就是说,当我提交 master
不是最新的时。
我该如何解决这个问题?我必须合并吗? (如果是这样,我想运行的确切命令是什么?)
当我运行 gitbranch 时,它告诉我我目前不在分支上
$ git branch
* (no branch)
master
- 更长的故事是我试图撤消一些提交,但不知道我应该如何做这样做。我想那并不太长。
Long story short*, I did some things with my git repository that I don't know how to do, and ended up detaching my head, that is, when I commit master
isn't up to date.
How can I fix this? Do I have to merge? (If so, what are the exact commands I want to run?)
When I run git branch, it tells me I'm currently on no branch
$ git branch
* (no branch)
master
- Longer story is that I tried to undo some commits and had no idea how I was supposed to do that. I guess that wasn't too long.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最简单(安全)的方法是创建一个指向您的 HEAD 的临时分支,签出 master,合并临时分支,然后删除它:
您也可以避免使用临时分支,而只需使用 reflog 从 no 获取提交分支:
HEAD@{1}
这里指的是您签出的上一个提交;HEAD@{0}
与HEAD
相同,即您正在处理的当前提交。如果您想查看HEAD
指向的所有先前提交(尚未过期),您可以这样做:您说发生这种情况是因为您试图撤消提交。正如 mletterle 指出的,您可以使用
git reset
。git reset --hard rev
会将您所在的分支、HEAD、索引和工作副本设置为特定修订版。请注意,这会消除未提交的更改,因此只有在您真正知道自己在做什么时才这样做。通常更安全的做法是执行git reset rev
,这不会影响您的工作副本,然后使用git checkout< 有选择地恢复工作副本中的各个文件/code>,以确保您不会意外破坏您正在做的某些工作。
此外,
git reset
通常应该仅用于未推送的提交。如果您试图撤消已经推送的内容,并且其他人已经合并,您通常需要git 恢复
。Easiest (safe) way would be to create a temporary branch pointing to your HEAD, checkout master, merge the temporary branch, and then delete it:
You could also avoid using the temp branch, and just use the reflog to get the commits from no branch:
HEAD@{1}
here refers to the previous commit that you had checked out;HEAD@{0}
is the same asHEAD
, the current commit that you are working from. If you want to see all of the previous commits thatHEAD
has pointed to (that haven't yet expired), you can do:You said that this happened because you were trying to undo commits. As mletterle points out, you can do this with a
git reset
.git reset --hard rev
will set the branch you're on, HEAD, your index, and your working copy to a particular revision. Note that this will blow away uncommitted changes, so only do it if you really know what you're doing. Safer would generally be to dogit reset rev
, which doesn't affect your working copy, and then selectively revert individual files in your working copy withgit checkout
, in order to ensure that you don't accidentally destroy some work you were doing.Also,
git reset
should generally only be used only on unpushed commits. If you are trying to undo something that has already been pushed, and other people have already merged, you generally wantgit revert
.有几种方法可以让你最终进入(没有分支),但本质上它是一个用于检查事物的垃圾分支,例如旧的提交。如果您错误地编辑了(无分支)上的文件,您可以切换回 master,就像从任何其他进行更改的分支切换一样。查看 git-checkout 的帮助,但您需要执行类似 git-checkout -m 之类的操作来移回 master 并将当前索引与其合并。
There are few ways you can end up on (no branch), but essentially its a junk branch used for examining things, e.g. old commits. If you mistakenly edited files on (no branch), you can switch back to master just as you would from any other branch with changes. Check out the help for git-checkout, but you're going to want to do something like
git-checkout -m
to move back to master and merge your current index along with it.我喜欢布莱恩·坎贝尔的回答。至于撤消提交, git reset --hard rev 应该是您所寻找的。 http://git-scm.com/docs/git-reset
I like Brian Campbell's answer. As for undoing commits, git reset --hard rev should be what you were looking for. http://git-scm.com/docs/git-reset
也许你应该回到你正在工作的分支。如果是师父那就是
Maybe you should just go back to the branch you were working on. If it was master it would be