简体   繁体   English

如何以分离头模式将文件提交到分支

[英]How to commit files in detached head mode to a branch

I ended up in a weird situation where I checkout a previous commit on my branch v4.1.0-rc12.我最终遇到了一个奇怪的情况,我在我的分支 v4.1.0-rc12 上检查了以前的提交。 I've modified some files around and I want to commit it to the v4.1.0-rc12 branch.我已经修改了一些文件,我想将它提交到 v4.1.0-rc12 分支。

Now that HEAD pointer is in detached mode since I checked out a previous commit, how do I commit this changes to v4.1.0-rc12 branch in detached mode?现在 HEAD 指针在我签出之前的提交后处于分离模式,我如何在分离模式下将这些更改提交到 v4.1.0-rc12 分支?

I wish I had done "revert" instead of "checkout" the commit hash.我希望我已经完成了“还原”而不是“签出”提交哈希。

Is there any possible solution to commit the edited files and add it to my v4.1.0-rc12 branch?是否有任何可能的解决方案来提交编辑的文件并将其添加到我的 v4.1.0-rc12 分支?

This is what my "git reflog" shows这就是我的“git reflog”显示的

 441bfac HEAD@{0}: commit: Resolved #110
 dc00e4f HEAD@{1}: checkout: moving from v4.1.0-rc12 to dc00e4f
 2542748 HEAD@{2}: commit: Login & SignUp revamp (Broken)
 dc00e4f HEAD@{3}: commit: Resolved #109 and #110
 4472914 HEAD@{4}: commit: Resolved #108

I want to save 441bfac to v4.1.0-rc12我想将 441bfac 保存到 v4.1.0-rc12

Any help or notion would be appreciated.任何帮助或想法将不胜感激。

First, create new branch from your previous commit, checkout to newly created branch, update your files and than commit the changes.首先,从您之前的提交创建新分支,签出新创建的分支,更新您的文件,然后提交更改。 After that you can merge your changes to any branch.之后,您可以将更改合并到任何分支。

Method 1:方法一:

$ git checkout v4.1.0-rc12 branch
(see if it succeeds, if so):
$ git add ...     # if / as needed
$ git commit

This might not work, because the git checkout step will refuse to switch to the branch, if (and only if) said switching will wipe out the changes you've already made.可能不起作用,因为git checkout步骤将拒绝切换到分支,如果(且仅当)所述切换将清除您已经进行的更改。

The git checkout step will succeed , and carry your changes along, if it can.如果可以的话, git checkout步骤将成功,并进行您的更改。

If the git checkout step succeeds, this is by far the easiest way to deal with it all.如果git checkout步骤成功,这是迄今为止处理这一切的最简单方法。 If not, proceed to alternate methods.如果没有,请继续使用其他方法。


Method 2 ( avi's answer ):方法2( avi的回答):

$ git checkout -b tempbranch
$ git add ...      # as and if needed
$ git commit

This (the -b tempbranch ) creates a new, temporary branch pointing to the commit at which you detached your HEAD earlier.这( -b tempbranch )创建一个新的临时分支,指向您之前分离 HEAD 的提交。 The git commit then creates a new commit on this temporary branch:然后git commit在这个临时分支上创建一个新的提交:

          o--o--o   <-- v4.1.0-rc12 branch
         /
...--o--o
         \
          o         <-- tempbranch

Now you can git checkout whatever you like, and the commit you made on tempbranch is permanently stored in your repository (well, permanent unless/until you delete the name tempbranch ), which enables you to copy stuff from it whenever you want.现在您可以git checkout任何您喜欢的内容,并且您在tempbranch上所做的提交将永久存储在您的存储库中(嗯,永久存储,除非/直到您删除名称tempbranch ),这使您可以随时从中复制内容。


Method 3:方法三:

$ git stash
$ git checkout v4.1.0-rc12 branch
$ git stash apply    # or even "git stash pop" but I prefer "apply"
... check everything out ...
... if it all looks right ...
$ git stash drop     # "git stash pop" means "apply and then auto-drop"

What git stash does is make a commit (well, actually two commits) that are not on any branch. git stash所做的是进行不在任何分支上的提交(实际上是两次提交)。 The effect is the same as when you make the temporary branch.效果与制作临时分支时相同。 That leaves the problem of deleting the non-branch when you're done with it, so git stash drop deletes the unnamed non-branch that holds the commits.这留下了在完成后删除非分支的问题,因此git stash drop删除了保存提交的未命名的非分支。

The git stash apply step essentially (though it's more complicated than this) cherry-picks the commit that git stash made, wherever you are now: so this is like cherry-picking the commit you made on tempbranch . git stash apply步骤本质上(尽管它比这更复杂)cherry-picks git stash所做的提交,无论你现在在哪里:所以这就像cherry-picking 你在tempbranch上所做的提交。

Note that git stash pop automatically invokes git stash drop , even if the result of the git stash apply is a mess, you've lost your temporary commits if you pop but not if you apply .请注意, git stash pop会自动调用git stash drop ,即使git stash apply的结果是一团糟,如果你pop但你不会丢失临时提交,但如果你apply If everything goes well there's no real difference and pop is more convenient;如果一切顺利,则没有真正的区别,并且pop更方便; this is just a case of leaving a better bread-crumb trail in case things go bad.这只是留下更好 的面​​包屑痕迹以防万一事情变糟的情况。


(My personal preference, incidentally, is for method 1 first if possible, then usually 2, then 3, but sometimes—when I am sure it's going to work—I'll prefer method 3 to method 2.) (顺便说一句,我个人的偏好是,如果可能的话,首先是方法 1,然后通常是 2,然后是 3,但有时——当我确定它会起作用时——我会更喜欢方法 3 而不是方法 2。)

I had a similar situation, and I solved in a way that, in your case, would be:我有类似的情况,我以一种方式解决了,在你的情况下,将是:

git checkout v4.1.0-rc12
git merge 441bfac

General case:一般情况:

git checkout branch-to-have-the-changes
git merge changes-that-we-previously-made-id

If your new changes are committed to a detached HEAD, and you want v4.1.0-rc12 to point to that same commit also:如果您的新更改已提交到分离的 HEAD,并且您希望 v4.1.0-rc12 也指向相同的提交:

git branch -f v4.1.0-rc12 HEAD
git checkout v4.1.0-rc12

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM