一步一步教你完成Github的第一个Contribution
让Roshan Jossey带你花十分钟贡献你第一个开源项目。
Next Stop, GitHub. Image via unsplash.com
前言
如果你没有GitHub的账号,或者还不知道什么是Git,请阅读: 新手开发者? 你早就该学Git了.
见见我们的老师
在此但愿你已经有GitHub的账号,那么就开始贡献你第一个开源项目吧!
我知道,作为一个新开发者,贡献一个项目说起来有点不可思议,虽然当时我也是这样的。我花了很长一段时间才完成我第一个Pull Request。这就是我为什么想要阅读Roshan Jossey的原因。Roshan created First Contributions —一个GitHub仓库,教新手贡献的步骤,也同样提供了一个仓库给你用来贡献第一个第一个项目。
开始你第一个开源贡献
如果你不会英语, 有多达13种语言供你选择: Spanish, Dutch, Hindi, Russian, Japanese, Vietnamese, Polish, Korean, German, Simplified Chinese, Traditional Chinese, Greek, العربية.
1. Fork 一个仓库
First Contributions 通过点击页面右侧的Fork按钮来Fork一个仓库. 这将会复制一个仓库到你的账号中去。
2. 克隆仓库
现在我们克隆这个项目到本地. 点击克隆按钮然后点击 copy to clipboard 图标.
打开命令行工具执行以下命令:
`git clone "url you just copied"`
这里的 “url you just copied” (命令行中不需要冒号) 是仓库地址 First Contributions . 填入在上一步获得到的地址。
例如:
`git clone [https://github.com/this-is-you/first-contributions.git](https://github.com/this-is-you/first-contributions.git)`
这里的 this-is-you
是你的GitHub账户. 复制你first-contributions 这个项目的内容到你的电脑。
3. 创建一个分支
在你的电脑中改变一个仓库文件夹 (如果你还没到这一步):
`cd first-contributions`
创建一个分支使用 git checkout
命令:
`git checkout -b <add-your-name>`
例如:
`git checkout -b add-alonzo-church`
(分支的名称不需要 add 这个词, 但有一种情况可以加上,就是把你的名字加上去。)
4. 做一些改动然后递交
用编辑器打开 Contributors.md
这个文件, 把你的名字加进去然后保存.在这个目录下执行 git status
, 你就会发现已经有变化了. 把改变的文件加入你的分支只需要执行 git add
命令:
`git add Contributors.md`
递交刚才的文件执行 git commit
命令:
`git commit -m "Add <your-name> to Contributors list"`
替换 `` 成你的名字
5. 推送变动内容到GitHub
推送变动执行 git push
:
`git push origin <add-your-name>`
替换 `` 成你之前创建的分支名称
6. 查看你提交的变动
进入GitHub项目主页, 你会看到一个 Compare & pull request
按钮,点击这个按钮.
现在提交你的递交请求.
很快我就会把所有的变动合并到这个项目的主分支,一旦变动合并,你将会受到一个通知邮件。 你项目的主分支将不会有变动,是为了和我的项目保持同步
The master branch of your fork won’t have the changes. In order to keep your fork synchronized with mine, 继续往下看.
7. 保持你的fork和仓库同步
首先,切换主分支
`git checkout master`
然后加上仓库的地址 upstream remote url
:
`git remote add upstream [https://github.com/Roshanjossey/first-contributions](https://github.com/Roshanjossey/first-contributions)`
This is a way of telling git that another version of this project exists in the specified url and we’re calling it upstream
. Once the changes are merged, fetch the new version of my repository:
`git fetch upstream`
Here we’re fetching all the changes in my fork (upstream remote). Now, you need to merge the new revision of my repository into your master branch.
`git rebase upstream/master`
Here you’re applying all the changes you fetched to master branch. If you push the master branch now, your fork will also have the changes:
`git push origin master`
Notice here you’re pushing to the remote named origin.
At this point I have merged your branch `` into my master branch, and you have merged my master branch into your own master branch. Your branch is now no longer needed, so you may delete it:
`git branch -d <add-your-name>`
同样你可以删除你远程仓库
`git push origin --delete <add-your-name>`
这不是必须的, but the name of this branch shows its rather special purpose. Its life can be made correspondingly short.