Best and Safe Way to Merge a Git Branch into Master
- Steps to merging branch into master
- Pulling, Merging and Pushing Changes
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.
Steps to merging branch into master¶
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 have done from time to time. Below, you can find the 6 simple steps that will lead you to merge the branch into master safely.
Note that before starting, you should be on your branch.
The initial command to run is git fetch for getting the latest updates of your repository.
Then, you need to git rebase (suppose, the name of remote is origin, which is by default).
The git rebase command will bring the latest commits of master to your branch.
git rebase origin/master
To learn how to properly git rebase, you can refer to How to Rebase Git Branch.
Switching to master¶
Switch to the master branch by running git checkout:
git checkout master
Get the latest changes from master by using git pull:
git pull origin master
Then merge the changes with the git merge command:
git merge test
The final step is pushing local changes to the remote repository :
git push origin master
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.