git 如何处理在分离头中进行的提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7124486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:37:54  来源:igfitidea点击:

What to do with commit made in a detached head

gitgit-checkout

提问by benzen

Using git I made something like this

使用 git 我做了这样的事情

git clone
git checkout {a rev number tree rev before} (here I started to be in a detached head state)
//hacking
git commit
//hacking
git commit
(some commit where made on origin/master)
git pull (which does complete because there was some error due to the fact that I'm no more on master)

Because it said to me that I can still commit when in a detached head state, I did so. But now I want to like merge my detached head branch and my local master branch, and then push my bunch of changes to origin/master.

因为它告诉我,在超脱的头部状态下我仍然可以承诺,所以我这样做了。但是现在我想合并我的分离头分支和我的本地主分支,然后将我的一堆更改推送到 origin/master。

So my question is how could I merge the master branch with my actual state (detached head)

所以我的问题是如何将主分支与我的实际状态(分离头)合并

回答by Ryan Stewart

Create a branch where you are, then switch to master and merge it:

在你所在的地方创建一个分支,然后切换到 master 并合并它:

git branch my-temporary-work
git checkout master
git merge my-temporary-work

回答by CB Bailey

You could do something like this.

你可以做这样的事情。

# Create temporary branch for your detached head
git branch tmp

# Go to master
git checkout master

# Merge in commits from previously detached head
git merge tmp

# Delete temporary branch
git branch -d tmp

Even simpler would be

更简单的是

git checkout master
git merge HEAD@{1}

but this has the slight danger that if you do make a mistake it can be a little harder to recover the commits made on the detached head.

但这有一个轻微的危险,如果你犯了一个错误,那么恢复在分离头上所做的提交可能会有点困难。

回答by Bhushan

This is what I did:

这就是我所做的:

Basically, think of the detached HEADas a new branch, without name. You can commit into this branch just like any other branch. Once you are done committing, you want to push it to the remote.

基本上,将其detached HEAD视为一个没有名称的新分支。您可以像任何其他分支一样提交到此分支。完成提交后,您想将其推送到远程。

So the first thing you need to do is give this detached HEADa name. You can easily do it like, while being on this detached HEAD:

所以你需要做的第一件事就是给它detached HEAD一个名字。您可以轻松地做到这一点,同时进行detached HEAD

git checkout -b some-new-branch

Now you can push it to remote like any other branch.

现在您可以像任何其他分支一样将其推送到远程。

In my case, I also wanted to fast-forward this branch to master along with the commits I made in the detached HEAD(now some-new-branch). All I did was

就我而言,我还想将这个分支与我在detached HEAD(现在some-new-branch)中所做的提交一起快进到 master 。我所做的只是

git checkout master

git pull # To make sure my local copy of master is up to date

git pull # To make sure my local copy of master is up to date

git checkout some-new-branch

git merge master // This added current state of master to my changes

git merge master // This added current state of master to my changes

Of course, I merged it later to master.

当然,我后来将它合并到master.

That's about it.

就是这样。

回答by Arnaud Le Blanc

You can just do git merge <commit-number>or git cherry-pick <commit> <commit> ...

你可以做git merge <commit-number>git cherry-pick <commit> <commit> ...

As suggested by Ryan Stewart you may also create a branch from the current HEAD:

根据 Ryan Stewart 的建议,您还可以从当前 HEAD 创建一个分支:

git branch brand-name

Or just a tag:

或者只是一个标签:

git tag tag-name

回答by Razan Paul

In case of detached HEAD, commits work like normal, except no named branch gets updated. To get master branch updated with your committed changes, make a temporary branch where you are (this way the temporary branch will have all the committed changes you have made in the detached HEAD) , then switch to the master branch and merge the temporary branch with the master.

在分离的 HEAD 的情况下,提交工作正常,除了没有命名分支被更新。要使用您提交的更改更新主分支,请在您所在的位置创建一个临时分支(这样临时分支将拥有您在分离的 HEAD 中所做的所有提交的更改),然后切换到主分支并将临时分支与大师。

git branch  temp
git checkout master
git merge temp

回答by Nesha Zoric

An easy fix is to just create a new branch for that commit and checkout to it: git checkout -b <branch-name> <commit-hash>.

一个简单的办法是只创建该提交一个新的分支,结帐吧:git checkout -b <branch-name> <commit-hash>

In this way, all the changes you made will be saved in that branch. In case you need to clean up your master branch from leftover commits be sure to run git reset --hard master.

这样,您所做的所有更改都将保存在该分支中。如果您需要从剩余的提交中清理主分支,请确保运行git reset --hard master.

With this, you will be rewriting your branches so be sure not to disturb anyone with these changes. Be sure to take a look at this article for a better illustration of detached HEADstate.

有了这个,您将重写您的分支,因此请确保不要因这些更改而打扰任何人。请务必查看这篇文章,以更好地说明 分离的 HEAD状态。

回答by rookie

Maybe not the best solution, (will rewrite history) but you could also do git reset --hard <hash of detached head commit>.

也许不是最好的解决方案,(将重写历史)但你也可以做git reset --hard <hash of detached head commit>.