Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git merge branch into master

I have a master branch and a working branch_1. I want to 'move' branch_1 exactly as it is to master. So I want something like this:

git checkout master git merge branch_1 # I don't know what is correct... 

Things which I did but I got loads of files messed up with annoying conflicts. So now master contains exactly the same files of branch_1 avoiding any conflicts, just overwriting files. Any help?

like image 450
Trt Trt Avatar asked Jan 30 '13 13:01

Trt Trt


People also ask

How do I merge a branch into master?

First we run git checkout master to change the active branch back to the master branch. Then we run the command git merge new-branch to merge the new feature into the master branch. Note: git merge merges the specified branch into the currently active branch. So we need to be on the branch that we are merging into.

How do I merge one branch to another?

To merge branches locally, use git checkout to switch to the branch you want to merge into. This branch is typically the main branch. Next, use git merge and specify the name of the other branch to bring into this branch.

Can you merge a branch into master multiple times?

Merging a branch multiple times into another works fine if there were changes to merge. Show activity on this post. Actually yes you totally can, when you merge again it will bring over any commits that don't exist on production.


2 Answers

Conflicts are going to happen if both branches have changes to the files. This is a good thing. Keeping your branches up-to-date with each other will prevent some of them . However over all, conflicts are not bad. The rebase option can also prevent many of them from happening.

git merge branch_1 

If you are on master, merging will bring the changes as you expect.

http://www.kernel.org/pub/software/scm/git/docs/git-merge.html

You could also

git rebase branch_1 

This will take the changes from branch_1 and append them to master without a merge commit.

http://www.kernel.org/pub/software/scm/git/docs/git-rebase.html

like image 106
Schleis Avatar answered Sep 24 '22 01:09

Schleis


Maybe you should not merge?

  1. Checkout branch_1
  2. Rebase master changes into branch_1
  3. Fix any errors that might have occured after testing your code
  4. Checkout master
  5. Rebase branch_1 changes into master

or in code:

git checkout branch_1 git rebase master (...) git checkout master git rebase branch_1 

This also gives you the opportunity to squash several commits into one, if you want to make your changesets more dense, and prevents these annoying merge-commits in your history.

like image 22
sjas Avatar answered Sep 24 '22 01:09

sjas