Git 将 master 合并到特性分支中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16955980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 16:20:38  来源:igfitidea点击:

Git merge master into feature branch

gitgit-branchgit-mergegit-flowfeature-branch

提问by theomega

Let's say we have the following situation in Git:

假设我们在 Git 中有以下情况:

  1. A created repository:

    mkdir GitTest2
    cd GitTest2
    git init
    
  2. Some modifications in the master take place and get committed:

    echo "On Master" > file
    git commit -a -m "Initial commit"
    
  3. Feature1 branched off master and some work is done:

    git branch feature1
    git checkout feature1
    echo "Feature1" > featureFile
    git commit -a -m "Commit for feature1"
    
  4. Meanwhile, a bug is discovered in the master-code and a hotfix-branch is established:

    git checkout master
    git branch hotfix1
    git checkout hotfix1
    
  5. The bug is fixed in the hotfix branch and merged back into the master (perhaps after a pull request/code review):

    echo "Bugfix" > bugfixFile
    git commit -a -m "Bugfix Commit"
    git checkout master
    git merge --no-ff hotfix1
    
  6. Development on feature1 continues:

    git checkout feature1
    
  1. 创建的存储库:

    mkdir GitTest2
    cd GitTest2
    git init
    
  2. master 中的一些修改发生并被提交:

    echo "On Master" > file
    git commit -a -m "Initial commit"
    
  3. Feature1 从 master 分支出来,完成了一些工作:

    git branch feature1
    git checkout feature1
    echo "Feature1" > featureFile
    git commit -a -m "Commit for feature1"
    
  4. 同时,在master-code中发现了一个bug,并建立了一个hotfix-branch:

    git checkout master
    git branch hotfix1
    git checkout hotfix1
    
  5. 该错误在 hotfix 分支中修复并合并回 master(可能在 pull request/code review 之后):

    echo "Bugfix" > bugfixFile
    git commit -a -m "Bugfix Commit"
    git checkout master
    git merge --no-ff hotfix1
    
  6. 功能 1 的开发仍在继续:

    git checkout feature1
    

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).

我想防止在我的功能分支上获得两个与功能实现无关的新提交。如果我使用拉取请求,这对我来说尤其重要:所有这些提交也将包含在拉取请求中,并且必须进行,尽管这已经完成(因为修补程序已经在 master 中)。

I can not do a git merge master --ff-only: "fatal: Not possible to fast-forward, aborting.", but I am not sure if this helped me.

我不能做git merge master --ff-only:“致命:不可能快进,中止。”,但我不确定这是否对我有帮助。

回答by Sven

How do we merge the master branch into the feature branch? Easy:

我们如何将 master 分支合并到 feature 分支中?简单:

git checkout feature1
git merge master

There is no point in forcing a fast forward merge here, as it cannot be done. You committed both into the feature branch and the master branch. Fast forward is impossible now.

在这里强制进行快进合并是没有意义的,因为它无法完成。您同时提交到功能分支和主分支。现在快进是不可能的。

Have a look at GitFlow. It is a branching model for git that can be followed, and you unconsciously already did. It also is an extension to Git which adds some commands for the new workflow steps that do things automatically which you would otherwise need to do manually.

看看GitFlow。它是一个可以遵循的 git 分支模型,而你已经在不知不觉中做到了。它也是 Git 的扩展,它为新的工作流程步骤添加了一些命令,这些命令可以自动执行您需要手动执行的操作。

So what did you do right in your workflow? You have two branches to work with, your feature1 branch is basically the "develop" branch in the GitFlow model.

那么您在工作流程中做对了什么?您有两个分支可以使用,您的 feature1 分支基本上是 GitFlow 模型中的“开发”分支。

You created a hotfix branch from master and merged it back. And now you are stuck.

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

The GitFlow model asks you to merge the hotfix also to the development branch, which is "feature1" in your case.

GitFlow 模型要求您将修补程序也合并到开发分支,在您的情况下是“feature1”。

So the real answer would be:

所以真正的答案是:

git checkout feature1
git merge --no-ff hotfix1

This adds all the changes that were made inside the hotfix to the feature branch, but onlythose changes. They might conflict with other development changes in the branch, but they will not conflict with the master branch should you merge the feature branch back to master eventually.

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

Be very careful with rebasing. Only rebase if the changes you did stayed local to your repository, e.g. you did not push any branches to some other repository. Rebasing is a great tool for you to arrange your local commits into a useful order before pushing it out into the world, but rebasing afterwards will mess up things for the git beginners like you.

重新定位时要非常小心。仅当您所做的更改保留在您的存储库本地时才重新定位,例如您没有将任何分支推送到其他某个存储库。变基是一个很好的工具,可以让您在将本地提交推向世界之前将其安排为有用的顺序,但是之后变基会给像您这样的 git 初学者搞砸事情。

回答by David Sulc

You should be able to rebase your branch on master:

您应该能够在 master 上重新设置分支:

git checkout feature1
git rebase master

Manage all conflicts that arise. When you get to the commits with the bugfixes (already in master), Git will say that there were no changes and that maybe they were already applied. You then continue the rebase (while skipping the commits already in master) with

管理所有出现的冲突。当您使用错误修正(已经在 master 中)进行提交时,Git 会说没有更改并且可能已经应用了它们。然后继续 rebase(同时跳过 master 中已经提交的提交)

git rebase --skip

If you perform a git logon your feature branch, you'll see the bugfix commit appear only once, and in the master portion.

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

For a more detailed discussion, take a look at the Git book documentation on git rebase(https://git-scm.com/docs/git-rebase) which cover this exact use case.

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

================ Edit for additional context ====================

================ 编辑附加上下文 ====================

This answer was provided specifically for the question asked by @theomega, taking his particular situation into account. Note this part:

这个答案是专门为@theomega 提出的问题提供的,考虑到他的特殊情况。注意这部分:

I want to prevent [...] commits on my feature branch which have no relation to the feature implementation.

我想防止在我的功能分支上提交 [...] 与功能实现无关的提交。

Rebasing his private branch on master is exactly what will yield that result. In contrast, merging master into his branch would precisely do what he specifically does not want to happen: adding a commit that is not related to the feature implementation he is working on via his branch.

将他的私有分支重新建立在 master 上正是会产生这种结果的原因。相比之下,将 master 合并到他的分支中恰恰会做他特别不想发生的事情:添加一个与他正在通过他的分支工作的功能实现无关的提交。

To address the users that read the question title, skip over the actual content and context of the question, and then only read the top answer blindly assuming it will always apply to their (different) use case, allow me to elaborate:

为了解决阅读问题标题的用户,跳过问题的实际内容和上下文,然后只盲目阅读最高答案,假设它始终适用于他们的(不同)用例,请允许我详细说明:

  • only rebase private branches (i.e. that only exist in your local repository and haven't been shared with others). Rebasing shared branches would "break" the copies other people may have.
  • if you want to integrate changes from a branch (whether it's master or another branch) into a branch that is public (e.g. you've pushed the branch to open a pull request, but there are now conflicts with master, and you need to update your branch to resolve those conflicts) you'll need to merge them in (e.g. with git merge masteras in @Sven's answer).
  • you can also merge branches into your local private branches if that's your preference, but be aware that it will result in "foreign" commits in your branch.
  • 仅对私有分支进行变基(即,只存在于您的本地存储库中且未与其他人共享的分支)。重定位共享分支会“破坏”其他人可能拥有的副本。
  • 如果你想将一个分支(无论是 master 还是另一个分支)的更改集成到一个公共的分支中(例如,你已经推送了分支来打开一个拉取请求,但是现在与 master 有冲突,你需要更新您的分支来解决这些冲突)您需要将它们合并(例如git merge master在@Sven 的回答中)。
  • 如果这是您的偏好,您也可以将分支合并到您的本地私有分支中,但请注意,这会导致您的分支中出现“外部”提交。

Finally, if you're unhappy with the fact that this answer is not the best fit for your situation even though it was for @theomega, adding a comment below won't be particularly helpful: I don't control which answer is selected, only @theomega does.

最后,如果您对这个答案并不最适合您的情况这一事实感到不满,即使它是针对@theomega 的,在下面添加评论也不会特别有帮助:我不控制选择哪个答案,只有@theomega 可以。

回答by zimi

Based on this article, you should:

根据本文,您应该:

  • create new branch which is based upon new version of master

    git branch -b newmaster

  • merge your old feature branch into new one

    git checkout newmaster

  • resolve conflict on new feature branch

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

    git branch -b newmaster

  • 将旧的功能分支合并到新的分支中

    git checkout newmaster

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

The first two commands can be combined to git checkout -b newmaster.

前两个命令可以组合成git checkout -b newmaster.

This way your history stays clear because you don't need back merges. And you don't need to be so super cautious since you don't need to do a Git rebase.

这样您的历史记录就会保持清晰,因为您不需要反向合并。而且您不需要非常谨慎,因为您不需要执行 Git 变基。

回答by xgqfrms

git merge

git merge

you can follow below steps

您可以按照以下步骤操作

1. merge origin/masterbranch to featurebranch

1.合并origin/master分支到feature分支

# step1: change branch to master, and pull to update all commits
$ git checkout master
$ git pull

# step2: change branch to target, and pull to update commits
$ git checkout feature
$ git pull

# step3: merge master to feature(?? current is feature branch)
$ git merge master



2. merge featurebranch to origin/masterbranch

2. 合并feature分支到origin/master分支

origin/masteris the remote master branch, while masteris the local master branch

origin/master是远程主分支,master而是本地主分支

$ git checkout master
$ git pull origin/master

$ git merge feature
$ git push origin/master

回答by jkdev

Zimi's answerdescribes this process generally. Here are the specifics:

紫米的回答大致描述了这个过程。以下是具体内容:

  1. Create and switch to a new branch. Make sure the new branch is based on masterso it will include the recent hotfixes.

    git checkout master
    git branch feature1_new
    git checkout feature1_new
    
    # Or, combined into one command:
    git checkout -b feature1_new master
    
  2. After switching to the new branch, merge the changes from your existing feature branch. This will add your commits without duplicating the hotfix commits.

    git merge feature1
    
  3. On the new branch, resolve any conflicts between your feature and the master branch.

  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. 在新分支上,解决您的功能和主分支之间的任何冲突。

Done! Now use the new branch to continue to develop your feature.

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

回答by Aaron M.

Here is a script you can use to merge your master branch into your current branch.

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

The script does the following:

该脚本执行以下操作:

  • Switches to the master branch
  • Pulls the master branch
  • Switches back to your current branch
  • Merges the master branch into your current branch
  • 切换到主分支
  • 拉取主分支
  • 切换回当前分支
  • 将主分支合并到您当前的分支中

Save this code as a batch file (.bat) and place the script anywhere in your repository. Then click on it to run it and you are set.

将此代码保存为批处理文件 (.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

回答by Bob Gilmore

You might be able to do a "cherry-pick" to pull the exactcommit(s) that you need in to your feature branch.

您也许可以进行“挑选”以将您需要的确切提交拉到您的功能分支中。

Do a git checkout hotfix1to get on the hotfix1 branch. Then do a git logto get the SHA-1 hash (big sequence of random letters and numbers that uniquely identifies a commit) of the commit in question. Copy that (or the first 10 or so characters).

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

Then, git checkout feature1to get back onto your feature branch.

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

Then, git cherry-pick <the SHA-1 hash that you just copied>

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

That will pull that commit, and onlythat commit, into your feature branch. That change will be in the branch - you just "cherry-picked" it in. Then, resume work, edit, commit, push, etc. to your heart's content.

这将把那个提交,并且只有那个提交,拉到你的功能分支中。该更改将在分支中 - 您只是“挑选”它。然后,继续工作、编辑、提交、推送等,以满足您的需求。

When, eventually, you perform another merge from one branch into your feature branch (or vice-versa), Git will recognize that you've already merged in that particularcommit, know that it doesn't have to make it again, and just "skip over" it.

最终,当您从一个分支执行另一次合并到您的功能分支(或反之亦然)时,Git 将识别出您已经在该特定提交中合并,知道它不必再次进行合并,并且只需“跳过”它。

回答by Tony

I am on the feature branch and made refactorings. I want to merge the master changes now to my feature branch. I am far behind. Note I do not want to pull the master changes to my localbecause my feature branch have modules moved from one place to another. I found just performing below without pull does not work. it says "Already up to date."

我在功能分支上并进行了重构。我想现在将主更改合并到我的功能分支。我远远落后。注意我不想将主更改拉到我的本地,因为我的功能分支有模块从一个地方移动到另一个地方。我发现在没有拉的情况下只在下面执行是行不通的。它说“已经是最新的”。

 //below does not get the latest from remote master to my local feature branch without git pull
    git checkout master 
    git fetch 
    git checkout my-feature-branch 
    git merge master

This below works, note use git merge origin/master:

下面的工作,注意使用 git merge origin/master:

 git checkout master 
    git fetch 
    git checkout my-feature-branch 
    git merge origin/master