sanon
sanon

Reputation: 6491

How to switch a remote repository to a different branch

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

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:

git branch B
git checkout B
git push origin B

Upvotes: 34

Views: 90566

Answers (3)

SAB
SAB

Reputation: 309

To switch to a remote repo
git branch -r ## list all the branches including the remote branches
git switch <branchname>

Upvotes: 8

buildAll
buildAll

Reputation: 822

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

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/develop branch. Type following:

git checkout remotes/origin/develop

Then type git branch --all again to find this:

*(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/develop branch exactly.

Upvotes: 65

dahlbyk
dahlbyk

Reputation: 77530

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").

Upvotes: 7

Related Questions