Git merge master into feature branch

后端 未结 11 655
既然无缘
既然无缘 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:09

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

    1. Create and switch to a new branch. Make sure the new branch is based on master so 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.

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

    0 讨论(0)
  • 2020-11-27 09:09

    I add my answer, similar to others but maybe it will be the quickest one to read and implement.

    NOTE: Rebase is not needed in this case.

    Assume I have a repo1 and two branches master and dev-user.

    dev-user is a branch done at a certain state of master.

    Now assume that both dev-user and master advance.

    At some point I want dev-user to get all the commits made in master.

    How do I do it?

    git checkout master 
    git pull 
    git checkout dev-user
    git pull
    git merge master 
    git push 
    

    I hope this helps someone else in the same situation.

    0 讨论(0)
  • 2020-11-27 09:14

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

    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.

    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.

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

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

    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 only those 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.

    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.

    0 讨论(0)
  • 2020-11-27 09:14

    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

    The first two commands can be combined to 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.

    0 讨论(0)
  • 2020-11-27 09:15

    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.

    :: 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
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题