首页 文章

Git将master合并到功能分支中

提问于
浏览
667

可以说我们在git中有以下情况:

  • 已创建的存储库:
mkdir GitTest2
cd GitTest2
git init
  • 主要的一些修改发生并得到提交 .
echo "On Master" > file
git commit -a -m "Initial commit"
  • Feature1分支主,一些工作完成:
git branch feature1
git checkout feature1
echo "Feature1" > featureFile
git commit -a -m "Commit for feature1"
  • 同时,在主代码中发现错误并 Build 了修补程序分支
git checkout master
git branch hotfix1
git checkout hotfix1
  • 该错误已在修复程序分支中修复并合并回主服务器(可能在执行拉取请求/代码审查之后):
echo "Bugfix" > bugfixFile
git commit -a -m "Bugfix Commit"
git checkout master
git merge --no-ff hotfix1
  • feature1的开发继续:
git checkout feature1

Now my question: Say I need the hotfix in my feature branch, maybe because the bug also occurs there. How can I achieve this without duplicating the commits into my feature branch? I want to prevent to get two new commits on my feature branch which have no relation to the feature implementation. This especially seems important for me if I use Pull Requests: All these commits will also be included in the Pull Request and have to be reviewed although this has already been done (as the hotfix is already in the master).

我不能做 git merge master --ff-only :"fatal: Not possible to fast-forward, aborting.",但我不确定这是否对我有帮助 .

6 回答

  • 17

    你应该能够在master上重新分支你的分支:

    git checkout feature1
    git rebase master
    

    管理所有出现的冲突 . 当你使用错误修正(已经在master中)进行提交时,git会说没有任何更改,也许它们已经应用了 . 然后,您继续使用rebase(同时跳过已在master中的提交)

    git rebase --skip
    

    如果在功能分支上执行 git log ,您将看到错误修正提交仅出现一次,并且在主要部分中 .

    有关更详细的讨论,请查看 git rebasehttps://git-scm.com/docs/git-rebase)上的Git书籍文档,其中涵盖了这个确切的用例 .

  • 805

    如何将主分支合并到功能分支?简单:

    git checkout feature1
    git merge master
    

    在这里强制快进合并毫无意义,因为它无法完成 . 您已将两者都提交到功能分支和主分支 . 快进现在是不可能的 .

    看看gitflow . 它是git的分支模型,可以遵循,你无意识地已经做到了 . 它也是git的扩展,它为新的工作流程步骤添加了一些命令,这些命令会自动执行,否则您需要手动执行这些操作 .

    那么你在工作流程中做了什么?你有两个分支可以使用,你的feature1分支基本上是gitflow模型中的“develop”分支 .

    您从master创建了一个修补程序分支并将其合并 . 现在你被卡住了 .

    gitflow模型要求您将修补程序也合并到devel分支,在您的情况下为“feature1” .

    所以真正的答案是:

    git checkout feature1
    git merge --no-ff hotfix1
    

    这会将修补程序内部所做的所有更改添加到功能分支,但仅包括这些更改 . 它们可能与分支中的其他开发更改冲突,但如果最终将功能分支合并回master,它们将不会与主分支冲突 .

    重新定位时要非常小心 . 只有在您所做的更改保留在您的存储库本地时才进行rebase,例如你没有将任何分支推送到其他存储库 . 重新定位是一个很好的工具,你可以将你的本地提交安排到一个有用的顺序,然后再将其推向世界,但之后的重新定位会让像你这样的git初学者陷入困境 .

  • 384

    基于this article你应该:

    • 创建基于新版本master的新分支

    • 将旧功能分支合并为新功能分支

    • 解决新功能分支上的冲突

    这样,您的历史记录保持清晰,因为您不需要返回合并 . 而且你不需要如此超级谨慎,因为你不需要git rebase

  • 51

    Zimi's answer一般地描述了这个过程 . 以下是具体内容:

    1)创建并切换到新分支 . 确保新分支基于 master ,因此它将包含最新的修补程序 .

    git checkout master
    git branch feature1_new
    git checkout feature1_new
    
    # Or, combined into one command:
    git checkout -b feature1_new master
    

    2)切换到新分支后,合并现有功能分支中的更改 . 这将添加您的提交而不复制修补程序提交 .

    git merge feature1
    

    3)在新分支上,解决功能与主分支之间的任何冲突 .

    完成!现在使用新分支继续开发您的功能 .

  • 1

    您可以执行"cherry-pick"将所需的确切提交提取到功能分支 .

    执行 git checkout hotfix1 以获取hotfix1分支 . 然后执行 git log 以获取相关提交的SHA1哈希(随机字母的大序列和唯一标识提交的数字) . 复制那个(或前10个左右的字符) .

    然后, git checkout feature1 返回到您的功能分支 .

    然后, git cherry-pick <the SHA1 hash that you just copied>

    这会将该提交(仅提交)提交到您的功能分支中 . 这个改变将在分支中 - 你只需要"cherry-picked" . 然后,恢复工作,编辑,提交,推送等等到你心中的内容 .

    最后,当你从一个分支执行另一个合并到你的功能分支(反之亦然)时,git会认识到你已经在该特定提交中合并,知道它不必再次进行,并且只是"skip over"它 .

  • 8

    这是一个可用于将主分支合并到当前分支的脚本 .

    该脚本执行以下操作:

    • 切换到主分支

    • 拉出主分支

    • 切换回当前分支

    • 将主分支合并到当前分支中

    将此代码保存为批处理文件(.bat),并将脚本放在存储库中的任何位置 . 然后单击它以运行它并设置 .

    :: This batch file pulls current master and merges into current branch
    
    @echo off
    
    :: Option to use the batch file outside the repo and pass the repo path as an arg
    set repoPath=%1
    cd %repoPath%
    
    FOR /F "tokens=*" %%g IN ('git rev-parse --abbrev-ref HEAD') do (SET currentBranch=%%g)
    
    echo current branch is %currentBranch%
    echo switching to master
    git checkout master
    echo.
    echo pulling origin master
    git pull origin master
    echo.
    echo switching back to %currentBranch%
    git checkout %currentBranch%
    echo.
    echo attemting merge master into %currentBranch%
    git merge master
    echo.
    echo script finished successfully
    PAUSE
    

相关问题