Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to git checkout a remote branch named the same as a directory?

Tags:

git

branch

We have a remote branch named deploy for building and testing deploy scripts. Not surprisingly, the deploy scripts end up in a directory called deploy. Now that the directory deploy is in the branch master, when doing an initial clone it's cumbersome to actually check out that branch.

$ git clone bitbucket.org:/myplace/mything
$ cd mything
$ ls
deploy          extensions      installExtensions   src         tests
$ git branch -r | grep dep
  origin/deploy
$ git checkout deploy
$ git branch
* master
$ git checkout origin/deploy
Note: checking out 'origin/deploy'.

You are in 'detached HEAD' state. [SNIP]

At this point should I just create a local branch named deploy and set it to track the remote? Is there any syntax I can give git so it knows I want to checkout a remote branch, not a local path?

like image 239
kojiro Avatar asked May 17 '13 13:05

kojiro


People also ask

How do I checkout a remote branch with a different name?

To be precise, renaming a remote branch is not direct – you have to delete the old remote branch name and then push a new branch name to the repo. Step 2: Reset the upstream branch to the name of your new local branch by running git push origin -u new-branch-name .

How do you checkout a branch that already exists?

First, make sure that the target branch exists by running the “git branch” command. Now that you made sure that your branch exists, you can switch from the master branch to the “feature” branch by executing the “git checkout” command. That's it!

What happens when you checkout a remote branch in git?

Git checkout remote branch is a way for a programmer to access the work of a colleague or collaborator for the purpose of review and collaboration. There is no actual command called “git checkout remote branch.” It's just a way of referring to the action of checking out a remote branch.


2 Answers

You could simply create a new local branch that points to the remote branch using either of these commands (the latter will check it out immediately):

git branch deploy origin/deploy
git checkout -b deploy origin/deploy

This will however not set up the tracking functionality that happens when Git automatically creates a branch for a remote branch. To do that you have to do the following:

git branch -u origin/deploy

As an alternative, you can do this all in a single command, which is the same what Git would automatically do:

git checkout -b deploy --track origin/deploy
like image 173
poke Avatar answered Oct 17 '22 23:10

poke


My workaround for this is

git checkout deploy --

like image 4
Corey Kosak Avatar answered Oct 17 '22 23:10

Corey Kosak