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

shu*_*ngh 2 git version-control github git-branch

我有一个topical被采取的development分支。现在topicalBranch领先于提交,同时development接受来自其他一些贡献者的拉取请求,并且更新的合并development与我的最新提交有冲突的更改C4

所以当前的树看起来像:

C1---C2---C5 development
      \
      C3---C4 topicalBranch
Run Code Online (Sandbox Code Playgroud)

我希望新树看起来像:

C1---C2---C5 development
           \
           C3'---C4' topicalBranch
Run Code Online (Sandbox Code Playgroud)

其中 C3' 和 C4' 相对于 C5 进行了更改。我查了一下Git-Rebasing,但我想topicalBranch更新,而不对development.

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

Jef*_*ett 5

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

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

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

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

现在让我们转向主题。

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

并模拟从其他开发人员那里拉来的提交。

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

现在我们可以看到该树与您的树一样,我们希望将新提交从开发分支获取到主题分支中。

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

所以让我们重新设定基础。

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

最后,您可以看到 topicalBranch 的历史记录被重写,以包含之前来自开发分支的新提交。

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

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

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