从更新的基本分支获取更改到我的功能分支

时间:2016-07-13 13:56:01

标签: git github version-control git-branch

我有一个topicaldevelopment分支。现在提前topicalBranch,同时development接受来自其他贡献者的拉取请求,development上的更新合并与我最近的提交C4发生冲突。

所以当前树看起来像:

C1---C2---C5 development
      \
      C3---C4 topicalBranch

我希望新树看起来像:

C1---C2---C5 development
           \
           C3'---C4' topicalBranch

其中C3'和C4'与C5有变化。我查了Git-Rebasing,但我想更新topicalBranch,而不对development进行任何更改。

使用topicalBranch分支所做的更改更新development的最佳方式是什么,以便我可以向development分支发送新的请求。

2 个答案:

答案 0 :(得分:3)

rebase绝对是你想要的。但是,注意这将重写您的历史记录。因此,如果您要从其他开发人员拉动您的topicalBranch的位置推送到远程存储库,那么他们将不得不强制或重新拉动。但如果你是唯一一个在topicalBranch上工作的人,那么这不是问题。

让我们通过初始化一个新的存储库并进行一些提交来演示一个树根在rebase之后的样子来重建你的场景。

jeff ~ $ mkdir test && cd test
jeff test $ git init
Initialized empty Git repository in /home/jeff/test/.git/
jeff test (master #) $ touch file1 && git add . && git commit -m "init repo"
[master (root-commit) ba3e0ed] init repo
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file1
jeff test (master) $ git branch -m development

jeff test (development) $ touch file2 && git add . && git commit -m "file2"
[development fb03bd9] file2
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file2

现在让我们分一杯羹。

jeff test (development) $ git checkout -b topicalBranch
Switched to a new branch 'topicalBranch'
jeff test (topicalBranch) $ touch file3 && git add . && git commit -m "file3"
[topicalBranch c9ffa5a] file3
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file3
jeff test (topicalBranch) $ touch file4 && git add . && git commit -m "file4"
[topicalBranch 5322397] file4
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file4

并模拟其他开发人员提交的提交。

jeff test (topicalBranch) $ git checkout development
Switched to branch 'development'
jeff test (development) $ touch file5 && git add . && git commit -m "file5"
[development e237fb5] file5
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file5

现在我们可以看到树就像你的那样,我们希望从开发分支到主题分支获得新的提交。

jeff test (development) $ git log --graph --oneline --decorate --all
* e237fb5 (HEAD -> development) file5
| * 5322397 (topicalBranch) file4
| * c9ffa5a file3
|/  
* fb03bd9 file2
* ba3e0ed init repo

所以,让我们来吧。

jeff test (development) $ git checkout topicalBranch
Switched to branch 'topicalBranch'
jeff test (topicalBranch) $ git rebase development
First, rewinding head to replay your work on top of it...
Applying: file3
Applying: file4

最后你可以看到topicalBranch重写了它的历史,以包含之前开发分支的新提交。

jeff test (topicalBranch) $ git log --graph --oneline --decorate --all
* f332250 (HEAD -> topicalBranch) file4
* a069799 file3
* e237fb5 (development) file5
* fb03bd9 file2
* ba3e0ed init repo

现在,您将能够轻松地快速将topicalBranch合并到开发中。

对于进入开发分支的新提交,可以根据需要重复此过程。我建议经常这样做,这样可以定期解决冲突,而不是在topicalBranch最终完成时解决一连串冲突 - 此外,这将帮助您尽早发现配置和架构漂移。

答案 1 :(得分:1)

您需要将开发分支合并到您的主题分支中,因此它看起来更像

C1---C2--------C5 development
      \         \
      C3---C4---C6' topicalBranch

因此,您需要从您的主题分支git merge development开始。

相关问题