2

What does the following command do:

git fetch <remote> <branch> && git checkout FETCH_HEAD

I have only one local branch (master) and I execute the above command. What changes to my local repository should I expect and how can I verify (see) them?

Roman
  • 124,451
  • 167
  • 349
  • 456

1 Answers1

5

You are probably aware of what the first part of your command will do. git fetch <remote> <some_branch> will update the local tracking branch of the branch you specify. But this will not alter the actual local corresponding some_branch on your machine.

When you do a git fetch, Git has a special ref called FETCH_HEAD which points to the branch which was just fetched. In this case, it would point to remote/some_branch, since this is the branch which was just fetched. By doing

git checkout FETCH_HEAD

you would be checking out origin/some_branch in a detached HEAD state. This may or not may not be what you intend, but in any case, your compound command would not actually update the local some_branch. To do that, you would need an additional git merge step, or to just do a git pull from some_branch.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Few points: (1) I do not think that I have a "local tracking branch". If I execute "git branch -r" (before and after the fetch) I do not see the branch that I am fetching. (2) I am not sure I understood your explanation of the "git checkout FETCH_HEAD" command. Do you mean that by executing this command I just go to a "local copy" of the remote branch that i have just fetched? – Roman Dec 06 '17 at 13:53
  • Um...you certainly have a local tracking branch, or else the fetch/pull would not be possible. Type `git branch -a` and you will see the local and tracking branches. If you `git checkout FETCH_HEAD` then you would checkout the local tracking branch in the detached HEAD state. If some of the terms I am using are unfamiliar to you, then research them because they come up often in Git lingo. – Tim Biegeleisen Dec 06 '17 at 14:15