如何在 git 中切换到不同的远程分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5736987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:41:56  来源:igfitidea点击:

How to switch to a different remote branch in git

git

提问by sanon

I have 3 local and 3 remote branches and want to be on the same branch on both.

我有 3 个本地分支和 3 个远程分支,并且希望在这两个分支上都位于同一个分支上。

on local:

在本地:

git branch
  A
* B
  master

git branch -r
  origin/A
  origin/B
  origin/master

on remote:

在远程:

git branch
  A
  B
* master

I am able to commit, push and pull B but my update hook deploys master instead of B, I suppose because the remote branch is still set to master. I created branch B using:

我能够提交、推送和拉取 B,但我的更新挂钩部署的是 master 而不是 B,我想是因为远程分支仍然设置为 master。我使用以下方法创建了分支 B:

git branch B
git checkout B
git push origin B

采纳答案by dahlbyk

As far as I know, there's no way to change a remote's current branch with git push. Pushing will just copy your local changes up into that repository. Typically remotes you push to should be --bare, without a working directory (and thus no "current branch").

据我所知,没有办法用git push. 推送只会将您的本地更改复制到该存储库中。通常,您推送到的遥控器应该是--bare,没有工作目录(因此没有“当前分支”)。

回答by buildAll

Below is my method to switch and work for a remote branch of a git repository.

下面是我切换并为 git 存储库的远程分支工作的方法。

Have a look for all the branches first, just input following command in the terminal:

首先查看所有分支,只需在终端中输入以下命令:

git branch --all

And then you will see the all the branches on local and remote. Something like this:

然后你会看到本地和远程的所有分支。像这样的东西:

*master
remotes/origin/develop
remotes/origin/master
remotes/origin/web
remotes/origin/app

Let's pretend you want to switch to the remotes/origin/developbranch. Type following:

假设您想切换到remotes/origin/develop分支。键入以下内容:

git checkout remotes/origin/develop

Then type git branch --allagain to find this:

然后git branch --all再次输入以找到:

*(detached from remotes/origin/develop)
master
remotes/origin/develop
remotes/origin/master
remotes/origin/web
remotes/origin/app

And then just do:

然后就这样做:

git checkout -b develop

From now on, you are working on the remotes/origin/developbranch exactly.

从现在开始,您将remotes/origin/develop完全在分支上工作。