What is the best (and safest) way to merge a Git branch into master?

ID : 303

viewed : 132

Tags : gitgit-branchgit-mergebranching-and-merginggit

Top 5 Answer for What is the best (and safest) way to merge a Git branch into master?

vote vote


How I would do this

git checkout master git pull origin master git merge test git push origin master 

If I have a local branch from a remote one, I don't feel comfortable with merging other branches than this one with the remote. Also I would not push my changes, until I'm happy with what I want to push and also I wouldn't push things at all, that are only for me and my local repository. In your description it seems, that test is only for you? So no reason to publish it.

git always tries to respect yours and others changes, and so will --rebase. I don't think I can explain it appropriately, so have a look at the Git book - Rebasing or git-ready: Intro into rebasing for a little description. It's a quite cool feature

vote vote


This is a very practical question, but all the answers above are not practical.


git checkout master git pull origin master git merge test git push origin master 

This approach has two issues:

  1. It's unsafe, because we don't know if there are any conflicts between test branch and master branch.

  2. It would "squeeze" all test commits into one merge commit on master; that is to say on master branch, we can't see the all change logs of test branch.

So, when we suspect there would some conflicts, we can have following git operations:

git checkout test git pull  git checkout master git pull git merge --no-ff --no-commit test 

Test merge before commit, avoid a fast-forward commit by --no-ff,

If conflict is encountered, we can run git status to check details about the conflicts and try to solve

git status 

Once we solve the conflicts, or if there is no conflict, we commit and push them

git commit -m 'merge test branch' git push 

But this way will lose the changes history logged in test branch, and it would make master branch to be hard for other developers to understand the history of the project.

So the best method is we have to use rebase instead of merge (suppose, when in this time, we have solved the branch conflicts).

Following is one simple sample, for advanced operations, please refer to http://git-scm.com/book/en/v2/Git-Branching-Rebasing

git checkout master git pull git checkout test git pull git rebase -i master git checkout master git merge test 

Yep, when you have uppers done, all the Test branch's commits will be moved onto the head of Master branch. The major benefit of rebasing is that you get a linear and much cleaner project history.

The only thing you need to avoid is: never use rebase on public branch, like master branch.

Never do operations like the following:

git checkout master git rebase -i test 

Details for https://www.atlassian.com/git/tutorials/merging-vs-rebasing/the-golden-rule-of-rebasing


vote vote


Neither a rebase nor a merge should overwrite anyone's changes (unless you choose to do so when resolving a conflict).

The usual approach while developing is

git checkout master git pull git checkout test git log master.. # if you're curious git merge origin/test # to update your local test from the fetch in the pull earlier 

When you're ready to merge back into master,

git checkout master git log ..test # if you're curious git merge test git push 

If you're worried about breaking something on the merge, git merge --abort is there for you.

Using push and then pull as a means of merging is silly. I'm also not sure why you're pushing test to origin.

vote vote


I would first make the to-be-merged branch as clean as possible. Run your tests, make sure the state is as you want it. Clean up the new commits by git squash.

Besides KingCrunches answer, I suggest to use

git checkout master git pull origin master git merge --squash test git commit git push origin master 

You might have made many commits in the other branch, which should only be one commit in the master branch. To keep the commit history as clean as possible, you might want to squash all your commits from the test branch into one commit in the master branch (see also: Git: To squash or not to squash?). Then you can also rewrite the commit message to something very expressive. Something that is easy to read and understand, without digging into the code.

edit: You might be interested in

So on GitHub, I end up doing the following for a feature branch mybranch:

Get the latest from origin

$ git checkout master $ git pull origin master 

Find the merge base hash:

$ git merge-base mybranch master c193ea5e11f5699ae1f58b5b7029d1097395196f  $ git checkout mybranch $ git rebase -i c193ea5e11f5699ae1f58b5b7029d1097395196f 

Now make sure only the first is pick, the rest is s:

pick 00f1e76 Add first draft of the Pflichtenheft s d1c84b6 Update to two class problem s 7486cd8 Explain steps better 

Next choose a very good commit message and push to GitHub. Make the pull request then.

After the merge of the pull request, you can delete it locally:

$ git branch -d mybranch 

and on GitHub

$ git push origin :mybranch 
vote vote


Old thread, but I haven't found my way of doing it. It might be valuable for someone who works with rebase and wants to merge all the commits from a (feature) branch on top of master. If there is a conflict on the way, you can resolve them for every commit. You keep full control during the process and can abort any time.

Get Master and Branch up-to-date:

git checkout master git pull --rebase origin master git checkout <branch_name> git pull --rebase origin <branch_name> 

Merge Branch on top of Master:

git checkout <branch_name> git rebase master 

Optional: If you run into Conflicts during the Rebase:

First, resolve conflict in file. Then:

git add . git rebase --continue 

You could abort the rebase anytime with:

git rebase --abort 

Push your rebased Branch:

git push origin <branch_name> 

If you have this branch pushed before, you need to override it with a force push:

git push origin -f <branch_name> 

Before doing so, check always whether your current local branch matches your expectations, because the force push overrides the old one in the remote repository.

Now you've got two options:

  • A) Create a PR (e.g. on GitHub) and merge it there via the UI
  • B) Go back on the command line and merge the branch into master
git checkout master git merge --no-ff <branch_name> git push origin master 


Top 3 video Explaining What is the best (and safest) way to merge a Git branch into master?