git 如何分叉我自己的 GitHub 存储库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3772684/
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 04:36:01  来源:igfitidea点击:

How can I fork my own GitHub repository?

gitgithubgit-fork

提问by Fuego DeBassi

So, total newbie to Git. Been reading through the guides and think I have the basics but am having difficulties accomplishing this one goal.

所以,完全是 Git 的新手。通读了指南并认为我有基础知识,但在实现这一目标时遇到了困难。

I have a repo created for my generic markup source code. Just stuff I reuse for every breakout. It's called markupDNA.git

我为我的通用标记源代码创建了一个 repo。只是我在每次突破时重复使用的东西。它被称为markupDNA.git

I would like to have different directories in my mac sites dir ~/Sites/project-N. Where I build upon the generic stuff and do a breakout of a site. I would like these to be tied to my main git repo as forks, but you cannot fork your own repo?

我想在我的 mac 站点 dir 中有不同的目录~/Sites/project-N。在那里我建立在通用的东西上并突破一个网站。我希望将这些作为分支绑定到我的主要 git 仓库,但您不能分支自己的仓库?

I wish I could do something like this:

我希望我能做这样的事情:

git clone <url> name
git add .
# make changes
git commit -m 'whatever'
git push

But I don't want it to push to origin. I want it to push to a fork of the markupDNArepo whence it was cloned. But it seems like it just pushes my changes right up into the origin master. The idea is to keep the markupDNAclean and just have a lot of forks for my different projects, each of which will have their own cloned dir on my hard drive.

但我不希望它推到原点。我希望它推送到markupDNA它被克隆的repo 的一个分支。但它似乎只是将我的更改直接推送到原始主机。我们的想法是保持markupDNA干净,并且为我的不同项目提供很多分支,每个项目在我的硬盘上都有自己的克隆目录。

Any ideas?

有任何想法吗?

采纳答案by Brian Campbell

It will probably be a lot easier to use branches, rather than using separate forks. You can still have separate checkouts for each branch; just clone your repo multiple times, and use git checkoutin each one to switch it to the appropriate branch (or git checkout -bto create the branch and check it out all at once). Once you have created the branches, you can push them to GitHub using git push origin <branchname>.

使用分支可能会容易得多,而不是使用单独的分叉。您仍然可以为每个分行单独结账;只需多次克隆您的 repo,并git checkout在每个repo 中使用将其切换到适当的分支(或git checkout -b创建分支并一次性检查所有)。创建分支后,您可以使用 将它们推送到 GitHub git push origin <branchname>

回答by kubi

You're doing the right thing.

你在做正确的事。

cd ~/Sites/
git clone ~/Dev/markupDNA/ project-N
cd project-N
git remote rename origin markupDNA
  • Nav to the folder where you store your projects
  • clone your base markupDNArepo with custom name
  • rename the remote so that if you want to an 'origin' later, you can
  • 导航到您存储项目的文件夹
  • markupDNA使用自定义名称克隆您的基本存储库
  • 重命名遥控器,以便以后如果您想要“原点”,您可以

回答by Iwazaru

This tutorialgives a simple and straightforward answer :

本教程给出了一个简单明了的答案:

  1. Create a new empty github forkedreporepository
  2. Clone it locally:

    git clone https://github.com/YOURUSERNAME/forkedrepo.git
    
  3. Add the original github repository as remote of the new local repository:

    git remote add upstream https://github.com/YOURUSERNAME/originalrepo.git
    
  4. Pull down a copy of the original github repository to your new local repository:

    git pull upstream master
    
  5. Push the files from your new local repository to new github repository:

    git push origin master
    
  1. 创建一个新的空 githubforkedrepo仓库
  2. 在本地克隆它:

    git clone https://github.com/YOURUSERNAME/forkedrepo.git
    
  3. 添加原始 github 存储库作为新本地存储库的远程:

    git remote add upstream https://github.com/YOURUSERNAME/originalrepo.git
    
  4. 将原始 github 存储库的副本拉到新的本地存储库中:

    git pull upstream master
    
  5. 将文件从新的本地存储库推送到新的 github 存储库:

    git push origin master
    

This won't be recognized by github as a fork of the original repository, off course, but this is as good as it gets.

当然,这不会被 github 识别为原始存储库的分支,但这已经足够了。

回答by funroll

Surprised no-one has referenced this guy's blog postyet.

令人惊讶的是,还没有人参考过这个人的博客文章

Here's the relevant steps:

以下是相关步骤:

$ git clone [email protected]:YOURNAME/foo.git bar
$ cd bar
$ vim .git/config
[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = [email protected]:YOURNAME/bar.git #replace foo with bar
$ git remote add upstream [email protected]:YOURNAME/foo.git
$ git push -u origin master

Instead of editing the config, I usually use a combination of git remote removeand git remote add.

相反编辑配置的,我通常使用的组合git remote removegit remote add

You could also use git remote renamefollowed by git remote addif you wanted to keep the upstream origin around.

如果您想保留上游原点,也可以使用git remote rename后跟git remote add

回答by cnd

to make true GitHub fork of own repository you can use this steps:

要使自己的存储库真正的 GitHub 分支,您可以使用以下步骤:

  1. Create an organization
  2. Fork to organization
  3. Rename forked project
  4. Move back to your account
  1. 创建一个组织
  2. 分叉到组织
  3. 重命名分叉项目
  4. 移回您的帐户

回答by Peter Tillemans

Sure you can clone from a clone. In git there is no concept of a main repo. People often designate a main repo, but that is by convention, not because of a technical reason.

当然,您可以从克隆进行克隆。在 git 中没有主 repo 的概念。人们通常会指定一个主仓库,但这是约定俗成的,而不是因为技术原因。

So you can do everything exactly as you describe and even more.

因此,您可以完全按照您的描述做任何事情,甚至更多。

For example it makes sense to keep a branch in your breakout sites with ideas for additions or modifications to your generic stuff. You can pull these up towards your generic repo and distribute again from there.

例如,在您的突破站点中保留一个分支,其中包含对您的通用内容进行添加或修改的想法是有意义的。您可以将它们拉到您的通用存储库中,然后从那里再次分发。

回答by mihow

You can also use Github's import tool.

你也可以使用Github 的导入工具

(it's made for importing from SVN, etc. but you can use it for this too)

(它是为从 SVN 等导入而制作的,但您也可以将其用于此目的)

  • Create new repository in Github. IMPORTANT: don't "initialize with a README"
  • On the next screen click "Import code"
  • Paste the URL to the Github repo you want to import. (screenshot)
  • 在 Github 中创建新的存储库。重要提示:不要“用 README 初始化”
  • 在下一个屏幕上单击“导入代码”
  • 将 URL 粘贴到要导入的 Github 存储库。(截图

Again, this is creating a new repository and not a true fork on Github. But all your branches, history, etc will be there.

同样,这是在创建一个新的存储库,而不是 Github 上的真正分支。但是你所有的分支、历史等等都会在那里。