How Do I Safely Merge a Git Branch into Master?

Better Stack Team
Updated on June 24, 2024

To safely merge a Git branch into the master branch (or any other target branch), you can follow these steps to ensure a smooth and reliable merge:

First, ensure that your local master branch is up to date with the latest changes from the remote repository:

 
git checkout master
git pull origin master

Merge the feature branch into the master branch using the git merge command:

 
git merge <feature-branch>

Replace <feature-branch> with the name of your feature branch.

If Git encounters any merge conflicts during the merge process, it will pause and prompt you to resolve them. Open the conflicted files in your text editor, resolve the conflicts manually, and then stage the changes using git add. After resolving all conflicts, continue the merge process by running:

 
git merge --continue

After resolving conflicts (if any), review the changes introduced by the merge by examining the diff or using a visual Git tool.

 
git diff master..HEAD

Before pushing the merged changes to the remote repository, test the merged code locally to ensure that it functions as expected and doesn't introduce any regressions or issues.

If everything looks good, push the merged changes to the remote repository:

 
git push origin master

Once the feature branch has been merged into master and pushed to the remote repository, you can delete the feature branch if it's no longer needed:

 
git branch -d <feature-branch>

Replace <feature-branch> with the name of your feature branch.

Note:

  • Always update your master branch with the latest changes from the remote repository before merging a feature branch to ensure that you're merging into the most recent version of the codebase.
  • Resolving merge conflicts requires careful attention to ensure that conflicting changes are integrated correctly.
  • Testing the merged code locally helps identify any issues before pushing changes to the shared repository.
  • Deleting feature branches after merging helps keep the repository clean and organized.
Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github