git 如何将master移动到HEAD?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26570242/
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
How to move master to HEAD?
提问by martin's
* [971f835] (HEAD, original_idea) Now working here. Some comment 9
* [692b673] Some comment 8
* [3ebff62] Reverted to original idea. Some comment 7
| * [72ea01d] (master) Decided it wasn't worth the effort. Some comment 6
| * [985c1ad] Some comment 5
| * [4d7d491] Some comment 4
| * [0c697bb] Branched to try an idea. Some comment 3
|/
* [7280b1f] Some comment 2
* [5c2c6d0] Some comment 1
* [bc7aac6] Initial commit
So, master
got left behind. Not sure how that happened. Once I decided I was done with the branch that didn't work out I checked out [7280b1f]
and continued from there.
所以,master
被抛在了后面。不知道那是怎么发生的。一旦我决定我完成了没有工作的分支,我就退房[7280b1f]
并从那里继续。
How do I fix this? I this the infamous "detached head" issue?
我该如何解决?我这是臭名昭著的“分离头”问题?
Does master
have to be aligned with HEAD
for optimal health of the repo?
是否master
必须与HEAD
repo 的最佳运行状况保持一致?
My workflow is super-simple. This is just managing a single text file. In the vast majority of cases it's:
我的工作流程非常简单。这只是管理单个文本文件。在绝大多数情况下,它是:
git add .
git commit -am "Some comment"
And that's it.
就是这样。
I decided to try an idea and created a branch. When it didn't work out I checked out the root of that branch and went back to the git add/commit
routine.
我决定尝试一个想法并创建一个分支。当它不起作用时,我检查了该分支的根并返回到git add/commit
例程。
采纳答案by Ed I
Is this the infamous "detached head" issue?
这是臭名昭著的“分离头”问题吗?
No, HEAD is pointing at branch original_idea
不,HEAD 指向分支original_idea
Does master have to be aligned with HEAD for optimal health of the repo?
master 是否必须与 HEAD 保持一致才能实现 repo 的最佳运行状况?
No, masteris just a conventional name, usually taken to be a branch which is readily buildable and hopefully a working snapshot.
不,master只是一个传统的名称,通常被认为是一个易于构建的分支,并且希望是一个工作快照。
How do I fix this?
我该如何解决?
If you have pushed masterto another repository, you need to merge you changes from original_ideainto master:
如果您已将master推送到另一个存储库,则需要将original_idea 中的更改合并到master 中:
git checkout master
git merge original_idea
Be aware that if there are any conflicting changes you'll need to resolve them.
请注意,如果有任何冲突的更改,您需要解决它们。
If you have not yet pushed masterto another repository, you could delete masterand recreate it from original_idea. WARNING:Everyone who was working on masterwill have to reset their masterbranch.
如果您尚未将master推送到另一个存储库,则可以删除master并从original_idea重新创建它。 警告:在master 上工作的每个人都必须重置他们的master分支。
$ git checkout master
Switched to branch 'master'
$ git checkout -b experiment_1
Switched to a new branch 'experiment_1'
# Here we are going to delete master. Should be safe, since experiment_1
# was created from this point on the previous command
$ git branch -d master
Deleted branch master (was 72ea01d).
$ git checkout original_idea
Switched to branch 'original_idea'
# Now that we checked out the branch HEAD is pointing to, we can recreate master
$ git checkout -b master
Switched to a new branch 'master'
回答by VonC
If git branch
shows that your current branch is original_idea, that wouldn't be a detached HEAD situation.
如果git branch
显示您当前的分支是 original_idea,那不会是一个分离的 HEAD 情况。
But in any case, if you want your master to reflect your "current commit" (which is what HEAD means in Git):
但无论如何,如果你想让你的主人反映你的“当前提交”(这就是HEAD 在 Git 中的意思):
git branch -f master HEAD
git checkout master
You would find other alternatives in "How to I “move” my commits from “no branch” to an actual branch?".
您会在“如何将我的提交从“无分支”“移动”到实际分支?“中找到其他替代方案。
The problem with this is that it makes the entire branch currently ending at master disappear.
这样做的问题是它会使当前以 master 结尾的整个分支消失。
Then all you need to do is:
那么你需要做的就是:
- make sure HEAD is referenced by a branch (like
original_idea
on your schema) - rebase that branch on top of master:
- 确保 HEAD 被分支引用(比如
original_idea
在你的架构上) - 在 master 之上重新建立该分支:
That would mean:
那将意味着:
git checkout original_idea
git rebase master
git checkout master
git merge original_idea