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

Get changes from updated base branch into my feature branch

我有一个 topical 分支 development。现在 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.

做任何更改

development 分支上所做的更改来更新我的 topicalBranch 的最佳方法是什么,以便我可以向 development 分支发出新的拉取请求。

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

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

所以你需要从你的主题分支git merge development做。

rebase绝对是你想要的。但是,请注意 这将重写您的历史记录。因此,如果您从其他开发人员正在拉取您的 topicalBranch 的地方推送到远程存储库,那么他们将不得不强制或变基拉取。但是,如果您是唯一一个使用 topicalBranch 的人,那么这不是问题。

让我们通过初始化一个新的存储库并进行一些提交来重建您的场景,以演示变基后树的外观。

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 最终完成时解决一连串的冲突 - 此外,这将帮助您尽早发现配置和架构偏差。