Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to switch to a different remote branch in git

Tags:

git

I have 3 local and 3 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
like image 317
sanon Avatar asked Apr 20 '11 21:04

sanon


People also ask

How do I switch from one remote branch to another?

In order to switch to a remote branch, make sure to fetch your remote branch with “git fetch” first. You can then switch to it by executing “git checkout” with the “-t” option and the name of the branch.

How do I switch to a new branch?

New Branches The git branch command can be used to create a new branch. When you want to start a new feature, you create a new branch off main using git branch new_branch . Once created you can then use git checkout new_branch to switch to that branch.


2 Answers

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.

like image 105
buildAll Avatar answered Sep 22 '22 23:09

buildAll


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

like image 24
dahlbyk Avatar answered Sep 22 '22 23:09

dahlbyk