您的位置:首页 > 编程语言

Mac之Git/GitHub使用(3)——Fork A Repo

2014-04-07 15:22 525 查看
2014-04-07 wcdj

摘要:前两篇文章《Mac之Git/GitHub使用(1)——Set
up Git 》、《Mac之Git/GitHub使用(2)——Create A Repo》了解后,我们已经可以创建和使用自己的GitHub了。但是GitHub用处不只是当做一个version control system (VCS)工具来用,其更大的作用是Social
Coding的理念。本文继续介绍下在GitHub上如何Fork和学习别人的repo。

Fork

Creating a fork is producing a personal copy of someone else's project. Forks act as a sort of bridge between the original repository and your personal copy. You can submit Pull Requests to help make other people's projects better by offering your changes
up to the original project. Forking is at the core of social coding at GitHub.

Contributing to a project

At some point you may find yourself wanting to contribute to someone else's project, or would like to use someone's project as the starting point for your own. This is known as "forking". For this tutorial, we'll be using theSpoon-Knife
project, hosted on GitHub.com.

当我们在GitHub上发现别人一个不错的repo时,例如,Spoon-Knife(This repository is meant to provide an example forforking a repository on GitHub.)这个project,我们如何fork它呢?

Step 1: Fork the "Spoon-Knife" repository

To fork this project, click the "Fork" button in the GitHub.com repository.



点击Fork后,会发现这个Spoon-Knife repo已经成为我的一个私人repo。



Step 2: Clone your fork

You've successfully forked the Spoon-Knife repository, but so far it only exists on GitHub. To be able to work on the project, you will need to clone it to your local machine.

是的,在我们成功Fork后需要把这个repo clone到我们本地的计算机上(Clones your fork of the repository into the current directory in terminal),即把代码checkout到当前执行命令时的目录下,执行如下命令。



Step 3: Configure remotes

When a repository is cloned, it has a default remote called
origin
that points to your fork on GitHub, not the original repository it was forked from. To keep track of the original repository, you need to add another remote named
upstream
:



一旦我们执行完repo的clone命令后,我们会默认分配一个remote对应远程的GitHub上的repo,其在本地的名字是origin。如果我们想与original的repo保存同步更新,则需要添加另外一个remote,名字叫upstream,请执行如下命令。



注意:这里需要修改origin或upstream的配置,为了使用SSH的安全验证,否则https的方式无法在terminal验证通过:

gerryyang@mba:Spoon-Knife$git remote rm origin
gerryyang@mba:Spoon-Knife$git remote add origin git@github.com:gerryyang/Spoon-Knife.git


More Things You Can Do

You've successfully forked a repository, but get a load of these other cool things you can do:

我们已经成功地Fork了一个repo后,就可以做如下几个事情了。

(1) Push commits

Once you've made some commits to a forked repository and want to push it to your forked project, you do it the same way you would with a regular repository:



是的,在Fork和clone完repo后,我们就可以commit自己的文件了(方法和普通的repo是一样的)。并且,GitHub再次对commit的内容做了一次强调,在GitHub上不要上传过大的文件,包括一些库、二进制文件、压缩包等。为了测试,对README.md文件进行修改,在最后添加一句话,然后通过git status可以查看本地文件是否有改动,并可以使用git diff来对比所修改的文件。



确认本地修改的文件没问题后,就可以commit和push操作了。



之后就可以在GitHub上看到刚执行的commit操作。



(2) Pull in upstream changes

If the original repository you forked your project from gets updated, you can add those updates to your fork by running the following code:

如果发现我们fork的original repo提交了新的更新,则我们可以使用下面命令将所有更新merge到我们fork的repo中。

git fetch upstream
# Fetches any new changes from the original repository
git merge upstream/master
# Merges any changes fetched into your working files


关于pull和fetch & merge的区别:



(3) Create branches

Branching allows you to build new features or test out ideas without putting your main project at risk. In git, branch is a sort of bookmark that references the last commit made in the branch. This makes branches very small and easy to work with.

如果我们想尝试开发一些新的功能和特性,或者在多人共同开发的时候,可以选择创建不同的分支来完成。

How do I use branches?

Branches are pretty easy to work with and will save you a lot of headaches, especially when working with multiple people. To create a branch and begin working in it, run these commands:

git branch mybranch
# Creates a new branch called "mybranch"
git checkout mybranch
# Makes "mybranch" the active branch

Alternatively, you can use the shortcut:

git checkout -b mybranch
# Creates a new branch called "mybranch" and makes it the active branch

To switch between branches, use
git checkout
.

git checkout master
# Makes "master" the active branch
git checkout mybranch
# Makes "mybranch" the active branch

Once you're finished working on your branch and are ready to combine it back into the
master
branch, use
merge
.

git checkout master
# Makes "master" the active branch
git merge mybranch
# Merges the commits from "mybranch" into "master"
git branch -d mybranch
# Deletes the "mybranch" branch

Tip: When you switch between branches, the files that you work on (the "working copy") are updated to reflect the changes in the new branch. If you have changes you have not committed, git will ensure you do not lose them. Git is
also very careful during merges and pulls to ensure you don't lose any changes.
When in doubt, commit early and commit often.


(4) Pull requests

If you are hoping to contribute back to the original fork, you can send the original author apull request.

(5) Unwatch the main repository

When you fork a particularly popular repository, you may find yourself with a lot of unwanted updates about it. To unsubscribe from updates to the main repository, click the "Unwatch" button on themain repository and select "Not Watching".



(6) Delete your fork

At some point you may decide that you want to delete your fork. To delete a fork, just follow the same steps as you would todelete a regular repository.

Celebrate

You have now forked a repository. What do you want to do next?

Set Up Git
Create A Repository
Fork A Repository
Be Social

参考

[1]
https://help.github.com/articles/fork-a-repo
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: