所以,你了解如何使用 git。你有一个 GitHub 代码仓库,并且可以推送代码。一切都很好。但是,你究竟如何为别人的 GitHub 项目做贡献呢? 这就是我在学习 git 和 GitHub 之后想要了解的。在本文中,我将解释如何 fork 一个 git 代码仓库,进行更改,并提交拉取请求。
当你想参与 GitHub 项目时,第一步是 fork 代码仓库。

使用我的演示代码仓库来尝试一下。
到达那里后,点击右上角的 Fork 按钮。这将在你的 GitHub 用户账户下创建我的演示代码仓库的一个新副本,URL 类似于
https://github.com/<YourUserName>/demo
这个副本包含来自原始代码仓库的所有代码、分支和提交。
接下来,通过在你的计算机上打开终端并运行以下命令来克隆代码仓库
git clone https://github.com/<YourUserName>/demo
一旦代码仓库被克隆,你需要做两件事
-
通过发出以下命令创建一个新分支
git checkout -b new_branch
-
使用以下命令为上游代码仓库创建一个新的 remote
git remote add upstream https://github.com/kedark3/demo
在本例中,“上游代码仓库”指的是你从中 fork 的原始代码仓库。
现在你可以对代码进行更改。以下代码创建一个新分支,进行任意更改,并将其推送到 new_branch
$ git checkout -b new_branch
Switched to a new branch ‘new_branch’
$ echo “some test file” > test
$ cat test
Some test file
$ git status
On branch new_branch
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
test
nothing added to commit but untracked files present (use "git add" to track)
$ git add test
$ git commit -S -m "Adding a test file to new_branch"
[new_branch (root-commit) 4265ec8] Adding a test file to new_branch
1 file changed, 1 insertion(+)
create mode 100644 test
$ git push -u origin new_branch
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 918 bytes | 918.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
Remote: Create a pull request for ‘new_branch’ on GitHub by visiting:
Remote: http://github.com/example/Demo/pull/new/new_branch
Remote:
* [new branch] new_branch -> new_branch
一旦你将更改推送到你的代码仓库,比较 & 拉取请求 按钮将出现在 GitHub 中。

opensource.com
点击它,你将被带到此屏幕

通过点击 创建拉取请求 按钮来打开一个拉取请求。这允许代码仓库的维护者审查你的贡献。从这里,如果你的贡献良好,他们可以合并它,或者他们可能会要求你进行一些更改。
TLDR(太长不看)
总而言之,如果你想为一个项目做贡献,最简单的方法是
- 找到你想贡献的项目
- Fork 它
- 克隆到你的本地系统
- 创建一个新分支
- 进行你的更改
- 将其推送回你的代码仓库
- 点击 比较 & 拉取请求 按钮
- 点击 创建拉取请求 以打开一个新的拉取请求
如果审查者要求更改,请重复步骤 5 和 6 以向你的拉取请求添加更多提交。
祝你编程愉快!
5 条评论