How do I clone a specific Git branch? [duplicate]

Question

Git clone will clone remote branch into local.

Is there any way to clone a specific branch by myself without switching branches on the remote repository?

Answer

git clone -b <branch> <remote_repo>

Example:

git clone -b my-branch [email protected]:user/myproject.git

With Git 1.7.10 and later, add --single-branch to prevent fetching of all branches. Example, with OpenCV 2.4 branch:

git clone -b opencv-2.4 --single-branch https://github.com/Itseez/opencv.git

gpg failed to sign the data fatal: failed to write commit object [Git 2.10.0]

Your branch is ahead of 'origin/master' by 3 commits