如何调和独立的头目和原产地/主人?已经签出旧哈希 [英] How to reconcile detached head and origin/master? Already checked out old hash

查看:131
本文介绍了如何调和独立的头目和原产地/主人?已经签出旧哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简介和搜索

所以我认为我犯了一个严重错误,对此我感到担忧.我已经解析了负责人的论坛,并且我几乎已经找到答案了,但是这些场景太具体了,不适用于我所在的位置.如果您找到一个可以回答我问题的特定主题,请链接我.

So I think I have made a grave mistake and I am concerned. I have parsed the forums for detached heads and I have come close to finding an answer, but the scenarios are too specific and not applicable to where I am at. If you find a specific thread that answers my question, please do link me.

例如:如何将分离的HEAD与master协调/origin?

那家伙已经完成了git rebase -i,而我还没有.我曾尝试过重新定基,但是并不能解决我的问题.

The guy has already done git rebase -i, which I have not. I've tried rebasing, but it didn't solve my issue.

背景很少:

  • 我是git的新手;我只能自己使用git
  • 我拥有远程存储库
  • 需要一套旧代码,这是时间敏感的问题
  • 我捡起一点程序,并在没有其他人编码的工作场所中使用它(所以我不太愿意寻求帮助)

问题:

要访问具有工作代码的旧提交,我做了(可能是错误的事情):

To access the old commit with working code, I did (probably the wrong thing):

git log
git checkout <hash-of-desired-old-commit>

我继续完成代码.感谢版本控制!但是射击,为什么git这么复杂?如何保存这项工作?我现在头昏脑胀. Origin/Master是将来的某些提交,最好将其全部删除.

I proceeded to finish the code. Thank goodness for version control! But shoot, why is git so complicated? How do I save this work? I am now in a detached head. Origin/Master is in some future commits that preferably should all be deleted.

我尝试过的事情:

  • 将整个文件夹复制并粘贴到其他地方只是为了备份
  • git add -A后跟git commit -m "oh shit, detached head commit"git rebase master
  • git reset --hard HEAD~3此处的建议:检出旧提交和使其成为新提交
  • copied and pasted the entire folder elsewhere just for back-up
  • git add -A followed with git commit -m "oh shit, detached head commit" and git rebase master
  • git reset --hard HEAD~3 per recommendation here: Checkout old commit and make it a new commit

问题:我如何调和我的头颅与出身/主人?核对后,是否需要git push --force或其他内容(特别是使用--force标志)?这个独立的头上有工作,而且我读到,我等待的时间越长,垃圾收集可能会进来并吞噬丢失的分支.抱歉,我确实没有接受过git的任何正式培训...我几乎准备好签出Origin Master,然后重写代码.

Question: How do I reconcile my detached head with origin/master? Once reconciled, do I need to git push --force or something (specifically with the --force flag)? There is work at this detached head, and I've read that the longer I wait, garbage collection may come and eat my lost branch. Sorry, I do lack any formal training in git... I am almost ready to just check out origin master and then rewrite the code.

谢谢.

推荐答案

如果您位于独立的HEAD上,并且想要保存HEAD当前处于的任何状态,只需创建一个分支:

If you are on a detached HEAD and you want to save whatever state that HEAD is currently in, just create a branch:

git branch save-this-head

现在,您有一个分支指向该完全分离的HEAD,因此您可以安全地进行检查,而不会丢失任何东西:

Now, you have a branch pointing to that exact detached HEAD, so you can check that out safely without losing stuff:

git checkout save-this-head

现在,您不再处于分离的HEAD状态.您和您的更改都是安全的!如果您有任何待处理的更改,请立即提交,以便我们可以自由移动.

And now, you are no longer in a detached HEAD state. You and your changes are safe! If you have any pending changes, you should commit those now, so we can move around freely.

然后,您可以将此新分支重新建立到其他分支上,例如主分支:

Afterwards, you can just rebase this new branch onto something else, e.g. the master branch:

git rebase master

那应该在它上面重播master上缺少的提交.因此,将恢复自分离的HEAD以来所做的更改.

That should replay the commits that are missing on master on top of it. So changes made since the detached HEAD will be recovered.

如果您只是想保持在save-this-head状态并丢弃先前的主服务器,则只需签出主服务器,然后将其重置为保存状态即可:

If you instead just want to stay on that save-this-head state, and throw away the previous master, you can just checkout master and then reset it to the saves state:

git checkout master
git reset --hard save-this-head

这将使master等效于save-this-head,并丢弃master上的所有其他更改.如果要推送此消息,则可能必须先强制​​推送,然后才能从远程删除提交.

This will make master equivalent to save-this-head, throwing away all other changes on master. If you want to push this, you will likely have to force push then in order to remove the commits from the remote.

我读到,我等待的时间越长,垃圾收集可能会进来并吞噬丢失的分支

I've read that the longer I wait, garbage collection may come and eat my lost branch

首先,只要您保持该状态,HEAD指针将一直查看该提交对象(这就是为什么它首先被称为分离式HEAD"的原因).这样可以防止任何垃圾回收删除该提交对象(及其所有父对象).

First of all, as long as you stay in that state, the HEAD will pointer will keep looking at that commit object (which is why it’s called "detached HEAD" in the first place). This will prevent any garbage collection from removing that commit object (and all its parents).

即使您不小心离开了分离的HEAD状态,而使提交被引用为悬挂" 提交,Git也会将对象保留多个星期,甚至考虑允许该对象成为垃圾对象集.仅仅因为它要进行垃圾回收并不意味着GC会真正运行,尤其是当您不与存储库进行交互时.

Even if you accidentally left the detached HEAD state, making the commit you were referring a "dangling" commit, Git will keep the objects around for multiple weeks before even considering to allow the object being garbage collected. And just because it is subject to garbage collection that does not mean that the GC will actually run, especially when you otherwise don’t interact with the repository.

顺便说一句.将来,只要您想返回历史记录中的某个提交并希望(不仅仅是看),都应该创建一个分支.即使只是暂时的,拥有它也可以让您更自由地走动,并且不会因为分离的HEAD状态而沮丧于您做任何事情:

Btw. in the future, whenever you want to go back to a commit in your history and expect to do something (other than just looking), you should just create a branch. Even if it’s just a temporary one, having it will allow you to move around more freely and you won’t be discouraged from doing anything by that detached HEAD state:

git checkout -b fix-something <some-old-hash>

这篇关于如何调和独立的头目和原产地/主人?已经签出旧哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆