git 将主头移动到分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10753330/
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
Moving master head to a branch
提问by kostja
I have several feature branches and a master branch. Feature2 is done. Normally I would rebase (working with a remote SVN repo and would like to keep the history, so no regular merge) and ff-merge. But since master hasnt changed since I branched, I would like to move the master head (at E
) to G
. Using git branch -f master G
does not result in any visible changes, I assumed this is because G
is on a different branch.
我有几个功能分支和一个主分支。功能2完成。通常我会变基(使用远程 SVN 存储库并希望保留历史记录,因此没有定期合并)和 ff-merge。但是自从我分支以来 master 没有改变,我想将 master 头 (at E
)移动到G
. 使用git branch -f master G
不会导致任何可见的变化,我认为这是因为G
在不同的分支上。
Is it safe to use git update-ref -f master G
here instead? Should I stick with rebase/ff-merge? Something even better?
git update-ref -f master G
在这里使用是否安全?我应该坚持使用 rebase/ff-merge 吗?还有更好的吗?
feature1 C-D
/
master A-B-E
\
feature2 F-G
Thank you.
谢谢你。
采纳答案by ellotheth
A regular merge of G into master will do the trick, no need to rebase:
将 G 定期合并到 master 中即可解决问题,无需重新设定基准:
feature1 C-D
/
master A-B-E
\
feature2 F-G
git checkout master
git merge feature2
feature1 C-D
/
master, feature2 A-B-E-F-G
回答by ralphtheninja
You don't have to merge the branches, a reset is enough. Assuming master is checked out:
您不必合并分支,重置就足够了。假设 master 被检出:
git reset --hard feature2
回答by GoZoner
No merging required - just rename the branches. Since you don't care about feature2 ('is done') nor the existing master (at 'E') you just need the following.
无需合并 - 只需重命名分支。由于您不关心 feature2 ('is done') 或现有的 master (at 'E'),您只需要以下内容。
git branch -d master
git branch -m feature2 master
Simple is better?
越简单越好?
Remember there are two key concepts involved:
请记住,涉及两个关键概念:
- The Git commit graph, and
- The Git references
- Git 提交图,以及
- Git参考
When you do a merge (of various flavors and including rebase) you are changing the commit graph. The changes involve adding nodes, adding links or perhaps moving links. References (including branches and tags) only point to commits and thus changing a reference just changes the pointed-to commit - not the structure of the graph.
当您进行合并(各种风格并包括变基)时,您正在更改提交图。更改涉及添加节点、添加链接或可能移动链接。引用(包括分支和标签)仅指向提交,因此更改引用只会更改指向的提交 - 而不是图的结构。
So, in your case, there is no needed change to the structure, just a changing of the references.
因此,在您的情况下,无需更改结构,只需更改引用即可。
A one line version is:
单行版本是:
git branch -f master feature2
which keeps the feature2 branch around (unlike the prior two-liner which axes feature2).
这使 feature2 保持分支(与之前的两个轴的轴 feature2 不同)。
回答by jthill
The update-ref is safe. A branch head is nothing more than a little "read me!" tag hung on a commit. It's purely by convention that git picks it up and hangs it on a different commit for you at times.
update-ref 是安全的。一个分支负责人无非就是一个小小的“读我!” 标记挂在提交上。按照惯例,git 有时会拿起它并将其挂在不同的提交上。
Using git branch -f master G does not result in any visible changes
使用 git branch -f master G 不会导致任何可见的变化
What does git log --decorate --oneline --all
say? git show master
?
什么git log --decorate --oneline --all
发言权? git show master
?
回答by Dan Lister
It might be best to perform a non fast forward merge of feature 2 into master by using git merge --no-ff feature2
if you have checked out onto the master branch. You should end up with the following
git merge --no-ff feature2
如果您已检出到 master 分支,最好使用将功能 2 非快进合并到 master 中。你应该得到以下结果
feature1 C-D
/
master A-B-E-----H
\ /
feature2 F-G