I'm just getting started with git and I have a question. My app has 10 other developers working on it, each one having their own branch like dev_XXXXX. So if I do a clone of the repository, do all of their code gets copied to my machine? In that case I dont want that. Suppose my branch is dev_swamy, how do I then clone just the stable branch and dev_swamy? Thanks.
5 Answers
By default git clone
would fetch all branches, but those branches would be stored as remote-tracking branches: for example branch 'dev_XXXXX' would be stored as 'origin/dev_XXXXX' (with 'refs/remotes/origin/dev_XXXXX' as full name). Those remote-tracking branches wouldn't be visible in git branch
output: you would need git branch -r
to list remote-tracking branches (or git branch -a
to list all branches). If those branches do not diverge too much from mainline, they wouldn't take too much disk space in repository. Therefore I don't see why you want to clone only selected branches.
Nevertheless if you want to have a clone with only two selected branches, you can do it like this:
First, create new empty repository
$ mkdir repoclone $ cd repoclone/ $ git init Initialized empty Git repository in /home/user/repoclone/.git/
Then add your repository under the name 'origin' (just like "git clone" would name it), requesting tracking of only two branches: 'master' and 'dev_swamy', using "git remote" command. Check that it was added correctly.
$ git remote add -t master -t dev_swamy origin user@example.com:repo.git $ git remote origin $ git remote show origin * remote origin Fetch URL: user@example.com:repo.git Push URL: user@example.com:repo.git HEAD branch: master Remote branches: master new (next fetch will store in remotes/origin) dev_swamy new (next fetch will store in remotes/origin)
If the stable branch is called 'stable' rather than 'master', you would have of course to modify above example. Also there is
-m <branch>
option if you want specified branch to be default branch in remote.Fetch from 'origin' (you could do this also by using
-f
option to "git remote add" above):$ git fetch remote: Counting objects: 282, done. remote: Compressing objects: 100% (193/193), done. remote: Total 282 (delta 82), reused 0 (delta 0) Receiving objects: 100% (282/282), 81.30 KiB | 135 KiB/s, done. Resolving deltas: 100% (82/82), done. From user@example.com:repo.git * [new branch] master -> origin/master * [new branch] dev_swamy -> origin/dev_swamy From user@example.com:repo.git * [new tag] v1.0 -> v1.0 * [new tag] v1.0.1 -> v1.0.1 * [new tag] v1.1 -> v1.1
Set up local branch 'master' (where you would do your work) to follow 'origin/master' (to have 'origin/master' as upstream), just like "git clone" would do:
$ git checkout -t origin/master Branch master set up to track remote branch master from origin. Already on 'master'
You can repeat this for branch 'dev_swamy'.
Now you can see how config file looks like. You can get exactly the same result by editing
.git/config
file to look like the following, and then doing "git fetch".$ cat .git/config # or just open this file in your editor [core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true [remote "origin"] url = user@example.com:repo.git fetch = +refs/heads/master:refs/remotes/origin/master fetch = +refs/heads/dev_swamy:refs/remotes/origin/dev_swamy [branch "master"] remote = origin merge = refs/heads/master
Don't forget to introduce yourself to Git before starting work on repository (i.e. set 'user.name' and 'user.email' config variables; usually in per-user config file)!

- 309,089
- 65
- 217
- 230
If you clone, all revisions in all branches are cloned along, but the cloned repository will check out master by default.
Just taking selected branches is trickier since git does not really think you should work that way. You have to pull down the branches manually:
mkdir repoclone
cd repoclone
git init
git remote add origin git://remote/url
git fetch origin master:master
git fetch origin dev_XXX:dev_XXX
Above is what I knew worked. However, if you want to set up a git repo that works as normal, just has a more narrow view of its remote branches? You can do that pretty easily:
mkdir repoclone cd repoclone git init git remote add origin git://remote/url # now open .git/config for editing in your editor # replace the following line (grab all remote branches) fetch = +refs/heads/*:refs/remotes/origin/* # replace with lines listing exactly which branches you want fetch = +refs/heads/master:refs/remotes/origin/master fetch = +refs/heads/dev_XXX:refs/remotes/origin/dev_XXX # save the file, now run git fetch

- 48,117
- 14
- 92
- 101
-
Aha! This is something I've been thinking about for a while - is there a way to clone only one branch. An interesting idea to turn it on it's head and fetch only what you need. But the first fetch generates this error: "fatal: Refusing to fetch into current branch refs/heads/master of non-bare repository" – Steve Folly Oct 23 '09 at 20:50
-
BTW with modern git you can use `git init repoclone` – Jakub Narębski Oct 23 '09 at 22:07
-
@Steve: Ah, you are right. You can replace with "`git fetch origin master; git reset --hard FETCH_HEAD`" to setup master. – u0b34a0f6ae Oct 23 '09 at 22:50
-
@Jakub: "modern"? Not even 1.6.4 supports that. So more like absolutely latest git. – u0b34a0f6ae Oct 23 '09 at 22:52
-
It's easier (in the long run) to add a fetch config for the remote which doesn't contain the default wildcard. Then you can keep the local and remote branches separate in the same way as you would for a normal clone and just use a simple `git fetch` for incremental updates. – CB Bailey Oct 23 '09 at 22:53
-
@charles: You are right and I realized at the same time you wrote your comment. – u0b34a0f6ae Oct 23 '09 at 23:03
Another way to do this is to avoid a direct clone, but instead manually add a remote with a custom set of fetch refspecs.
e.g.
mkdir myclone
cd myclone
git init
git remote add origin url://origin.repo
# Add fetch rules for the branches that we want to track
git config remote.origin.fetch +refs/heads/master:+refs/remotes/origin/master
git config --add remote.origin.fetch +refs/heads/dev_swamy:+refs/remotes/origin/dev_swamy
# fetch now fetches just what we need, subsequently it will do incremental fetches
git fetch
# Creating local branches tracking the remote branches
git checkout -b master origin/master
git branch dev_swamy origin/dev/swamy

- 755,051
- 104
- 632
- 656
Possibly a more uptodate way (using a small random repo found online):
Do a clean checkout of the specific branch, and only that branch.
$ git clone https://github.com/martinmimigames/little-music-player --single-branch --branch=main
Cloning into 'little-music-player'...
remote: Enumerating objects: 1187, done.
remote: Counting objects: 100% (292/292), done.
remote: Compressing objects: 100% (142/142), done.
remote: Total 1187 (delta 114), reused 243 (delta 82), pack-reused 895
Receiving objects: 100% (1187/1187), 1.61 MiB | 9.38 MiB/s, done.
Resolving deltas: 100% (487/487), done.
Go into the checkout:
$ cd little-music-player
Show what branches are known (confirming only the 1 required branch is known):
$ git branch --all
* main
remotes/origin/HEAD -> origin/main
remotes/origin/main
Fetch another branch:
$ git fetch origin m3u:m3u
From https://github.com/martinmimigames/little-music-player
* [new branch] m3u -> m3u
List the known branches again:
$ git branch --all
m3u
* main
remotes/origin/HEAD -> origin/main
remotes/origin/main
To improve performance on large repos, you can add a --depth nnn
option to the checkouts if you're planning to do some work between the 2 branches and you know that there is only recent deviation between them (the value of the depth is upto you).
If you are only intending to operate on specific files within the repo, then the use of sparse checkouts is another option (setup more along the lines of git init
and config changes before doing the fetch - a very different way of doing things).
Hope this helps.

- 3,769
- 30
- 40
I think the more important question here is what others will be pushing, not what you will be cloning or pulling. Since each developer is working on his own branch, another question is how you end up with a common code base. Are the developers merging their branches to master? And are they then pushing their changed master branch to a central repository? If that is the case, there is no way for you to pull the other developers' branches anyway.
If that is not the case, I fail to see how you can form a functioning team.
And as I final thought: I'd be curious to know why you wouldn't want to clone the other developers' branches to your repository?

- 47,505
- 4
- 67
- 87