如何查看远程 Git 分支?

发布于 2024-08-12 07:37:16 字数 267 浏览 13 评论 0原文

有人使用 git Push origin test 将名为 test 的分支推送到共享存储库。我可以使用 gitbranch -r 看到分支。如何查看远程 test 分支?我尝试过:

  • git checkout test,它没有执行任何操作
  • git checkout origin/test给出*(无分支)

Somebody pushed a branch called test with git push origin test to a shared repository. I can see the branch with git branch -r. How do I check out the remote test branch? I've tried:

  • git checkout test, which does nothing
  • git checkout origin/test gives * (no branch)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

扬花落满肩 2024-08-19 07:37:17

要获取所有远程分支,请使用以下命令:

git fetch --all

然后签出该分支:

git checkout test

To get all remote branches, use this:

git fetch --all

Then check out to the branch:

git checkout test
叹倦 2024-08-19 07:37:17

在我看来,没有人提出最简单的方法(或者也许我太笨了,认为这是“一种方法”)。但无论如何,试试这个:

git pull origin remoteBranchName
git switch remoteBranchName

这在同样的情况下对我有用(在我上次拉取请求后在远程创建的分支)。

It seems to my that no one suggested the simplest way (or maybe I'm too dumb to think this is "a way"). But anyway, try this:

git pull origin remoteBranchName
git switch remoteBranchName

This worked for me in the same case (a branch created on the remote after my last pull request).

对你的占有欲 2024-08-19 07:37:17

对于我们来说,似乎 remote.origin.fetch 配置出现了问题。因此,除了 master 之外,我们看不到任何其他远程分支,因此 git fetch [--all] 没有帮助。 git checkout mybranch 和 git checkout -b mybranch --track origin/mybranch 都不起作用,尽管它肯定是在远程。

之前的配置只允许获取 master

$ git config --list | grep fetch
remote.origin.fetch=+refs/heads/master:refs/remotes/origin/master

使用 * 修复它并从 origin 获取新信息:

$ git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'

$ git fetch
...
 * [new branch] ...
...

现在我们可以 git checkout本地远程分支。

我不知道这个配置如何最终出现在我们的本地存储库中。

For us, it seems the remote.origin.fetch configuration gave a problem. Therefore, we could not see any other remote branches than master, so git fetch [--all] did not help. Neither git checkout mybranch nor git checkout -b mybranch --track origin/mybranch did work, although it certainly was at remote.

The previous configuration only allowed master to be fetched:

$ git config --list | grep fetch
remote.origin.fetch=+refs/heads/master:refs/remotes/origin/master

Fix it by using * and fetch the new information from origin:

$ git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'

$ git fetch
...
 * [new branch] ...
...

Now we could git checkout the remote branch locally.

I don't have any idea how this configuration ended up in our local repository.

微暖i 2024-08-19 07:37:16

根据配置的是一个还是多个远程存储库,答案会有所不同。这样做的原因是,对于单个远程情况,可以简化一些命令,因为歧义较少。

针对 Git 2.23 进行了更新:对于旧版本,请参阅末尾的部分。

使用一个远程

在这两种情况下,首先从远程存储库获取数据,以确保您已下载所有最新更改。

$ git fetch

这将为您获取所有远程分支。您可以通过以下方式查看可用于签出的分支:

$ git branch -v -a

...
remotes/origin/test

remotes/* 开头的分支可以被视为远程分支的只读副本。要在分支上工作,您需要从中创建一个本地分支。这是通过 Git 命令 switch (自 Git 2.23 开始)通过给它远程分支的名称(减去远程名称)来完成的:

$ git switch test

在这种情况下,Git 正在猜测(可以使用 禁用) --no-guess)表明您正在尝试签出并跟踪具有相同名称的远程分支。

使用多个远程库

在存在多个远程存储库的情况下,需要显式命名远程存储库。

和以前一样,首先获取最新的远程更改:

$ git fetch origin

这将为您获取所有远程分支。您可以通过以下方式查看可用于签出的分支:

$ git branch -v -a

有了远程分支,您现在需要使用 -c 签出您感兴趣的分支以创建新的本地分支:

$ git switch -c test origin/test

有关更多信息使用 git switch

$ man git-switch

在 Git 2.23 之前,

Git 2.23 中添加了 git switch,在此之前 git checkout 用于切换分支。

仅使用单个远程存储库进行签出:

git checkout test

如果配置了多个远程存储库,那么它会变得有点长

git checkout -b test <name of remote>/test

The answer has been split depending on whether there is one remote repository configured or multiple. The reason for this is that for the single remote case, some of the commands can be simplified as there is less ambiguity.

Updated for Git 2.23: For older versions, see the section at the end.

With One Remote

In both cases, start by fetching from the remote repository to make sure you have all the latest changes downloaded.

$ git fetch

This will fetch all of the remote branches for you. You can see the branches available for checkout with:

$ git branch -v -a

...
remotes/origin/test

The branches that start with remotes/* can be thought of as read only copies of the remote branches. To work on a branch you need to create a local branch from it. This is done with the Git command switch (since Git 2.23) by giving it the name of the remote branch (minus the remote name):

$ git switch test

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

With Multiple Remotes

In the case where multiple remote repositories exist, the remote repository needs to be explicitly named.

As before, start by fetching the latest remote changes:

$ git fetch origin

This will fetch all of the remote branches for you. You can see the branches available for checkout with:

$ git branch -v -a

With the remote branches in hand, you now need to check out the branch you are interested in with -c to create a new local branch:

$ git switch -c test origin/test

For more information about using git switch:

$ man git-switch

Prior to Git 2.23

git switch was added in Git 2.23, prior to this git checkout was used to switch branches.

To checkout out with only a single remote repository:

git checkout test

if there are multiple remote repositories configured then it becomes a bit longer

git checkout -b test <name of remote>/test
伏妖词 2024-08-19 07:37:16

旁注:使用现代 Git (>= 1.6.6),您可以使用 just

git checkout test

(注意它是 'test' 而不是 'origin/test')来执行神奇的 DWIM-mery 并为您创建本地分支“测试”,其上游将是远程的-跟踪分支“起源/测试”。


gitbranch 输出中的 * (nobranch) 意味着您位于未命名的分支上,处于所谓的“分离 HEAD”状态(HEAD 直接指向提交,而不是对某些本地分支的符号引用)。如果您在此未命名分支上进行了一些提交,则始终可以在当前提交之外创建本地分支:

git checkout -b test HEAD

评论中建议的更现代的方法:

@Dennis:git checkout,例如 git checkout origin/test 会导致分离的 HEAD / 未命名分支,而 git checkout testgit checkout -b test origin/test 结果在本地
分支 test (远程跟踪分支 origin/test 作为上游) –
雅库布·纳伦布斯基 2014 年 1 月 9 日 8:17

强调 git checkout origin/test

Sidenote: With modern Git (>= 1.6.6), you are able to use just

git checkout test

(note that it is 'test' not 'origin/test') to perform magical DWIM-mery and create local branch 'test' for you, for which upstream would be remote-tracking branch 'origin/test'.


The * (no branch) in git branch output means that you are on unnamed branch, in so called "detached HEAD" state (HEAD points directly to commit, and is not symbolic reference to some local branch). If you made some commits on this unnamed branch, you can always create local branch off current commit:

git checkout -b test HEAD

A more modern approach as suggested in the comments:

@Dennis: git checkout <non-branch>, for example git checkout origin/test results in detached HEAD / unnamed branch, while git checkout test or git checkout -b test origin/test results in local
branch test (with remote-tracking branch origin/test as upstream) –
Jakub Narębski Jan 9 '14 at 8:17

emphasis on git checkout origin/test

黑凤梨 2024-08-19 07:37:16

在这种情况下,您可能想要创建一个本地 test 分支来跟踪远程 test 分支:

$ git branch test origin/test

git 的早期版本中,您需要显式的 --track 选项,但现在当您从远程分支分支时,这是默认选项。

要创建本地分支切换到它,请使用:

$ git checkout -b test origin/test

In this case, you probably want to create a local test branch which is tracking the remote test branch:

$ git branch test origin/test

In earlier versions of git, you needed an explicit --track option, but that is the default now when you are branching off a remote branch.

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

$ git checkout -b test origin/test
世态炎凉 2024-08-19 07:37:16

接受的答案对您不起作用?

虽然第一个和选定的答案在技术上正确,但您可能尚未从远程存储库检索所有对象和引用。如果是这种情况,您将收到以下错误:

$ git checkout -b remote_branch origin/remote_branch

致命:git checkout:更新路径与切换分支不兼容。
您是否打算签出无法解析为提交的“origin/remote_branch”?

解决方案

如果您收到此消息,则必须先执行 git fetch origin,其中 origin 是远程存储库的名称,然后再运行 git checkout remote_branch >。下面是一个包含响应的完整示例:

$ git fetch origin
remote: Counting objects: 140, done.
remote: Compressing objects: 100% (30/30), done.
remote: Total 69 (delta 36), reused 66 (delta 33)
Unpacking objects: 100% (69/69), done.
From https://github.com/githubuser/repo-name
   e6ef1e0..5029161  develop    -> origin/develop
 * [new branch]      demo       -> origin/demo
   d80f8d7..359eab0  master     -> origin/master

$ git checkout demo
Branch demo set up to track remote branch demo from origin.
Switched to a new branch 'demo'

如您所见,运行 git fetch origin 检索了我们尚未设置为在本地计算机上跟踪的任何远程分支。从那里,由于我们现在有了对远程分支的引用,我们可以简单地运行 git checkout remote_branch ,我们将获得远程跟踪的好处。

Accepted answer not working for you?

While the first and selected answer is technically correct, there's the possibility you have not yet retrieved all objects and refs from the remote repository. If that is the case, you'll receive the following error:

$ git checkout -b remote_branch origin/remote_branch

fatal: git checkout: updating paths is incompatible with switching branches.
Did you intend to checkout 'origin/remote_branch' which can not be resolved as commit?

Solution

If you receive this message, you must first do a git fetch origin where origin is the name of the remote repository prior to running git checkout remote_branch. Here's a full example with responses:

$ git fetch origin
remote: Counting objects: 140, done.
remote: Compressing objects: 100% (30/30), done.
remote: Total 69 (delta 36), reused 66 (delta 33)
Unpacking objects: 100% (69/69), done.
From https://github.com/githubuser/repo-name
   e6ef1e0..5029161  develop    -> origin/develop
 * [new branch]      demo       -> origin/demo
   d80f8d7..359eab0  master     -> origin/master

$ git checkout demo
Branch demo set up to track remote branch demo from origin.
Switched to a new branch 'demo'

As you can see, running git fetch origin retrieved any remote branches we were not yet setup to track on our local machine. From there, since we now have a ref to the remote branch, we can simply run git checkout remote_branch and we'll gain the benefits of remote tracking.

花伊自在美 2024-08-19 07:37:16

我尝试了上面的解决方案,但没有成功。试试这个,它有效:

git fetch origin 'remote_branch':'local_branch_name'

这将获取远程分支并创建一个名为 local_branch_name 的新本地分支(如果尚不存在)并跟踪其中的远程分支。

I tried the above solution, but it didn't work. Try this, it works:

git fetch origin 'remote_branch':'local_branch_name'

This will fetch the remote branch and create a new local branch (if not exists already) with name local_branch_name and track the remote one in it.

提赋 2024-08-19 07:37:16

您基本上可以看到分支,但您还没有该分支的本地副本!...

您需要获取分支...

您可以简单地获取然后签出到分支,使用下面的一行命令可以做到这一点:

git fetch && git checkout test

我还创建了下图,以便您分享差异,看看 fetch 的工作原理以及它与 pull 的不同之处:

< a href="https://i.sstatic.net/ODFYa.png" rel="noreferrer">git fetch

You basically see the branch, but you don't have a local copy of that yet!...

You need to fetch the branch...

You can simply fetch and then checkout to the branch, use the one line command below to do that:

git fetch && git checkout test

I also created the image below for you to share the differences, look at how fetch works and also how it's different to pull:

git fetch

江湖彼岸 2024-08-19 07:37:16

使用:

git checkout -b <BRANCH-NAME> <REMOTE-NAME>/<BRANCH-NAME>

在我的良性案例中,其他答案不适用于现代 Git。如果远程分支是新的,您可能需要先拉取,但我还没有检查这种情况。

Use:

git checkout -b <BRANCH-NAME> <REMOTE-NAME>/<BRANCH-NAME>

Other answers do not work with modern Git in my benign case. You might need to pull first if the remote branch is new, but I haven't checked that case.

夜吻♂芭芘 2024-08-19 07:37:16

这将为未命名源的远程(文档):

$ git checkout -t remote_name/remote_branch

要添加新的远程,您需要首先执行以下操作:

$ git remote add remote_name location_of_remote
$ git fetch remote_name

第一个告诉 Git 远程存在,第二个获取承诺。

This will DWIM for a remote not named origin (documentation):

$ git checkout -t remote_name/remote_branch

To add a new remote, you will need to do the following first:

$ git remote add remote_name location_of_remote
$ git fetch remote_name

The first tells Git the remote exists, the second gets the commits.

伴随着你 2024-08-19 07:37:16

要克隆 Git 存储库,请执行以下操作:

git clone <either ssh url /http url>

上述命令检查所有分支,但仅初始化 master 分支。如果您想签出其他分支,请执行以下操作:

git checkout -t origin/future_branch (for example)

此命令签出远程分支,并且您的本地分支名称将与远程分支相同。

如果您想在结帐时覆盖本地分支名称:

git checkout -t -b enhancement origin/future_branch

现在您的本地分支名称是 enhancement,但您的远程分支名称是 future_branch

To clone a Git repository, do:

git clone <either ssh url /http url>

The above command checks out all of the branches, but only the master branch will be initialized. If you want to checkout the other branches, do:

git checkout -t origin/future_branch (for example)

This command checks out the remote branch, and your local branch name will be same as the remote branch.

If you want to override your local branch name on checkout:

git checkout -t -b enhancement origin/future_branch

Now your local branch name is enhancement, but your remote branch name is future_branch.

看海 2024-08-19 07:37:16

我总是这样做:

git fetch origin && git checkout --track origin/branch_name

I always do:

git fetch origin && git checkout --track origin/branch_name

走走停停 2024-08-19 07:37:16

对于上述所有建议,我陷入了一种情况,看到 error: pathspec 'desired-branch' did not match any file(s)known to git. 。我使用的是 Git 版本 1.8.3.1。

所以这对我有用

git fetch origin desired-branch
git checkout -b desired-branch FETCH_HEAD

背后的解释是我注意到在获取远程分支时,它被获取到FETCH_HEAD

git fetch origin desired-branch

From github.com:MYTEAM/my-repo
    * branch            desired-branch -> FETCH_HEAD

I was stuck in a situation seeing error: pathspec 'desired-branch' did not match any file(s) known to git. for all of the suggestions above. I'm on Git version 1.8.3.1.

So this worked for me:

git fetch origin desired-branch
git checkout -b desired-branch FETCH_HEAD

The explanation behind is that I've noticed that when fetching the remote branch, it was fetched to FETCH_HEAD:

git fetch origin desired-branch

From github.com:MYTEAM/my-repo
    * branch            desired-branch -> FETCH_HEAD
苏别ゝ 2024-08-19 07:37:16

你可以尝试

git fetch remote
git checkout --track -b local_branch_name origin/branch_name

或者

git fetch
git checkout -b local_branch_name origin/branch_name

You can try

git fetch remote
git checkout --track -b local_branch_name origin/branch_name

or

git fetch
git checkout -b local_branch_name origin/branch_name
你列表最软的妹 2024-08-19 07:37:16

首先,您需要执行以下操作:

git fetch # 如果您不知道分支名称

git fetch origin branch_name

其次,您可以通过以下方式将远程分支检出到本地:

git checkout -b branch_name origin/branch_name

-b will create来自您选择的远程分支的指定名称的新分支。

First, you need to do:

git fetch # If you don't know about branch name

git fetch origin branch_name

Second, you can check out remote branch into your local by:

git checkout -b branch_name origin/branch_name

-b will create new branch in specified name from your selected remote branch.

只是我以为 2024-08-19 07:37:16

git remote show 命令将列出所有分支(包括未跟踪的分支)。然后你就可以找到你需要获取的远程分支名称。

示例:

git remote show origin

使用以下步骤获取远程分支:

git fetch <origin name> <remote branch name>:<local branch name>
git checkout <local branch name > (local branch name should the name that you given fetching)

示例:

git fetch origin test:test
git checkout test

The git remote show <origin name> command will list all branches (including un-tracked branches). Then you can find the remote branch name that you need to fetch.

Example:

git remote show origin

Use these steps to fetch remote branches:

git fetch <origin name> <remote branch name>:<local branch name>
git checkout <local branch name > (local branch name should the name that you given fetching)

Example:

git fetch origin test:test
git checkout test
赤濁 2024-08-19 07:37:16

我使用以下命令:

git checkout --track origin/other_remote_branch

I use the following command:

git checkout --track origin/other_remote_branch
寂寞笑我太脆弱 2024-08-19 07:37:16

命令

git fetch --all
git checkout -b <ur_new_local_branch_name> origin/<Remote_Branch_Name>

等于

 git fetch --all

,然后

 git checkout -b fixes_for_dev origin/development

两者都会从 development 创建一个 latestfixs_for_dev

Commands

git fetch --all
git checkout -b <ur_new_local_branch_name> origin/<Remote_Branch_Name>

are equal to

 git fetch --all

and then

 git checkout -b fixes_for_dev origin/development

Both will create a latest fixes_for_dev from development

冰火雁神 2024-08-19 07:37:16

只需使用远程分支的名称运行 git checkout 即可。 Git 会自动创建一个跟踪的本地分支远程分支:

git fetch
git checkout test

但是,如果在多个远程分支中找到该分支名称,则此操作将不起作用,因为 Git 不知道该使用哪一个。在这种情况下,您可以使用:

git checkout --track origin/test

git checkout -b test origin/test

2.19,Git 学习了 checkout.defaultRemote< /code>配置,它指定在解决此类歧义时默认使用的远程。

Simply run git checkout with the name of the remote branch. Git will automatically create a local branch that tracks the remote one:

git fetch
git checkout test

However, if that branch name is found in more than one remote, this won't work as Git doesn't know which to use. In that case you can use either:

git checkout --track origin/test

or

git checkout -b test origin/test

In 2.19, Git learned the checkout.defaultRemote configuration, which specifies a remote to default to when resolving such an ambiguity.

伤痕我心 2024-08-19 07:37:16

有很多替代方案,例如:

  • 替代方案 1:

    git fetch && git 结账测试
    

    这是最简单的方法。

  • 替代方案 2:

    git fetch
    git 结帐测试
    

    效果相同,但分两步。

There are many alternatives, for example:

  • Alternative 1:

    git fetch && git checkout test
    

    It's the simplest way.

  • Alternative 2:

    git fetch
    git checkout test
    

    It's the same, but in two steps.

世俗缘 2024-08-19 07:37:16

这些答案都不适合我。这有效:

git checkout -b feature/branch remotes/origin/feature/branch

None of these answers worked for me. This worked:

git checkout -b feature/branch remotes/origin/feature/branch
权谋诡计 2024-08-19 07:37:16

如果分支位于 origin 远程之外的其他位置,我喜欢执行以下操作:

$ git fetch
$ git checkout -b second/next upstream/next

这将签出 upstream 远程上的 next 分支到名为 second/next 的本地分支。这意味着如果您已经有一个名为 next 的本地分支,则不会发生冲突。

$ git branch -a
* second/next
  remotes/origin/next
  remotes/upstream/next

If the branch is on something other than the origin remote I like to do the following:

$ git fetch
$ git checkout -b second/next upstream/next

This will checkout the next branch on the upstream remote in to a local branch called second/next. Which means if you already have a local branch named next it will not conflict.

$ git branch -a
* second/next
  remotes/origin/next
  remotes/upstream/next
错々过的事 2024-08-19 07:37:16

git fetch && git checkout 你的分支名称

git fetch && git checkout your-branch-name

乖乖 2024-08-19 07:37:16

gitbranch -r 表示对象名称无效,因为该分支名称不在 Git 的本地分支列表中。使用以下命令从源更新本地分支列表:

git remote update

然后尝试再次检查远程分支。

这对我有用。

我相信 git fetch 会拉入所有远程分支,这不是原始发布者想要的。

git branch -r says the object name is invalid, because that branch name isn't in Git's local branch list. Update your local branch list from origin with:

git remote update

And then try checking out your remote branch again.

This worked for me.

I believe git fetch pulls in all remote branches, which is not what the original poster wanted.

冷默言语 2024-08-19 07:37:16

TL;DR

使用 git switch 而不是 git checkout。更多详细信息请参见此页面

我认为答案已经过时了。 Git 现在将 checkout 的部分功能拆分为 switchrestore

以下是我的总结:

如果您想更新远程分支的某些内容,您应该创建一个本地分支来“跟踪”远程分支。您可以在本地更新任何您想要的内容,最后推送到远程。如果您在克隆存储库后直接检出远程分支,您可能会看到“分离的 HEAD”状态和来自 Git 的以下消息:

Note: switching to 'origin/asd'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at d3e1083 Update a

那么我们如何创建本地分支来跟踪远程分支?

要创建本地分支来跟踪远程分支,您可以使用 git checkout <远程分支名称> 或 git switch <远程分支名称> 。如果你有一个文件或文件夹与你的远程分支名称相同,git checkout会输出一些错误消息,但git switch可以正常工作!

示例:

  1. 查看所有分支,我们要创建一个本地分支来跟踪远程分支remotes/origin/asd,并且我们还有文件名asd

    $ git 分支 -a
    * 掌握
      遥控器/原点/HEAD ->起源/主人
      遥控器/origin/asd
      遥控器/起源/ereres
      遥控器/原点/主控
      遥控器/origin/zxc
    $ls
    自闭症谱系障碍
    
  2. 文件名与远程分支相同,如果我们使用 git checkout 命令创建本地分支来跟踪远程分支,Git 应该输出一些错误消息

    $ git checkout asd
    致命:“asd”既可以是本地文件,也可以是跟踪分支。
    请使用 -- (和可选的 --no-guess)来消除歧义
    
  3. 如果我们使用 <代码>git开关!

    $ git switch ereres
    设置分支“ereres”以跟踪来自“origin”的远程分支“ereres”。
    切换到新分支“ereres”
    
    $ git 分支 -vv
    *ereres 3895036 [origin/ereres] 更新a
      master f9e24a9 [origin/master] 合并分支 'master'
    

TL;DR

Using git switch rather than git checkout. More details are on this page.

I think the answer is obsolete. Git split some functions of checkout to switch and restore now.

The following is my summary:

If you want to update something for a remote branch, you should create a local branch to "track" the remote branch. You can update anything you want in local and finally push to remote. If you check out to the remote branch directly after cloning your repository, you may see the "detached HEAD" status and the following message from Git:

Note: switching to 'origin/asd'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at d3e1083 Update a

So how can we create a local branch to track a remote branch?

To create a local branch to track a remote branch, you can use git checkout <remote branch name> or git switch <remote branch name>. If you have a file or folder has same name as your remote branch name, git checkout would output some error message, but git switch can work normally!

Example:

  1. See all branches, and we want to create a local branch to track the remote branch remotes/origin/asd, and we also have the file name asd:

    $ git branch -a
    * master
      remotes/origin/HEAD -> origin/master
      remotes/origin/asd
      remotes/origin/ereres
      remotes/origin/master
      remotes/origin/zxc
    $ ls
    a  asd
    
  2. The filename is same as remote branch, and Git should output some error messages if we are using the git checkout command to create a local branch to track a remote branch

    $ git checkout asd
    fatal: 'asd' could be both a local file and a tracking branch.
    Please use -- (and optionally --no-guess) to disambiguate
    
  3. It works if we are using git switch!

    $ git switch ereres
    Branch 'ereres' set up to track remote branch 'ereres' from 'origin'.
    Switched to a new branch 'ereres'
    
    $ git branch -vv
    * ereres 3895036 [origin/ereres] Update a
      master f9e24a9 [origin/master] Merge branch 'master' of
    
薔薇婲 2024-08-19 07:37:16

从远程获取并签出分支。

git fetch <remote_name> && git checkout <branch_name> 

例如:

git fetch origin && git checkout 功能/XYZ-1234-Add-alerts

Fetch from the remote and checkout the branch.

git fetch <remote_name> && git checkout <branch_name> 

E.g.:

git fetch origin && git checkout feature/XYZ-1234-Add-alerts

青朷 2024-08-19 07:37:16

其他人给出了解决方案,但也许我可以告诉你原因。

git checkout 测试什么都不做

不等于不起作用,所以我猜当您在终端中输入 'git checkout test' 并按 Enter 键时,没有消息出现,也没有发生错误。我说得对吗?

如果答案是“是”,我可以告诉你原因。

原因是您的工作树中有一个名为“test”的文件(或文件夹)。

git checkout xxx 解析时,

  1. Git 首先将 xxx 视为分支名称,但没有任何名为 test 的分支。
  2. 然后Git认为xxx是一个路径,幸运的是(或者不幸的是),有一个名为test.txt的文件。所以 git checkout xxx 意味着放弃 xxx 文件中的任何修改。
  3. 如果也不存在名为xxx的文件,那么Git会尝试根据一些规则创建xxx。其中一条规则是,如果 remotes/origin/xxx 存在,则创建一个名为 xxx 的分支。

Other guys and gals give the solutions, but maybe I can tell you why.

git checkout test which does nothing

Does nothing doesn't equal doesn't work, so I guess when you type 'git checkout test' in your terminal and press enter key, no message appears and no error occurs. Am I right?

If the answer is 'yes', I can tell you the cause.

The cause is that there is a file (or folder) named 'test' in your work tree.

When git checkout xxx parsed,

  1. Git looks on xxx as a branch name at first, but there isn't any branch named test.
  2. Then Git thinks xxx is a path, and fortunately (or unfortunately), there is a file named test. So git checkout xxx means discard any modification in xxx file.
  3. If there isn't file named xxx either, then Git will try to create the xxx according to some rules. One of the rules is create a branch named xxx if remotes/origin/xxx exists.
我三岁 2024-08-19 07:37:16

获取新创建的分支

git fetch

切换到另一个分支

git checkout BranchName

To get newly created branches

git fetch

To switch into another branch

git checkout BranchName
离鸿 2024-08-19 07:37:16

git checkout -b "Branch_name" [ B 表示创建本地分支]

gitbranch --all

git checkout -b "您的分支名称"

gitbranch

git pull origin "您的分支名称"

成功从 master 签出分支到开发分支

在此处输入图像描述

git checkout -b "Branch_name" [ B means Create local branch]

git branch --all

git checkout -b "Your Branch name"

git branch

git pull origin "Your Branch name"

successfully checkout from the master branch to dev branch

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文