-1

I have the following scenario:

I have my remote branch equal to my local branch. Then I made some changes in the remote branch and some different changes in my local branch.

My idea is to run git fetch, to sync my local branch, then check the differences, and then push everything, but it simply doesn't work! (git fetch seems to not get the changes made in the remote branch before) I know running git pull "fixes" this, or git push -f but I want to review the changes before commit.

Why doesn't git fetch work as expected? Or how can I otherwise review the differences before commit?

SharpKnight
  • 455
  • 2
  • 14
bakeiro
  • 1
  • 1
  • 1
  • @SurajRao well, my question was more about why in this scenario didn't work as expected, more than the differences between this 2 commands – bakeiro Dec 06 '17 at 11:17
  • 1
    Well, I don't agree that it's a dup of that question because it isn't what was actually asked, but it is true that if you *really* understand the difference between pull and fetch, then your expectation would be different and you wouldn't think that the fetch hadn't worked as expected. – Mark Adelsberger Dec 06 '17 at 11:57
  • `git fetch` does not obtain *changes* (and in fact branches don't *hold* changes). What `git fetch` obtains are *commits* (and each branch name, or any other name in Git, simply points to *one specific commit*). – torek Dec 06 '17 at 15:59

2 Answers2

2

git fetch downloads commits from the remote branch, but doesn't update your workspace. You won't see the commits until they are merged into your local branch.

git pull is a combination of git fetch and git merge - this is why git pull appears to "fix" this behaviour.

Edmund Dipple
  • 2,244
  • 17
  • 12
2

To view the differences between your local branch and the remote branch before merging them, you can use git diff, as outlined in this question, after executing git fetch.

If you want to execute the merge of your remote branch with your local branch without committing it immediately to allow you to review the result, you can use git pull --no-commit (as described on git-scm.com). The --no-commit option prevents Git from automatically committing a merge that may be part of the pull, which would allow you to review the merge result before committing it.

SharpKnight
  • 455
  • 2
  • 14