乐闻世界logo
搜索文章和话题

How do i check out a remote git branch

3个答案

1
2
3

The command to view remote Git branches is straightforward. You can use the following commands:

bash
git fetch --all git branch -r

Here is an explanation for each command:

  • git fetch --all:This command fetches the latest data from all remote repositories. It downloads any data that is not present locally but does not automatically merge or modify your current work.
  • git branch -r:This command lists all remote branches. The -r parameter stands for 'remote'.

Sometimes you can use git branch -r alone to view remote branches, but to ensure you see the latest list, it's best to run git fetch --all first. If you're only concerned with a specific remote repository, you can replace git fetch --all with git fetch <remote>.

Additionally, there is a command to view all information about both remote and local branches:

bash
git branch -a

This will display all local and remote branches. Remote branch names typically have a remotes/ prefix. For example, remotes/origin/main represents the main branch on the remote repository named origin.

2024年6月29日 12:07 回复

In this case, you might want to create a local branch named test that tracks the remote test branch:

bash
$ git branch test origin/test

In earlier versions of Git, you needed an explicit --track option, but now when branching from a remote branch, it is the default.

To create a local branch and switch to it, use:

bash
$ git checkout -b test origin/test
2024年6月29日 12:07 回复

According to whether one or multiple remote repositories are configured, the answer varies. This is because for a single remote setup, some commands can be simplified due to less ambiguity.

Updated for Git 2.23: For older versions, please refer to the end section.

Configured with one remote repository

In both cases, first fetch data from the remote repository to ensure you have downloaded all latest changes.

bash
$ git fetch

This will fetch all remote branches. You can view the branches available for checkout with:

bash
$ git branch -v -a

...

Branches starting with remotes/* can be considered read-only copies of remote branches. To work on a branch, you need to create a local branch from it. This is done using the Git command switch (available since Git 2.23) by specifying the remote branch name (without the remote name):

bash
$ git switch test

In this case, Git guesses (can be disabled with --no-guess) that you are trying to checkout and track a remote branch with the same name.

Configured with multiple remote repositories

When multiple remote repositories are configured, you need to explicitly name the remote repository.

As before, first fetch the latest remote changes:

bash
$ git fetch origin

This fetches all remote branches. You can view the branches available for checkout with:

bash
$ git branch -v -a

With remote branches, you now need to check out the branch you're interested in using -c to create a new local branch:

bash
$ git switch -c test origin/test

For more information on git switch, see:

bash
$ man git-switch

Git versions before 2.23

git switch was added in Git 2.23; before that, git checkout was used to switch branches.

For checking out with a single remote repository:

bash
git checkout test

If multiple remote repositories are configured, it becomes a bit longer:

bash
git checkout -b test <name of remote>/test
2024年6月29日 12:07 回复

你的答案