Git - git fetch

git

What is the purpose of the 'git fetch' command?

Alice can peek at what Bob did without merging first, using the "fetch" command; this allows Alice to inspect what Bob did, using a special symbol "FETCH_HEAD", in order to determine if he has anything worth pulling, like this:

git fetch /home/bob/myrepo master
git log -p HEAD..FETCH_HEAD

This operation is safe even if Alice has uncommitted local changes. The range notation "HEAD..FETCH_HEAD" means "show everything that is reachable from the FETCH_HEAD but exclude anything that is reachable from HEAD". Alice already knows everything that leads to her current state (HEAD), and reviews what Bob has in his state (FETCH_HEAD) that she has not seen with this command.

If Alice wants to visualize what Bob did since their histories forked she can issue the following command:

gitk HEAD..FETCH_HEAD

How can we fetch changes from a remote branch into a local branch?

git fetch user@example.com:my-repository.git/ remote-branch:local-branch

How can we get any changes from a remote repository?

You fetch them: git fetch. At this point they are in your repository and you can examine them using git log origin (git log HEAD..origin to see just the changes you don't have in your branch), diff them, and obviously, merge them - just do git merge origin. Note that if you don't specify a branch to fetch, it will conveniently default to the tracking remote.

Since you frequently just fetch + merge the tracking remote branch, there is a command to automate that: git pull

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License