git 如何分叉分支而不是存储库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38564473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 12:07:53  来源:igfitidea点击:

How to fork a branch and not the repository?

gitgithubgit-fork

提问by Shridhar R Kulkarni

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-bufferand 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.

如何分叉分支而不是完整的存储库?我只想复制分支https://github.com/COMSYS/contiki/tree/split-buffer而不是完整的存储库https://github.com/jenshiller/contiki如何在 Git 中分叉分支?在我的情况下不起作用。我不想克隆分支。

回答by Marcus Müller

In github (and in git's mental framework) you clone and fork repositories.

在 github(以及 git 的思维框架)中,您可以克隆和分叉存储库。

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.

没有办法分叉分支;那没有意义。只需 fork 项目,然后在您感兴趣的分支上工作。这样做不会丢失任何东西。

"Working off a branch" usually means you

“在分支机构工作”通常意味着您

  1. clone a repository (e.g. git clone http://repository), then
  2. check out the branch you're interested in (git checkout awesome-branchname),
  3. and create a new branch based of that (git checkout -b new-even-more-awesome-branch-name)
  1. 克隆一个存储库(例如git clone http://repository),然后
  2. 查看您感兴趣的分支 ( git checkout awesome-branchname),
  3. 并基于该 ( git checkout -b new-even-more-awesome-branch-name)创建一个新分支

回答by void.pointer

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.

首先,马库斯的解释是完全正确的。你只能分叉一个回购。但是,您可以通过在本地克隆您的 fork 后运行单行管道命令来获得您想要的最终结果。不过,这不是 Github 本身执行的单步操作。

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:

你想要的最终结果是一个只有 1 个分支的叉子。要获得此信息,请运行此命令。在这里,我假设您的 fork 的远程名称是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-parsepart of the command to work, you will have to run this beforehand:

请注意,要使git rev-parse命令的一部分起作用,您必须事先运行它:

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.

我们这样做的原因是因为并非每个 repo 都将其 HEAD 设置为master. 有些使用staging, 或develop, 或其他东西。所以这基本上比假设远程的主分支是master. 我们必须过滤掉远程端的 HEAD 分支,因为您无法远程删除主分支。

The end result of running the command above is that you will have at most two branches remaining on the remote:

运行上述命令的最终结果是,您将在遥控器上最多保留两个分支:

  1. The main branch (in this case, the symbolic ref referred to by fork/HEAD)
  2. The branch you want to keep
  1. 主分支(在这种情况下,由 引用的符号引用fork/HEAD
  2. 您要保留的分支

Note that if the branch you want to keep also happens to be the HEAD branch, you'll be left with just 1 branch.

请注意,如果您要保留的分支恰好也是 HEAD 分支,那么您将只剩下 1 个分支。

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.

如果您经常这样做,我建议为此编写一个脚本。您可以将其命名为类似git-delete-all-except.sh,并将其概括为采用远程名称(而不是假设fork),并且可能保留一个分支列表而不是一个。

The counter-argument for nothaving this as a script: It's a dangerous operation and 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!

将此作为脚本的反驳:这是一个危险的操作,您绝对不想在您或其他人已经积极致力于并推动更改的分叉或其他类型的存储库上运行它!

回答by jthill

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 cloneitself it's just shorthand for some easy setup.

但是您不需要该选项,因为它git clone本身只是一些简单设置的简写。

Here's git clone u://r/l:

这是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 clone --single-branch -b whatever u://r/l

git init l; cd $_
git remote add -ft whatever origin u://r/l

and the -foption is just shorthand for "do a git fetchafterwards".

-f选项只是“git fetch事后做”的简写。