Best and Safe Way to Merge a Git Branch into Master

Git has lots of powerful features that make programmers’ life more comfortable. One of the greatest things in Git is that you can easily create and merge git branches. However, which is the safest way to merge your local branch to the master? In this tutorial, we will tell you what the best approach is for doing it.

Let’s discuss the following scenario. Assuming you have created a new branch “test” from master. You work on this branch, but you also want it to be updated with git commits in master that other developers done from time to time.

Get the latest changes from master:

git checkout master
git pull origin master

Then merge the changes with the git merge command:

git merge test

The final step is pushing local changes to master:

git push origin master
When there are changes pushed from a different developer, you will see conflict as soon as you try to push your merge back. For this reason, always do a pull before merge.

Pulling, Merging and Pushing Changes

The git pull command fetches and downloads the content of remote Git Repository and merges the changes into your local repository. It is one of the “syncing” commands which work on the remote branches configured with the git remote command. It is thought to be the combination of git fetch followed by git merge as it not only downloads the remote content but also integrates it into the current working copy. The git merge command integrates the independent lines of development into a single branch. The command is used to combine two branches and also to merge multiple commits into one history. Once you make changes in the local repository and ready to share it with your team members, then execute git push. The git push command uploads the local changes to the remote repository.

To sum up, the commits are uploaded with git push, download with git fetch and git pull, and integrated with git merge.

Do you find this helpful?

Related articles