How can I fork a branch instead of a complete repository? I wish to copy only the branch https://github.com/COMSYS/contiki/tree/split-buffer and not the complete repository https://github.com/jenshiller/contiki. How to fork a branch in Git? won't work in my case. I don't want to clone the branch.
-
July 2022: This is possible, but only for the [*default* branch of a repository](https://stackoverflow.com/a/73142169/6309). – VonC Jul 27 '22 at 17:21
4 Answers
In github (and in git's mental framework) you clone and fork repositories.
There's no way to fork a branch; that doesn't make sense. Just fork the project, and work off the branch you're interested in. You don't lose anything by doing so.
"Working off a branch" usually means you
- clone a repository (e.g.
git clone http://repository
), then - check out the branch you're interested in (
git checkout awesome-branchname
), - and create a new branch based of that (
git checkout -b new-even-more-awesome-branch-name
)

- 34,677
- 4
- 53
- 94
-
2The problem is when the forked repo has a gazillion of branches and you don't want these to drown yours on the server side. – MarcH Dec 05 '20 at 05:05
-
3You can choose the branch to check out at cloning (`git clone -b branchname http://repo`) , and limit the depth of your clone, too (`--depth N`), @MarcH. That doesn't change the fact you're cloning a *repository*, not a *branch*. It just means that your forked repo doesn't contain *all* branchens with *all* history. Personal advise here: a metric ton of branches isn't a problem, you only work with the branches you care about; don't do that shallow clone unless you know what you're doing and the full clone is really prohibitively expensive in terms of storage or data transfer. – Marcus Müller Dec 05 '20 at 10:05
-
2The question is about forking not cloning. A ton of branches is a problem when you you look for one you don't remember the name of. – MarcH Dec 07 '20 at 18:10
I usually go for the option described in How do I clone a single branch in Git?
git clone <url> --branch <branch> --single-branch [<folder>]
It is quick, clean, and if I want to create a new repo out of the branch it's also very easy to do.

- 126
- 2
- 13
First of all, Marcus is completely right in his explanation. You can only fork a repo. However, you can get the end result of what you want by running single-line piped command after your clone your fork locally. It's not a single-step operation performed by Github itself, though.
The end result you want is a fork with only 1 branch in it. To get this, run this command. Here, I'm assuming the remote name for your fork is fork
:
git branch -r --list 'fork/*' | \
grep -v "$(git rev-parse --abbrev-ref fork/HEAD)" | \
sed 's~fork/~~' | \
grep -v split-buffer | \
xargs git push fork --delete
Note that for the git rev-parse
part of the command to work, you will have to run this beforehand:
git remote set-head fork master
The reason we do this is because not every repo has their HEAD set to master
. Some use staging
, or develop
, or something else. So this basically is a little more flexible than just assuming the remote's main branch is master
. We have to filter out the HEAD branch on the remote side because you cannot remotely delete the main branch.
The end result of running the command above is that you will have at most two branches remaining on the remote:
- The main branch (in this case, the symbolic ref referred to by
fork/HEAD
) - The branch you want to keep
Note that if the branch you want to keep also happens to be the HEAD branch, you'll be left with just 1 branch.
I would suggest making a script for this if you do it often. You can name it something like git-delete-all-except.sh
, and generalize it to take a remote name (instead of assuming fork
) and maybe a list of branches to keep instead of just one.
This is a dangerous operation. You absolutely would not want to run this on a fork or other type of repo that you or someone else has been actively already working on and pushing changes to! I recommend you do this only on freshly-forked repositories that have had no changes made yet.

- 24,859
- 31
- 132
- 243
git clone --single-branch u://r/l
clones just the main branch,
git clone -b $branch --single-branch u://r/l
clones only the specific branch you want.
But you don't need the option, as with git clone
itself it's just shorthand for some easy setup.
Here's git clone u://r/l
:
git init l; cd $_
git remote add -f origin u://r/l
and here's git clone --single-branch -b whatever u://r/l
:
git init l; cd $_
git remote add -ft whatever origin u://r/l
and the -f
option is just shorthand for "do a git fetch
afterwards".

- 55,082
- 5
- 77
- 137