I have a local Git repository called 'skeleton' that I use for storing project skeletons. It has a few branches, for different kinds of projects:
casey@agave [~/Projects/skeleton] git branch * master rails c c++
If I want to check out the master branch for a new project, I can do
casey@agave [~/Projects] git clone skeleton new Initialized empty Git repository in /Users/casey/Projects/new/.git/
and everything is how I want it. Specifically, the new master branch points to the skeleton master branch, and I can push and pull to move around changes to the basic project setup.
What doesn't work, however, is if I want to clone another branch. I can't get it so that I only pull the branch I want, for instance the rails
branch, and then the new repository has a master
branch that pushes to and pulls from the skeleton repository's rails
branch, by default.
Is there a good way to go about doing this? Or, maybe this isn't the way that Git wants me to structure things, and I'm certainly open to that. Perhaps I should have multiple repositories, with the Ruby on Rails skeleton repository tracking the master skeleton repository? And any individual project cloning the Ruby on Rails skeleton repository.
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.
You can clone a specific branch from a Git repository using the git clone –single-branch –branch command. This command retrieves all the files and metadata associated with one branch. To retrieve other branches, you'll need to fetch them later on.
Cloning a Single Branch Using git clone The classic git clone command with the --single-branch option will clone only the master branch by default. If you want to clone another branch, you should add the --branch flag with the name of the desired branch.
If you have a single remote repository, then you can omit all arguments. just need to run git fetch , which will retrieve all branches and updates, and after that, run git checkout <branch> which will create a local copy of the branch because all branches are already loaded in your system.
Note: the git1.7.10 (April 2012) actually allows you to clone only one branch:
# clone only the remote primary HEAD (default: origin/master) git clone <url> --single-branch # as in: git clone <url> --branch <branch> --single-branch [<folder>]
(<url>
is the URL if the remote repository, and does not reference itself the branch cloned)
You can see it in t5500-fetch-pack.sh
:
test_expect_success 'single branch clone' ' git clone --single-branch "file://$(pwd)/." singlebranch '
Tobu comments that:
This is implicit when doing a shallow clone.
This makesgit clone --depth 1
the easiest way to save bandwidth.
And since Git 1.9.0 (February 2014), shallow clones support data transfer (push/pull), so that option is even more useful now.
See more at "Is git clone --depth 1
(shallow clone) more useful than it makes out?".
"Undoing" a shallow clone is detailed at "Convert shallow clone to full clone" (git 1.8.3+)
# unshallow the current branch git fetch --unshallow # for getting back all the branches (see Peter Cordes' comment) git config remote.origin.fetch refs/heads/*:refs/remotes/origin/* git fetch --unshallow
As Chris comments:
the magic line for getting missing branches to reverse
--single-branch
is (git v2.1.4):
git config remote.origin.fetch +refs/heads/*:refs/remotes/origin/* git fetch --unshallow
With Git 2.26 (Q1 2020), "git clone --recurse-submodules --single-branch
" now uses the same single-branch option when cloning the submodules.
See commit 132f600, commit 4731957 (21 Feb 2020) by Emily Shaffer (nasamuffin
).
(Merged by Junio C Hamano -- gitster
-- in commit b22db26, 05 Mar 2020)
clone
: pass --single-branch during --recurse-submodulesSigned-off-by: Emily Shaffer
Acked-by: Jeff KingPreviously, performing "
git clone --recurse-submodules --single-branch
" resulted in submodules cloning all branches even though the superproject cloned only one branch.Pipe
--single-branch
through the submodule helper framework to make it to 'clone
' later on.
One way is to execute the following.
git clone user@git-server:project_name.git -b branch_name /your/folder
Where branch_name
is the branch of your choice and "/your/folder" is the destination folder for that branch. It's true that this will bring other branches giving you the opportunity to merge back and forth.
Update
Now, starting with Git 1.7.10, you can now do this
git clone user@git-server:project_name.git -b branch_name --single-branch /your/folder
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With