如何在 Github 上創(chuàng)建一個(gè)拉取請(qǐng)求
學(xué)習(xí)如何復(fù)刻一個(gè)倉(cāng)庫(kù),進(jìn)行更改,并要求維護(hù)人員審查并合并它。
你知道如何使用 git 了,你有一個(gè) GitHub 倉(cāng)庫(kù)并且可以向它推送。這一切都很好。但是你如何為他人的 GitHub 項(xiàng)目做出貢獻(xiàn)? 這是我在學(xué)習(xí) git 和 GitHub 之后想知道的。在本文中,我將解釋如何復(fù)刻fork一個(gè) git 倉(cāng)庫(kù)、進(jìn)行更改并提交一個(gè)拉取請(qǐng)求pull request。
當(dāng)你想要在一個(gè) GitHub 項(xiàng)目上工作時(shí),第一步是復(fù)刻一個(gè)倉(cāng)庫(kù)。

Forking a GitHub repo你可以使用我的演示倉(cāng)庫(kù)試一試。
當(dāng)你在這個(gè)頁(yè)面時(shí),單擊右上角的 “Fork”(復(fù)刻)按鈕。這將在你的 GitHub 用戶賬戶下創(chuàng)建我的演示倉(cāng)庫(kù)的一個(gè)新副本,其 URL 如下:
- https://github.com/<你的用戶名>/demo
這個(gè)副本包含了原始倉(cāng)庫(kù)中的所有代碼、分支和提交。
接下來(lái),打開(kāi)你計(jì)算機(jī)上的終端并運(yùn)行命令來(lái)克隆clone倉(cāng)庫(kù):
- git clone https://github.com/<你的用戶名>/demo
一旦倉(cāng)庫(kù)被克隆后,你需要做兩件事:
1、通過(guò)發(fā)出命令創(chuàng)建一個(gè)新分支 new_branch :
- git checkout -b new_branch
2、使用以下命令為上游倉(cāng)庫(kù)創(chuàng)建一個(gè)新的遠(yuǎn)程remote:
- git remote add upstream https://github.com/kedark3/demo
在這種情況下,“上游倉(cāng)庫(kù)”指的是你創(chuàng)建復(fù)刻來(lái)自的原始倉(cāng)庫(kù)。
現(xiàn)在你可以更改代碼了。以下代碼創(chuàng)建一個(gè)新分支,進(jìn)行任意更改,并將其推送到 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
一旦你將更改推送到您的倉(cāng)庫(kù)后, “Compare & pull request”(比較和拉取請(qǐng)求)按鈕將出現(xiàn)在GitHub。

GitHub's Compare & Pull Request button單擊它,你將進(jìn)入此屏幕:

GitHub's Open pull request button單擊 “Create pull request”(創(chuàng)建拉取請(qǐng)求)按鈕打開(kāi)一個(gè)拉取請(qǐng)求。這將允許倉(cāng)庫(kù)的維護(hù)者們審查你的貢獻(xiàn)。然后,如果你的貢獻(xiàn)是沒(méi)問(wèn)題的,他們可以合并它,或者他們可能會(huì)要求你做一些改變。
精簡(jiǎn)版
總之,如果您想為一個(gè)項(xiàng)目做出貢獻(xiàn),最簡(jiǎn)單的方法是:
- 找到您想要貢獻(xiàn)的項(xiàng)目
- 復(fù)刻它
- 將其克隆到你的本地系統(tǒng)
- 建立一個(gè)新的分支
- 進(jìn)行你的更改
- 將其推送回你的倉(cāng)庫(kù)
- 單擊 “Compare & pull request”(比較和拉取請(qǐng)求)按鈕
- 單擊 “Create pull request”(創(chuàng)建拉取請(qǐng)求)以打開(kāi)一個(gè)新的拉取請(qǐng)求
如果審閱者要求更改,請(qǐng)重復(fù)步驟 5 和 6,為你的拉取請(qǐng)求添加更多提交。