1

The documentation at Github-Help: Syncing a Fork shows three commands to keep my GitHub fork in sync with the upstream repo.

git fetch upstream
git checkout master
git merge upstream/master

Can I use the following two commands instead of the above three?

git checkout master
git pull upstream/master

Are the two sets of commands equivalent, or are there differences between them?

LightCC
  • 9,804
  • 5
  • 52
  • 92
Lone Learner
  • 18,088
  • 20
  • 102
  • 200
  • Possible duplicate of [In git how is fetch different than pull and how is merge different than rebase?](http://stackoverflow.com/questions/14894768/in-git-how-is-fetch-different-than-pull-and-how-is-merge-different-than-rebase) – Tim Biegeleisen Nov 15 '16 at 05:36
  • `git pull` = `git fetch` + `git merge`, at least in general – Tim Biegeleisen Nov 15 '16 at 05:36
  • FYI: `git pull upstream/master` is wrong, as the third word (`upstream/master`) must be the name of the *remote*, while `upstream/master` is the name of a *remote-tracking branch*. Git unfortunately uses very similar words that mean very different (albeit related) things: the word *branch* has at least two meanings, the word *remote* by itself has one meaning, and the phrase *remote-tracking branch* has yet another. – torek Nov 15 '16 at 05:42
  • The second set works if you cloned with `git clone -o upstream` or fetched `upstream` at least once. Note that if you cloned, your master branch probably already tracks `upstream/master`, in which case `git pull` is sufficient. Git sets things up for you. However if you created a local copy from scratch (`git init`) and added the remote after (`git remote add upstream `), you need to indicate `upstream/master` unless you setup the tracking yourself (`git branch --set-upstream-to upstream/master`). – Fabien Bouleau Oct 04 '17 at 08:38

1 Answers1

1

These command sets are not equivalent.

git pull

is split into two commands:

git fetch
git merge

The problem is, that git fetch requires a remote reference, while git merge requires a tracking reference, this is why the Github help page has:

git fetch upstream

but it has

git merge upstream/master

The merge command will take the upstream/master branch and merge it into the currently checked out branch (in this case 'master'). But the fetch command doesn't work on a branch, it requires a remote, so when you try:

git pull upstream/master

Git splits this into:

git fetch upstream/master
git merge upstream/master

which will fail on the fetch:

$ git pull upstream/master
fatal: 'upstream/master' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
LightCC
  • 9,804
  • 5
  • 52
  • 92
  • **Note**: `git pull upstream/master` works if you did `git fetch upstream` at least once: `upstream/master` references the local copy of the remote branch, not the copy on the remote server. – Fabien Bouleau Oct 04 '17 at 08:34
  • @FabienBouleau That's not correct on my system. Both a fetch and pull fail using `upstream/master`, even when it's already present. Fetch needs a remote reference, not a branch reference. Perhaps if you added the name "upstream/master" as a remote with the same URL/folder as "upstream", or with some other config changes, but not by default, at least when upstream is the secondary remote (i.e. origin is added first). – LightCC Oct 04 '17 at 14:15
  • My bad, the notation `upstream/master` is wrong in that case. It must be `git pull upstream master` or `git fetch upstream master`. And the first time you download the remote repository it must be `git fetch upstream` (without `master`) or the remote-tracking branch info will not be set (only `FETCH_HEAD`). Use `upstream/master` only to refer to the remote-tracking branch once it exists. – Fabien Bouleau Oct 04 '17 at 14:25