Search code examples
gitgithubgit-bashgithub-enterprise

Why merging development branch to feature-branch is required when merging a pull request?


I want to merge a GitHub pull request from my feature-branch to development. Here are the instructions from GitHub to do it via command line:

Step 1:

git fetch origin
git checkout -b feature-branch origin/feature-branch
git merge development

Step 2:

git checkout development
git merge --no-ff feature-branch
git push origin development

Why is git merge development required?


Solution

  • The issue here is that from the point at which you initially created your feature branch from development, both you and others may have made commits to the feature and development branches, respectively. A diagram would be helpful here:

    development: -- A -- B -- C -- D
                          \
    feature:               E -- F
    

    Here, you branched from development at the B commit, and since then you have made an F commit, and others (maybe even you as well) have made C and D commits to the remote development branch. The first of your two steps was to merge development into your feature branch:

    development: -- A -- B -- C -- D
                          \         \
    feature:               E -- F -- M1
    

    Now you have an M1 merge commit locally, and your branch should be completely up to date with whatever came in remotely in the C and D commits. Now you are in a position to merge your feature branch into development, leaving you with:

    development: -- A -- B -- C -- D -- M2
                          \         \  /
    feature:               E -- F -- M1
    

    As to why you need these two steps, the first step ensures that your local feature branch is up to date with whatever happened on the remote development since you initially created the branch. At least some of the time, not doing this step would mean that the merge would end up with some conflicts. GitHub detects this, and often will refuse to merge if you are not up to date. The second step is probably the one you were expecting, and is just the actual merge of your feature branch into development.