Fetch
The 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.
Syntax
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 merge
command.
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)
Next, running 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.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Git on Codecademy
- Course
Learn Git & GitHub
Use our beginner friendly Git course to integrate Git and GitHub and manage versions of your projects using Git branches.With CertificateBeginner Friendly4 hours - Course
Learn the Command Line
Learn about the command line, starting with navigating and manipulating the file system, and ending with redirection and configuring the environment.With CertificateBeginner Friendly4 hours