Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git clone a branch, not master

I use Git Bash for bitbucket. I have created a brunch and I pushed some commits, other people pushed commits in master. Now I am on a different machine. I want to clone or download the solution of the latest commit of my branch and not the master? How can I do that?

like image 909
mister_giga Avatar asked Jan 31 '17 19:01

mister_giga


People also ask

How do I clone a branch other than main?

git-remote add To clone a branch without fetching other branches, you can use the git-remote add command with git-fetch. The following example first creates and initializes an empty git repository. Then it adds a remote named origin for the specified repository and fetches the specified branch from the origin.

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.


1 Answers

I think you are looking for the --branch option to git clone, which allows you to specify which branch is initially checked out in the cloned repository.

git clone --branch mybranch $URL/foo

is roughly equivalent to

git clone $URL/foo
cd foo
git checkout mybranch
cd ..
like image 195
chepner Avatar answered Sep 23 '22 08:09

chepner