Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clone specific branch from git

I have a repo in bitbucket, and i am using git.

So my branches are

master
develop_one
develop_two

When i use git clone [email protected]:project/project.git, i am getting only the master branch code, but i need to clone/pull only develop_one branch, so how to clone/pull only develop_one branch code ?

Note: The other branches(develop_one, develop_two) are not merged in to master, and dont want to merge until the functionality has been completed actually, so want to get/clone/pull only a specific branch code

like image 994
Shiva Krishna Bavandla Avatar asked Nov 17 '13 17:11

Shiva Krishna Bavandla


People also ask

Can you git clone a specific branch?

There are two ways to clone a specific branch. You can either: Clone the repository, fetch all branches, and checkout to a specific branch immediately. Clone the repository and fetch only a single branch.

How do I clone down a specific branch?

In order to clone a specific branch, you have to execute “git branch” with the “-b” and specify the branch you want to clone. $ git clone -b dev https://github.com/username/project.git Cloning into 'project'... remote: Enumerating objects: 813, done.

Can you git fetch a specific branch?

Git fetch commands and options Fetch all of the branches from the repository. This also downloads all of the required commits and files from the other repository. Same as the above command, but only fetch the specified branch.

How do I clone a specific branch in git GUI?

Steps as below: Git GUI -> branch Tab -> checkout -> Tracking branch -> select origin/dev -> checkout -> branch Tab -> create -> name for dev and select this detached checkout -> create. Now you have a local dev branch the same as remote. You can check the history by repository Tab -> visualize dev's history.


2 Answers

Try:

git clone [email protected]:project/project.git -b develop_one --single-branch

For already cloned repos use:

git fetch
git checkout develop_one
# Or more specific:
git checkout --track -b develop_one

This will track the develop_one branch from the remote.

like image 78
tombruijn Avatar answered Oct 02 '22 22:10

tombruijn


Also, to add to @tomrbrujin 's answer, you can specify the name of the local destination directory in the clone command like this:

git clone https://github.com/example/example1.git -b branch_name --single-branch local_name

The final arg passed, local_name would create and name the local copy of the cloned remote directory "local_name."

Otherwise, the local directory name will only reflect the repo name, which here would be: example

like image 24
eulerworks Avatar answered Oct 02 '22 23:10

eulerworks