-1

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.

  • Read the accepted answer in the duplicate link [here](https://stackoverflow.com/questions/1709177/git-pull-a-certain-branch-from-github). – Tim Biegeleisen Feb 24 '19 at 06:45
  • @TimBiegeleisen Could you remove your duplicate - that question considers situation that is much more complex than mine and I am a beginner, so I am asking the answer to this concrete example. –  Feb 24 '19 at 06:47
  • It covers the same situation as yours actually. – Tim Biegeleisen Feb 24 '19 at 06:48
  • @TimBiegeleisen If you can't help me, then let someone else do it. –  Feb 24 '19 at 06:49

2 Answers2

1

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.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

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

Utsav Patel
  • 2,789
  • 1
  • 16
  • 26