git:合并分支但保留提交历史 [英] git: Merge Branches but Keep Commit History
问题描述
在我的git工作流中,我们有一个主存储库和一个分支master。每个人都从远程主人拉,每个人都推到远程主人。当我准备一个功能时,我想在自己的分支中工作。到目前为止,我的历史是这样的:
git pull --rebase
git checkout -b new_feature
<进行一些提交>
git checkout master
git pull --rebase
现在我想合并
$ b
- 我的本地主分支没有合并提交。
- 所有提交到我的new_feature分支中的提交合并到master中,就像我在master中创建它们一样。
- 所有合并的提交将合并到本地远程头指针的顶部。
我最关心的是第3项,当需要时,我可以安全地推送更改。如果合并的提交在提交之前与提交交织在一起,那么我将遇到问题推送,请参阅我遇到的相关问题:。
我已阅读以下内容:
p>- http://mettadore.com/2011/05/06/a-simple-git-rebase-workflow-explained/
- 在'git merge'后保留提交历史记录
- 你如何重新设置当前分支的变化?
A nd我想我需要做的:
$ g $ g $ g $ git checkout master $ b $ git pull --rebase $ b $ git checkout new_feature
git rebase master
git checkout master
git rebase new_feature
git push
我的理解是,
git checkout new_feature
git rebase master
会使new_feature显得好像从新的当前头部分支出来一样。真的吗?
git checkout master
git rebase new_feature
会将new_feature放在master的顶部。那是对的吗?如果是这样,这是我混乱的主要观点。如果git rebase master将主提交置于new_feature的底部,那么为什么git rebase new_feature会将new_feature提交放在master的顶部,也就是说为什么它不会做相反的处理?
以下是您可以使用的工作流程,它只是您需要做的事情。
$ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ b<做一堆提交>
git rebase master(2)
git checkout master
git merge new_feature(3)
git branch -D new_feature(4)
说明
(1) git pull --rebase
将首先获取 origin / master
,然后在其上重播您的本地 master
。请注意,在示例日志中,您的本地提交位于本地远程HEAD指针之上。
> git log --oneline --all -10 --decorate
d34d34c(HEAD,master)本地提交消息。
d3434r2本地提交消息。
d234d4c本地提交消息。
er3ede3(origin / master,origin / HEAD)远程提交消息。
sfe3fd3远程提交消息。
您现在可以结帐
new_feature
分支一段时间。当你完成... ...
<2> git rebase master
将重放 new_feature
位于 master
之上。再次,您的本地提交仍然在您的本地远程HEAD指针之上。
> git log --oneline --all -10 --decorate
fc5773d(new_feature)本地new_feature提交。
9282838本地new_feature提交。
d34d34c(HEAD,master)本地提交。
d3434r2本地提交。
d234d4c本地提交。
er3ede3(origin / master,origin / HEAD)远程提交。
sfe3fd3远程提交。
(3) (4)之后,您可以安全地删除 In my git workflow we have one main repository and one branch, master. Everyone pulls from remote master and everyone pushes to remote master. I want to work in my own branch while I prepare a feature. So far my history is something like this: Now I want to merge the branch and here's what I need: My biggest concern is item 3, when is needed so that I can safely push the changes. If the merged commits are intertwined with commits before head then I will have problems pushing, see related problem I had: git: Pushing Single Commits, Reordering with rebase, Duplicate Commits. I've read the following: And I think I need to do: My understanding is that will make new_feature appear as if it was branched off from the new current head. Is that true? And that will place new_feature on top of master. Is that correct? If so, this is the main point of my confusion. If "git rebase master" places master commits at the bottom of new_feature, then why does "git rebase new_feature" place new_feature commits at the top of master, i.e. why doesn't it do the opposite? Here is a workflow that you can use that does just what you need it to do.
(1) You can now (2) The (3) (4) After that, you can safely delete the
这篇关于git:合并分支但保留提交历史的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$> code> new_feature
git merge new_feature ,它会做一个快进合并。现在 HEAD
, new_feature
和 master
都指向同样的提交。
> git log --oneline --all -10 --decorate
fc5773d(HEAD,new_feature,master)本地new_feature提交。
9282838本地new_feature提交。
d34d34c本地提交。
d3434r2本地提交。
d234d4c本地提交。
er3ede3(origin / master,origin / HEAD)远程提交。
sfe3fd3远程提交。
new_feature
分支。推送前的最终日志将如下所示:
> git log --oneline --all -10 --decorate
fc5773d(HEAD,master)本地new_feature commit 2
9282838 Local new_feature commit。
d34d34c本地提交。
d3434r2本地提交。
d234d4c本地提交。
er3ede3(origin / master,origin / HEAD)远程提交。
sfe3fd3远程提交。
git pull --rebase
git checkout -b new_feature
<make some commits>
git checkout master
git pull --rebase
git checkout master
git pull --rebase
git checkout new_feature
git rebase master
git checkout master
git rebase new_feature
git push
git checkout new_feature
git rebase master
git checkout master
git rebase new_feature
Answer
git checkout master
git pull --rebase (1)
git checkout new_feature
<do a bunch of commits>
git rebase master (2)
git checkout master
git merge new_feature (3)
git branch -D new_feature (4)
Explanation
git pull --rebase
will first fetch origin/master
and then replay your local master
on top of it. Note in the example log that your local commits are on top of your "local remote HEAD pointer."> git log --oneline --all -10 --decorate
d34d34c (HEAD, master) Local commit message.
d3434r2 Local commit message.
d234d4c Local commit message.
er3ede3 (origin/master, origin/HEAD) Remote commit message.
sfe3fd3 Remote commit message.
checkout
and work on your new_feature
branch for a while. When you're done...git rebase master
will replay new_feature
on top of master
. Again, your local commits remain on top of your "local remote HEAD pointer."> git log --oneline --all -10 --decorate
fc5773d (new_feature) Local new_feature commit.
9282838 Local new_feature commit.
d34d34c (HEAD, master) Local commit.
d3434r2 Local commit.
d234d4c Local commit.
er3ede3 (origin/master, origin/HEAD) Remote commit.
sfe3fd3 Remote commit.
rebase
command just put new_feature
ahead of master, and to align them you need to run...git merge new_feature
, which will do a fast-forward merge. Now HEAD
, new_feature
, and master
all point to the same commit.> git log --oneline --all -10 --decorate
fc5773d (HEAD, new_feature, master) Local new_feature commit.
9282838 Local new_feature commit.
d34d34c Local commit.
d3434r2 Local commit.
d234d4c Local commit.
er3ede3 (origin/master, origin/HEAD) Remote commit.
sfe3fd3 Remote commit.
new_feature
branch. Your final log before pushing will look like this:> git log --oneline --all -10 --decorate
fc5773d (HEAD, master) Local new_feature commit 2
9282838 Local new_feature commit.
d34d34c Local commit.
d3434r2 Local commit.
d234d4c Local commit.
er3ede3 (origin/master, origin/HEAD) Remote commit.
sfe3fd3 Remote commit.