Reputation: 7063
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.
Upvotes: 33
Views: 92924
Reputation: 26315
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:
fork/HEAD
)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.
Upvotes: 3
Reputation: 136
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.
Upvotes: 4
Reputation: 60245
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".
Upvotes: 1
Reputation: 36337
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
git clone http://repository
), thengit checkout awesome-branchname
), git checkout -b new-even-more-awesome-branch-name
)Upvotes: 52