30

I am new to git version control and I dont know how to clone / pull a specific branch of a repo . Trying to get the branch master of the project, but it defaults to branch test_1

I have tried using the command git clone but it grabbed default test_1. I have also tried reading other questions but the commands are confusing me and I dont want to break anything.

How do i clone the master branch of this project so i can make changes and push to it?

Jeff P
  • 325
  • 1
  • 3
  • 5
  • 6
    Possible duplicate of [How to clone a specific Git branch?](http://stackoverflow.com/questions/1911109/how-to-clone-a-specific-git-branch) – osowskit Dec 20 '16 at 01:19
  • I don't see how this answer using `master` isn't what you are looking for. http://stackoverflow.com/a/4568323/1375964 – osowskit Dec 20 '16 at 01:46
  • @JeffP Be polite. If the linked answers do not help you, simply state why and give more details. – jadhachem Dec 20 '16 at 02:16

7 Answers7

44

You can use the following flags --single-branch && --depth to download the specific branch and to limit the amount of history which will be downloaded.

You will clone the repo from a certain point in time and only for the given branch

git clone -b <branch> --single-branch <url> --depth <number of commits>

--[no-]single-branch


Clone only the history leading to the tip of a single branch, either specified by the --branch option or the primary branch remote’s HEAD points at.

Further fetches into the resulting repository will only update the remote-tracking branch for the branch this option was used for the initial cloning. If the HEAD at the remote did not point at any branch when --single-branch clone was made, no remote-tracking branch is created.


--depth

Create a shallow clone with a history truncated to the specified number of commits

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
31

you can use this command for particular branch clone :

git clone <url of repo> -b <branch name to be cloned>

Eg: git clone https://www.github.com/Repo/FirstRepo -b master
aks7
  • 431
  • 3
  • 12
  • 6
    `-b` doesn't specify the branch to clone, it specifies the branch to checkout after the clone. – 1615903 Dec 20 '16 at 08:53
  • 2
    That is simply not true, see [documentation](https://git-scm.com/docs/git-clone#git-clone--bltnamegt). The whole repository is cloned and after that, the branch specified is checked out. – 1615903 Oct 16 '17 at 08:17
  • yeah thats true – aks7 Oct 09 '20 at 20:43
8

To clone a particular branch in a git repository

- Command: git clone "repository_url" -b "branch_name"
- Example: git clone https://gitlab.com/amm.kazi/yusufoverseas.git -b routes

Tahir77667
  • 2,290
  • 20
  • 16
6

I don't think you fully understand how git by default gives you all history of all branches.

git clone --branch master <URL> will give you what you want.

But in fact, in any of the other repos where you ended up with test_1 checked out, you could have just done git checkout master and it would have switched you to the master branch.

(What @CodeWizard says is all true, I just think it's more advanced than what you really need.)

Mort
  • 3,379
  • 1
  • 25
  • 40
5

a git repository has several branches. Each branch follows a development line, and it has its origin in another branch at some point in time (except the first branch, typically called master, that it starts as the default branch until someone changes, what almost never happens)

If you are new with git, remember those 2 fundamentals. Now, you just need to clone the repository, and it will be in some branch. if the branch is the one you are looking for, awesome. If not, you just need to change to the other branch - this is called checkout. Just type git checkout <branch-name>

In some cases you want to get updates for a specific branch. Just do git pull origin <branch-name> and it will 'download' the new commits (changes). If you didn't do any changes, it should go easy. If you also introduced changes on that branches, conflicts may appear. let me know if you need more info on this case also

pedrorijo91
  • 7,635
  • 9
  • 44
  • 82
1

Please try like this : git clone --single-branch --branch <branchname> <url>

replace <branchname> with your branch and <url> with your url.

url will be like http://username@git.test.com:portno/yourrepo.git.

Abhishek Kumar
  • 820
  • 10
  • 18
0

You may try this

git clone --single-branch --branch <branchname> host:/dir.git
Ariful Islam
  • 186
  • 1
  • 4