简体   繁体   中英

Git Update Local Branch with remote Master

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?

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.

当你在你当前的分支git merge master

如果您将remote设置为默认origin ,(您可以使用git remote -v进行检查),您可以这样做:

git merge origin master

你也可以改变:

git rebase origin/master

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM