企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] # Forks and Clones - Fork和clone一个开源的计划 ## Forks 你已经在电脑上建立了一个项目并且 推送 push 到 Github 上,但有趣的不仅如此,更重要的是与人们在不同的专案中合作。 当你 **fork** 一个 程式库 repository 时,实际上是复制了一份 repository 到自己的GitHub 帐号下,换句话说,你的fork 成为了一个 远端 **remote** 代码库 repository。通常我们通过 forks 用来建立自己所需使用的修改版本,或是协助原始项目修正错误、新增功能。 当你成功 forked 某个项目后,便可以从GitHub 将它 下载 **clone**到你的电脑上。这样一来就可以在没有网络的状况下,修改本地电脑里的项目内容。 ![](https://box.kancloud.cn/8129f4b24b2d0da2f46f258655b3cac8_552x551.png) ## 步骤:Fork Patchwork 代码库 Repository 我们接下来要使用的专案是[github.com/jlord/patchwork](https://github.com/jlord/patchwork) 。到页面上点击右上方的 fork 按钮,当 fork的动画完成时,你的帐号中就会出现一份Patchwork 项目的副本。接下来,把在右边侧栏的HTTPS clone URL 复制起来。 ## 步骤:在本地电脑上 下载 Clone Fork 现在,在命令行下载 clone 代码库 repository 到你的电脑上,他会自动替 代码库 repository建立一个新的文件夹,所以你不需要自己建立一个。但请注意不要在另一个Git程式库 repository 文件夹中 下载 clone!所以,如果你还在先前挑战所使用的'hello-world' 代码库 repository中,请先离开那个资料夹。 你可以透过切换文件夹指令以及两个点来移动到文件夹的上一层: ``` $ cd .. ``` 然后 下载 clone: ``` $ git clone <URL_FROM_GITHUB> ``` 切换到刚刚建立的 fork 资料夹(在这个例子中叫做'patchwork'): ``` $ cd patchwork ``` 现在你已经在电脑上得到了一份 代码库 repository 的副本,并且被自动连接到你GitHub 帐号下的 远端 remote 代码库 repository(你的 fork 副本)。 ### 每次`git clone`不需输入账号密码的方法 最`git clone`,每次都要输入帐号和密码,感觉不舒服,于是乎就做了如下设置,然后就可以开心的提交啦~ Linux 或者 Mac 下方法: 创建文件,进入文件,输入内容: ~~~ $ cd ~ $ touch .git-credentials $ vim .git-credentials https://{username}:{password}@github.com ~~~ 在终端下输入: ``` $ git config --global credential.helper store ``` 打开`~/.gitconfig`文件,会发现多了一项: ``` [credential] helper = store ``` 这个时候输入命令 `git clone http://username@url` 时不需要输入密码,即可完成代码的 git ## 步骤:连接到原始的 代码库 Repository 但如果原始项目的内容有改变了呢?你会希望能够收取 **pull**这些变更。所以让我们来新增另外一个远端 remote连结到原始的项目[github.com/jlord/patchwork](https://github.com/jlord/patchwork) ,代码库 repository 的URL 可以在GitHub 原始项目的侧边栏下方找到。 你可以随意替这个 远端 remote 连接命名,**但大家通常用'upstream',让我们也用这个名字**: ``` $ git remote add upstream https://github.com/jlord/patchwork.git ``` ## github中origin和upstream的区别 ![origin_upstream repo](https://box.kancloud.cn/7c0e87b741048e25e2ab962bdb557375_446x463.png) 下面一段是来自 GitHub pages 的解释: *When a repo is cloned, it has a default remote called origin that points to your fork on GitHub, not the original repo it was forked from. To keep track of the original repo, you need to add another remote named upstream git remote add upstream git://github.com/user/repo_name.git* 总结下来: 1. 如果是 upstream repo,你只可以拉取最新代码(即 git fetch ),从而保证你本地的仓库与源仓库同步 2. 如果是 origin repo,就是你自己的repo(自己创建的,或者 fork 的项目)你可以做 任何推拉操作(pull and push) 3. 你可以通过 pull request 向 upstream repo 贡献代码 ## Tips 增加远端 remote 连接 ``` $ git remote add <REMOTE_NAME> <URL> ``` 远端 remote 连接 ``` $ git remote -v ```