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?
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?
git clone --single-branch --branch <branchname> <remote-repo>
The --single-branch
option is valid from version 1.7.10 and later.
Please see also the other answer which many people prefer.
You may also want to make sure you understand the difference. And the difference is: by invoking git clone --branch <branchname> url
you're fetching all the branches and checking out one. That may, for instance, mean that your repository has a 5kB documentation or wiki branch and 5GB data branch. And whenever you want to edit your frontpage, you may end up cloning 5GB of data.
Again, that is not to say git clone --branch
is not the way to accomplish that, it's just that it's not always what you want to accomplish, when you're asking about cloning a specific branch.
git fetch
does not — it does not check out files, but that's not about the transfer. –
Paradise --depth 1
so that you only get the latest. This can save a lot of downloading time. –
Annelieseannelise git clone -b branch_name --single-branch 'repo_url'
–
Orazio clone
with --no-checkout
at all, and then set
a filter with sparse-checkout
with a particular --branch
as well to just clone a sub-set of the files in the branch that match your filter, which can even be just a list of file paths that you want (you may want to use --no-cone
). This set of features is currently evolving from git
v2.24 to v2.34. You may need to check which features you've got in your version. –
Discounter 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
--single-branch
; git 2.5 is out at time of writing this. Don't care for older versions. –
Ssm git checkout -b <Branch_Name>
git pull origin <Branch_Name>
, it works with me xD or, simiply git fetch <Branch_Name>
git checkout <Branch_Name>
–
Beckner -b
option requires a separate --single-branch
flag? Does -b
alone clones all branches? –
Ensheathe --single-branch
IS NOT required. As you can see in the manpages, git clone "...creates remote-tracking branches for each branch in the cloned repository" and "--[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." You save some time in big repos I guess. –
Misteach --single-braches
aren't required to work remind that <3 –
Dwight cd
into the repo if you cloned with HEAD pointing to the right branch. –
Outwear --single-branches
in which case you get only the commits leading to that branch and no other branches. Use git log --graph --oneline --all
to see the whole tree. –
Misteach git clone [email protected]:user/myproject.git/my-branch
–
Mitzi --single-branch
will cause future git fetch
calls to only fetch that branch. You may find yourself later wondering why your tools don't show all the remote branches that you KNOW exist! Even after you fetch. –
Considerable git clone --single-branch --branch <branchname> <remote-repo>
The --single-branch
option is valid from version 1.7.10 and later.
Please see also the other answer which many people prefer.
You may also want to make sure you understand the difference. And the difference is: by invoking git clone --branch <branchname> url
you're fetching all the branches and checking out one. That may, for instance, mean that your repository has a 5kB documentation or wiki branch and 5GB data branch. And whenever you want to edit your frontpage, you may end up cloning 5GB of data.
Again, that is not to say git clone --branch
is not the way to accomplish that, it's just that it's not always what you want to accomplish, when you're asking about cloning a specific branch.
git fetch
does not — it does not check out files, but that's not about the transfer. –
Paradise --depth 1
so that you only get the latest. This can save a lot of downloading time. –
Annelieseannelise git clone -b branch_name --single-branch 'repo_url'
–
Orazio clone
with --no-checkout
at all, and then set
a filter with sparse-checkout
with a particular --branch
as well to just clone a sub-set of the files in the branch that match your filter, which can even be just a list of file paths that you want (you may want to use --no-cone
). This set of features is currently evolving from git
v2.24 to v2.34. You may need to check which features you've got in your version. –
Discounter Here is a really simple way to do it :)
Clone the repository
git clone <repository_url>
List all branches
git branch -a
Checkout the branch that you want
git checkout <name_of_branch>
git clone <repository_url>
you're getting ALL branches...! then switch to <name_of_branch>
. –
Noheminoil To clone a branch without fetching other branches:
mkdir $BRANCH
cd $BRANCH
git init
git remote add -t $BRANCH -f origin $REMOTE_REPO
git checkout $BRANCH
remote add
and checkout
as here, then git remote rm origin
to clean up.) –
Fussbudget -f
from the git remote command, then using git fetch --depth=1 $BRANCH $TAG
, then git checkout FETCH_HEAD
. The init is innocuous, and changing tags will automatically update the checked out code. –
Vinaigrette git version 2.9.2
–
Califate Use:
git checkout -b <branch-name> <origin/branch_name>
For example in my case:
git branch -a
* master
origin/HEAD
origin/enum-account-number
origin/master
origin/rel_table_play
origin/sugarfield_customer_number_show_c
So to create a new branch based on my enum-account-number branch, I do:
git checkout -b enum-account-number origin/enum-account-number
After you hit Return, the following happens:
Branch enum-account-number set up to track remote branch refs/remotes/origin/enum-account-number.
Switched to a new branch "enum-account-number"
git pull origin
first so that git branch -a
can list all new (current) remote branches. –
Cupcake git fetch
is better so that the auto merge doesn't happen, though. –
Abnaki Create a branch on the local system with that name. e.g. say you want to get the branch named branch-05142011
git branch branch-05142011 origin/branch-05142011
It'll give you a message:
$ git checkout --track origin/branch-05142011
Branch branch-05142011 set up to track remote branch refs/remotes/origin/branch-05142011.
Switched to a new branch "branch-05142011"
Now just checkout the branch like below and you have the code
git checkout branch-05142011
git fetch origin [remote-branch]:[new-local-branch]
, I love that! –
Stackhouse git branch ue5-early-access origin/ue5-early-access
and it errors: "fatal: Not a valid object name: 'origin/ue5-early-access'.", any tip? –
Inflight git --branch <branchname> <url>
But Bash completion don't get this key: --branch
© 2022 - 2024 — McMap. All rights reserved.