Remote Git Repositories | git fetch
The git fetch command
git fetch downloads branches from a remote repository into the local repository. The fetched branches are not normal local branches; they are stored as remote-tracking branches. This lets you inspect changes before integrating them into your local repository.
Usage
git fetch <remote>
Fetches all branches from the specified repository. Related commits and files are also downloaded.
git fetch <remote> <branch>
Works like the command above, but fetches only the specified branch.
Additional notes
Fetching is what you do when you want to check another developer’s work. Fetched branches are represented as remote-tracking branches, so they do not affect your local development work. You can inspect the fetched branch before integrating it into the local repository, making fetch a safe operation. It is similar to SVN’s svn update in the sense that it lets you check progress in the central repository, but it does not actually integrate the branch into your local branch.
Remote-tracking branches
Remote-tracking branches are similar to local branches, but they point to branches in another developer’s repository. You can check out a remote-tracking branch like a local branch, but doing so puts the repository into a detached HEAD state, similar to checking out an old commit. In other words, you can think of remote-tracking branches as read-only branches.
To list remote-tracking branches, run git branch with the -r flag. Their names are prefixed with the remote connection name, so they are not confused with local branches:
git branch -r
# origin/master
# origin/develop
# origin/some-feature
You can inspect these branches with normal git checkout and git log commands. If the changes in a remote-tracking branch are acceptable, merge them into the local repository with git merge. Unlike SVN, synchronizing a local repository with a remote repository is a two-step process: fetch, then merge. git pull is a shortcut for that two-step process.
Example
This example shows a common workflow for synchronizing a local repository with the central repository’s master branch.
git fetch origin
The downloaded branches are displayed:
a1e8fb5..45e66a4 master -> origin/master
a1e8fb5..9e8ab1c develop -> origin/develop
* [new branch] some-feature -> origin/some-feature
As shown in the tutorial diagram, commits in remote-tracking branches are displayed differently from local branch commits. After running git fetch, you have access to all branches from the remote repository.
Git Tutorial: git fetch
To inspect commits added to master in the central repository, run git log against the master branch range:
git log --oneline master..origin/master
The following commands accept the changes and merge them into the local master branch:
git checkout master
git log origin/master
Then merge origin/master.
git merge origin/master
Now origin/master and master point to the same commit, and synchronization with the central repository is complete.