fetch command retrieves changes (commits, files, etc.) from another repository or local branch, but does not integrate them. This command allows programmers to bring in and review changes made in a central repository into their local machine.
git fetch <remote-name> <branch-name>
This will bring in changes made in the
<branch-name> that exist in the Git
<remote-name>. However, the changes will not be merged; that must be done with the
Note: Using the
pull command performs a
fetch, followed by a
merge, within the same line.
Fetching From a Remote
A common use case for the
fetch command is bringing in and reviewing changes made in a remote branch. The remote must exist in order for
fetch to be able to access and retrieve the changes made there.
$ git remote -vorigin https://github.com/CodeNinja/docs.git (fetch)origin https://github.com/CodeNinja/docs.git (push)
git fetch on the
main branch of the
origin remote will bring any changes, including any new commits or files, into the current branch:
$ git fetch origin mainFrom https://github.com/CodeNinja/docs* branch main -> FETCH_HEAD
After this point, a
merge command can be used to actually integrate the changes.