Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git Update Local Branch with remote Master

Tags:

git

gitlab

I see two possibilities of doing this:

  1. Do a replace of the local branch with the changes from remote master

  2. Follow the work flow that I get using Gitlab by creating a merge request and merge the changes from master branch into the branch that I wish to update to the latest from master

What are the advantages and disadvantages of both these approaches? I'm leaning more towards the first approach. What do you guys say?

like image 499
joesan Avatar asked Jan 07 '16 13:01

joesan


People also ask

How do I sync my branch with GitHub master?

In GitHub Desktop, click Current Branch. Click Choose a branch to merge into BRANCH. Click the branch you want to merge into the current branch, then click Merge BRANCH into BRANCH. Note: If there are merge conflicts, GitHub Desktop will warn you above the Merge BRANCH into BRANCH button.


2 Answers

The simple answer - there are plenty of more complicated ones - is to just do a merge, so:

git checkout master
git pull
git checkout <your-branch>
git merge master

(This is effectively the same as you describe in option 2)

Depending on your settings, you might not need all of those steps (but doing them all won't hurt) - I'd recommend reading up on each of the commands to find the precise workflow that fits you best.

This will merge the changes from master into your branch and probably create a new commit, with a comment making it clear that it's a merge.

The alternative, and slightly more advanced option would be to rebase, instead of merge, which will effectively rewind time to the point at which your branch diverged from master, then pull in the changes on master, bringing your branch in-line with master, but without your commits, and finally apply your commits at the end. The advantage of this is that it keeps the history more simple - you just get a straight line of changes, with the changes from your branch right at the end, rather than two separate branches that join at the point of the merge.

To do that, you'd do:

git checkout <your-branch>
git rebase master

I'd recommend reading the docs on rebase, because there are lots of cases where it gets difficult, and if you're new to git, definitely go for merge, but come back to rebase when you're more confident - it's a very powerful feature, and more like what I think you're describing in your option 1.

like image 110
DaveyDaveDave Avatar answered Oct 15 '22 02:10

DaveyDaveDave


If you remote is set to the default origin, (you can check it by using git remote -v), you could just do:

git merge origin master
like image 33
Michael Chekin Avatar answered Oct 15 '22 04:10

Michael Chekin