Merge development branch with master

GitGit Merge

Git Problem Overview


I have two branches namely master and development in a GitHub Repository. I am doing all my development in development branch as shown.

git branch development
git add *
git commit -m "My initial commit message"
git push -u origin development

Now I want to merge all the changes on the development branch into the master. My current approach is:

git checkout master 
git merge development
git push -u origin master 

Please let me know if the procedure I am following is correct.

Git Solutions


Solution 1 - Git

I generally like to merge master into the development first so that if there are any conflicts, I can resolve in the development branch itself and my master remains clean.

(on branch development)$ git merge master
(resolve any merge conflicts if there are any)
git checkout master
git merge development (there won't be any conflicts now)

There isn't much of a difference in the two approaches, but I have noticed sometimes that I don't want to merge the branch into master yet, after merging them, or that there is still more work to be done before these can be merged, so I tend to leave master untouched until final stuff.

EDIT: From comments

If you want to keep track of who did the merge and when, you can use --no-ff flag while merging to do so. This is generally useful only when merging development into the master (last step), because you might need to merge master into development (first step) multiple times in your workflow, and creating a commit node for these might not be very useful.

git merge --no-ff development

Solution 2 - Git

Personally, my approach is similar to yours, with a few more branches and some squashing of commits when they go back to master.

One of my co-workers doesn't like having to switch branches so much and stays on the development branch with something similar to the following all executed from the development branch.

git fetch origin master    
git merge master    
git push origin development:master

The first line makes sure he has any upstream commits that have been made to master since the last time updated his local repository.

The second pulls those changes (if any) from master into development

The third pushes the development branch (now fully merged with master) up to origin/master.

I may have his basic workflow a little wrong, but that is the main gist of it.

Solution 3 - Git

Explanation from the bottom for ones who came here without any knowledge of branches.

> Basic main/master branch development logic is: You work only on another branches, so you use main/master branch only to merge with another branch which is ready for merging.

You begin to create a new branch in this way:

  1. Clone repository in your local dir (or create a new repository):
$ cd /var/www
$ git clone [email protected]:user_name/repository_name.git
  1. Create a new branch. It will contain the latest files of your master branch repository
$ git branch new_branch
  1. Change your current git branch to the new_branch
$ git checkout new_branch
  1. Do coding, commits, as usual…
$ git add .
$ git commit -m “Initial commit”
$ git push # pushes commits only to “new_branch”
  1. When job is finished on this branch, merge with “master” branch:
$ git merge master
$ git checkout master # goes to master branch
$ git merge development # merges files in localhost. Master shouldn’t have any  commits ahead, otherwise there will be a need for pull and merging code by hands!
$ git push # pushes all “new_branch” commits to both branches - “master” and “new_branch”

I also recommend using the Sourcetree App to see visual tree of changes and branches.

Solution 4 - Git

1. //pull the latest changes of current development branch if any        
git pull (current development branch)

2. //switch to master branch
git checkout master 

3. //pull all the changes if any
git pull

4. //Now merge development into master    
git merge development
   
5. //push the master branch
git push origin master

Solution 5 - Git

It would be great if you can use the Git Flow workflow. It can merge develop branch into master easily.

What you want to do is just follow the git-flow instruction mentioned here:

STEPS:

  • setup the git-flow project
  • create branches and merge everything to develop
  • run the command git flow release start <version_number>
  • then provide a meaningful message for the release
  • run the command git flow release finish <version_number>
  • it will merge everything into master and change the branch to master.
  • run the command git push to publish the changes to the remote master.

For more information, visit the page - http://danielkummer.github.io/git-flow-cheatsheet/

Solution 6 - Git

Yes, this is correct, but it looks like a very basic workflow, where you're just buffering changes before they're ready for integration. You should look into more advanced workflows that git supports. You might like the topic branch approach, which lets you work on multiple features in parallel, or the graduation approach which extends your current workflow a bit.

Solution 7 - Git

If you are on Mac or Ubuntu, go to the working folder of the branch. In the terminal

suppose harisdev is the branchname.

git checkout master

if there are untracked or uncommitted files you will get an error and you have to commit or delete all the untracked or uncommitted files.

git merge harisdev 

git push origin master

One last command to delete the branch.

$ git branch -d harisdev

Solution 8 - Git

Step 1

Create and switch to a new "dev" branch, where your local git files are in-synced with the remote but "dev" branch does not exist yet.

git branch dev # create
git checkout dev # switch
# No need to git add or git commit, the current
# branch's files will be cloned to the new branch by-default.
git push --set-upstream origin dev # push the "dev" branch to the remote.

Step 2

Make your changes to the "dev" branch (your current if you follow step 1), commit and push them to the remote "dev" branch.

git add .
git commit -S -m "my first commit to the dev branch" # remove the -S if you're not "secure", secure = when you already setup crypto private and public keys (i.e "verified" green sign in github)
git push -u origin dev # push the changes to the remote, -u origin dev is optional but good to use.

Step 3

Merge your "dev" branch into the "master".

git checkout dev # switch to "dev" branch if you're not already.
git merge master # optionally, this command is being used to resolve any conflicts if you pushed any changes to your "master" but "dev" doesn't have that commit.
git checkout master # switch to "master", which is the branch you want to be merged.
git merge --no-ff dev # merge the "dev" branch into the "master" one.

Solution 9 - Git

This is how I usually do it. First, sure that you are ready to merge your changes into master.

  1. Check if development is up to date with the latest changes from your remote server with a git fetch
  2. Once the fetch is completed git checkout master.
  3. Ensure the master branch has the latest updates by executing git pull
  4. Once the preparations have been completed, you can start the merge with git merge development
  5. Push the changes with git push -u origin master and you are done.

You can find more into about git merging in the article.

Solution 10 - Git

  1. On branch Development, check git status using following command:

    git status

There should be no uncommitted code. If it is, push your code on Development branch:

git add *

git commit -m "My initial commit message"

git push origin Development

2) On Development branch, run following two commands:

git branch -f master HEAD

git push -f origin master

It will push your Development branch code to master branch.

Solution 11 - Git

Based on @Sailesh and @DavidCulp:

(on branch development)
$ git fetch origin master
$ git merge FETCH_HEAD
(resolve any merge conflicts if there are any)
$ git checkout master
$ git merge --no-ff development (there won't be any conflicts now)

The first command will make sure you have all upstream commits made to remote master, with Sailesh response that would not happen.

The second will perform a merge and create conflicts that you can then resolve.

After doing so, you can finally checkout master to switch to master.

Then you merge the development branch onto the local master. The no-ff flag will create a commit node in master for the whole merge to be trackable.

After that you can commit and push your merge.

This procedure will make sure there's a merge commit from development to master that people can see, then if they go look at the development branch they can see the individual commits you've made to that branch during its development.

Optionally, you can amend your merge commit before you push it, if you want to add a summary of what was done in the development branch.

EDIT: my original answer suggested a git merge master which didn't do anything, it's better to do git merge FETCH_HEAD after fetching the origin/master

Solution 12 - Git

Once you 'checkout' the development branch you ...

 git add .
 git commit -m "first commit"
 git push origin dev
 git merge master
  
 git checkout master 
 git merge dev
 git push origin master 

Solution 13 - Git

I think the easiest solution would be

git checkout master
git remote update
git merge origin/Develop -X theirs
git commit -m commit -m "New release"
git push --recurse-submodules=check --progress "origin" refs/heads/Master

This also preserves the history of all the branches in use

Solution 14 - Git

If you are using gerrit, the following commands work perfectly.

git checkout master
git merge --no-ff development

You can save with the default commit message. Make sure, the change id has been generated. You can use the following command to make sure.

git commit --amend

Then push with the following command.

git push origin HEAD:refs/for/refs/heads/master

You might encounter an error message like the below.

! [remote rejected] HEAD -> refs/for/refs/heads/master (you are not allowed to upload merges)

To resolve this, the gerrit project admin has to create another reference in gerrit named 'refs/for/refs/heads/master' or 'refs/for/refs/heads/*' (which will cover all branches in future). Then grant 'Push Merge Commit' permission to this reference and 'Submit' permission if required to Submit the GCR.

Now, try the above push command again, and it should work.

Credits:

https://github.com/ReviewAssistant/reviewassistant/wiki/Merging-branches-in-Gerrit

https://stackoverflow.com/a/21199818/3877642

Solution 15 - Git

1. //push the latest changes of current development branch if any        
git push (current development branch)

2. //switch to master branch
git checkout master 

3. //pull all the changes if any from (current development branch)
git pull origin (current development branch)

4. //Now merge development into master    
git merge development

5. //push the master branch
git push origin master

Error
To https://github.com/rajputankit22/todos-posts.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/rajputankit22/todos-posts.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Then Use 
5. //push the master branch forcefully
git push -f origin master

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPawanView Question on Stackoverflow
Solution 1 - GitSaileshView Answer on Stackoverflow
Solution 2 - GitDavid CulpView Answer on Stackoverflow
Solution 3 - GitGediminasView Answer on Stackoverflow
Solution 4 - GitRizwan SiddiqueeView Answer on Stackoverflow
Solution 5 - GitHarshaView Answer on Stackoverflow
Solution 6 - GitSergiu DumitriuView Answer on Stackoverflow
Solution 7 - GitHaris NpView Answer on Stackoverflow
Solution 8 - GitkatarasView Answer on Stackoverflow
Solution 9 - GitNesha ZoricView Answer on Stackoverflow
Solution 10 - GitHimanshu MahajanView Answer on Stackoverflow
Solution 11 - GitCiscoView Answer on Stackoverflow
Solution 12 - GitTiago MediciView Answer on Stackoverflow
Solution 13 - GitVishalView Answer on Stackoverflow
Solution 14 - GitSankaran SrinivasanView Answer on Stackoverflow
Solution 15 - GitAnkit Kumar RajpootView Answer on Stackoverflow