Git merge master into feature branch

后端 未结 11 693
既然无缘
既然无缘 2020-11-27 08:56

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

  1. A created repository:

    mkdir GitTest2
    cd GitTest2
    git init
    
  2. Some m

11条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 09:16

    You should be able to rebase your branch on 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

    git rebase --skip
    

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

    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.

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

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

    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.

    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 master as 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.

    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.

提交回复
热议问题