The Wayback Machine - https://web.archive.org/web/20250221152803/https://www.geeksforgeeks.org/how-to-checkout-remote-branch-in-git/
Open In App

How to Checkout Remote Branch in Git?

Last Updated : 13 May, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

When collaborating on projects using Git, you'll often need to switch between different versions of the codebase stored in remote repositories. These versions are managed through branches. To work on a specific version (branch) hosted remotely ( on GitHub), you need to check it out in your local Git repository.

Approach 1: Detaching the HEAD

This approach checks out the remote branch directly without creating a corresponding local branch. Your local HEAD pointer is attached to the remote branch.

Step 1: Fetch the Remote Branch (if necessary) :

If your local repository doesn't have information about the remote branch, use `git fetch` to download information:

git fetch origin  # Fetches all branches from the default remote "origin"

To fetch only a specific remote branch:

git fetch origin <branch_name>  # Replace `<branch_name>` with the actual branch name

Step 2: Checkout the Remote Branch

Use the following command, specifying the remote branch name prefixed with `origin/`:

git checkout origin/<branch_name>

Example: To checkout the remote branch `feature/new-design` from the remote repository `origin`:

git checkout origin/feature/new-design
git-checkout

This method creates a new local branch that tracks the remote branch. This allows you to work on the remote branch locally and easily synchronize your changes with the remote repository.

Step 1: Fetch the Remote Branch (if necessary) - Same as previous approach

Step 2: Checkout and Create a Local Branch

Use the `git checkout -b` command to create a new local branch named `<local_branch_name>` that tracks the remote branch `<branch_name>`.

git checkout -b <local_branch_name> origin/<branch_name>

Example: Create a local branch named `my-feature` to track the remote branch `feature/new-design`:

git checkout -b my-feature origin/feature/new-design
check

Step 3: Verification

After checking out the remote branch using either approach, use `git branch` to verify:

git branch

The currently checked-out branch will have an asterisk (*) next to its name.

git-branch

Choosing the Right Approach

  • Detaching the HEAD can be useful in specific cases, like temporary work on a remote branch without affecting your local branches. However, it's generally less common.
  • Creating a local tracking branch is the recommended approach for most situations. It keeps your local repository organized and simplifies synchronization with the remote repository.

Additional Considerations

  • If a local branch with the same name as the remote branch already exists, `git checkout` might behave differently. It's recommended to either delete the local branch beforehand or use `git checkout -b` to create a new tracking branch.
  • Detached HEAD doesn't have a local branch to commit to. You'll need to use `git checkout <branch_name>` to switch to a local branch before committing your changes.

Next Article
Article Tags :

Similar Reads

three90RightbarBannerImg