Fetching a Remote Branch

Updated

fetch is a Git command for downloading objects and refs from a remote repository and updating the corresponding remote-tracking refs. It does not merge those changes into your current branch or rewrite your working files.

Here's a common scenario: you've stepped away from a project, and you aren't sure if you've made changes because you operate on multiple machines.

You run:

git fetch origin main

Git fetches the remote's main branch and updates origin/main according to the configured fetch refspec. Now, on your machine, you can have your current local main and the last fetched state of the remote branch (origin/main) at the same time!

Here are two more options:

1. Inspect the remote-tracking branch directly

git switch --detach origin/main

This puts you in detached HEAD state, which is useful for looking around. If you make commits you want to keep, create a branch before moving away.

2. Create a local branch from that remote-tracking ref

git switch -c <new-local-branch> --track origin/<remote-branch-name>

The --track is often inferred when the start point is a remote-tracking branch, but writing it makes the relationship explicit.