简体   繁体   中英

Git - pull origin master after creating local branch

I'm just learning git and can't understand what is happening when we execute git pull origin master after we created new local branch and there are new commits in remote master. Consider the following example:

  1. There is one commit (aaaaaa) in origin master, for example initial commit.
  2. I clone origin master
  3. I create new local branch (new_feature), check out to it, and make commit (bbbbbb) to new_feature.
  4. Someone makes new commit (cccccc) to origin master.

What is happening when I do git pull origin master after step 4? Please, explain.

This Stack Overflow question largely explains what happens when you do a git pull from another remote branch.

# from new_feature
git pull origin master

The above is equivalent to this:

# from new_feature
git fetch origin master && git merge master

But, the kicker here is that in your case Git actually won't execute the above, because the merge is not a fast forward merge. This is because your branches currently look like this:

master:      ... A -- C
                  \
new_feature:       B

That is, merging the C commit on top of B is more than just a simple replay, because there could be merge conflicts.

What you should instead be doing in this situation, and arguably what you always should do in this situation, is to just merge master into your feature branch:

# from new_feature
git fetch origin master
git merge origin/master

Note that there may be merge conflicts, which you would then have to fix, then make a commit to complete the merge.

After the commit, your branch should look like this:

master:      ... A -- C
                  \    \
new_feature:       B -- D

Commit D is a merge commit, and actually has both B and C as parents.

When you run git pull origin master after step 4, git essentially tries to fetch and then merge the changes that are not present in your feature branch.

This process is well explained here

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