How to Fetch Repository Updates in Git

January 22, 2022
Cover image for How to Fetch Repository Updates in Git

When branches or changes are made by other developers, it's useful to be fetching those changes from the remote repository before pulling them in locally. A safe way to retrieve thse updates without affecting your local repository is to use the command git fetch.

Fetch Repository Updates from the Remote

To fetch all updates from the remote repository, run the following command:

git fetch

We may have situations where we have references to branches having remote tracking, but they have been deleted on the remote repository. To remove these references that no longer exist before fetching, run:

git fetch --prune
OR
git fetch -p

If we want to target a specific remote repository, we can use:

git fetch [REMOTE]

Example of Fetching from Different Remotes

Sometimes we may have different remote repositories, such as when we decide to fork a repository.

Fetching from the origin we can run:

git fetch origin

If we have another remote called upstream, we can fetch with:

git fetch upstream

Fetch from a Specific Branch

Sometimes we might only care about changes made to a specific branch. Therefore, there is no need to be fetching all the changes that were made to the repository. We can fetch updates from a specific branch with the following command:

git fetch [REMOTE] [BRANCH]

Fetch Repository Updates from all Registered Remotes

If we want to fetch from all registered remote repositories, we can use:

git fetch --all

We may want to see what updates will be coming in when we run git fetch, but not apply them immediately. This is useful to see what changes are coming in before we decide to update our local repository. To do so, we can run:

git fetch --dry-run

Conclusion

Fetching updates made to a git repository is useful to make sure we have the latest changes we can bring into our local repository. This allows developers to obtain each other's work they've contributed to the remote repository. It's a good practice to be running git fetch often when working in teams.