Skip to content
Fix Code Error

Git merge master into feature branch

March 13, 2021 by Code Error
Posted By: Anonymous

Let’s say we have the following situation in 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
    

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

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.

Solution

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.

Answered By: Anonymous

Related Articles

  • Setting up and using Meld as your git difftool and mergetool
  • Git workflow and rebase vs merge questions
  • Why does git perform fast-forward merges by default?
  • In plain English, what does "git reset" do?
  • Create Log File in Powershell
  • Checkout another branch when there are uncommitted…
  • Why call git branch --unset-upstream to fixup?
  • Git merge with force overwrite
  • Why do I have to "git push --set-upstream origin "?
  • How to create the branch from specific commit in…
  • Why do git fetch origin and git fetch : behave differently?
  • Difference between git checkout --track…
  • How do I 'overwrite', rather than 'merge', a branch…
  • Git Using Remote Branch
  • Recalculate merge conflicts (ie. how to generate…
  • Best way to integrate Vue.js into existing ASP.NET…
  • Git: Create a branch from unstaged/uncommitted…
  • Why does Git say my master branch is "already up to…
  • git lfs push to github failure on Ubuntu 18.04
  • What's the difference between "git reset" and "git…
  • How do I make a Git commit in the past?
  • Your configuration specifies to merge with the from…
  • Rebasing remote branches in Git
  • What exactly does the "u" do? "git push -u origin…
  • Pandas Merging 101
  • XAMPP Port 80 in use by "Unable to open process" with PID 4
  • How to recover stashed uncommitted changes
  • Practical uses of git reset --soft?
  • Various ways to remove local Git changes
  • How to track untracked content?
  • Git push rejected after feature branch rebase
  • How does PHP 'foreach' actually work?
  • What are the undocumented features and limitations…
  • How can I move HEAD back to a previous location?…
  • Git submodule update
  • Why does git say "Pull is not possible because you…
  • When would you use the different git merge strategies?
  • Retrieve specific commit from a remote Git repository
  • Delete all local git branches
  • Merge, update, and pull Git branches without using checkouts
  • How to clone ('fork') your own personal GitHub repo…
  • Git Cherry-pick vs Merge Workflow
  • git status shows modifications, git checkout --…
  • Output of git branch in tree like fashion
  • Git pull a certain branch from GitHub
  • How can I reconcile detached HEAD with master/origin?
  • Git submodule head 'reference is not a tree' error
  • Remove tracking branches no longer on remote
  • Homebrew install specific version of formula?
  • How to pull in changes from skeleton sub-repository…
  • Windows could not start the Apache2 on Local…
  • how does git know there is a conflict
  • Git merge errors
  • MySQL server has gone away - in exactly 60 seconds
  • How do I merge two dictionaries in a single…
  • git bring some "older" changes to a new branch after…
  • git pull while not in a git directory
  • Git Pull Request no changes but git diff show changes
  • In git, what is the difference between merge…
  • What's the difference between HEAD^ and HEAD~ in Git?
  • What is a tracking branch?
  • How to merge my branch to master if some other…
  • Squash the first two commits in Git?
  • git pull from master into the development branch
  • How do I delete a Git branch locally and remotely?
  • Finding what branch a Git commit came from
  • merge one local branch into another local branch
  • How to cherry pick a range of commits and merge into…
  • Why did my Git repo enter a detached HEAD state?
  • How do I clone a single branch in Git?
  • Does "git fetch --tags" include "git fetch"?
  • How can I delete all Git branches which have been merged?
  • How do I include certain conditions in SQL Count
  • How do you get git to always pull from a specific branch?
  • How to git reset --hard a subdirectory?
  • How to keep a git branch in sync with master
  • can I run multiple DataFrames and apply a function…
  • How do I add files and folders into GitHub repos?
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • Finding a branch point with Git?
  • git switch branch without discarding local changes
  • In Git, what is the difference between origin/master…
  • How do you fix a bad merge, and replay your good…
  • How to find the nearest parent of a Git branch?
  • How to UPSERT (MERGE, INSERT ... ON DUPLICATE…
  • How to commit my current changes to a different…
  • Move the most recent commit(s) to a new branch with Git
  • Git fetch remote branch
  • POI Word Unable to merge newly created cell vertically
  • How to use git merge --squash?
  • git stash blunder: git stash pop and ended up with…
  • How to use terminal commands with Github?
  • How do I revert a Git repository to a previous commit?
  • How does GitLab merge request with squash work under…
  • Git push won't do anything (everything up-to-date)
  • master branch and 'origin/master' have diverged, how…
  • Merge (with squash) all changes from another branch…
  • How to echo with different colors in the Windows…
  • Merge development branch with master
  • Egit rejected non-fast-forward

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How to allow only numeric (0-9) in HTML inputbox using jQuery?

Next Post:

ssh “permissions are too open” error

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error