feedforward 发表于 2022-1-13 00:36:03

如何在github上贡献代码

第1步: 你得注册github账号,并登录
第2部: fork你想贡献代码的项目,此次以https://github.com/apache/incubator-nuttx-apps为例
第3步: 进入fork到你账号下的项目,本次为https://github.com/zouboan/incubator-nuttx-apps,表明fork成功了;
第4步: 在本地新建一个文件夹,随便取个名,本次取名NuttX,并进入;
第5步: 将你fork的项目git到本地,有github的域名污染问题,不能用git clone https://github.com/zouboan/incubator-nuttx-apps.git,得用git clone git://github.com/zouboan/incubator-nuttx-apps.git
第6步: 将git下来的目录名改成你需要的,本次是将incubator-nuttx-apps改成apps以便编译能通过;
第7步: 将被Fork的项目添加为upstream,用以跟踪其所有修改: git remote add upstream git://github.com/zouboan/incubator-nuttx-apps.git
第8步: git pull upstream master执行结果如下:
From git://github.com/zouboan/incubator-nuttx-apps
* branch            master   -> FETCH_HEAD
*       master   -> upstream/master
Already up-to-date.第9步:新建一个分支以便修改代码(按照国际惯例, 我们一般不在 master 上提交新代码, 而需要为新增的功能或者fixbug建立新分支, 再合并到 master 上), 新分支可以随便取名,本次取名fft 命令为:git checkout -b fft   执行结果如下:
Switched to a new branch 'fft'
第10步: 用文本编辑器打开apps中的源文件随心所欲的修改代码;
第11步: 将修改过的代码提交到本地代码库,命令为:git commit -a -m "new commit"
执行结果报错了:
*** Please tell me who you are.

Run

git config --global user.email "you@example.com"
git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'zouboan@hp.(none)')这是由于我们首次使用.git ,为配置用户名和邮箱,通过以下两条命令来配置:
git config --global user.email "fft@feedforward.com.cn"
git config --global user.name "zouboan"
再次执行git commit -a -m "new commit"结果为:
git commit -a -m "new commit" new commit
2 files changed, 4 insertions(+), 4 deletions(-)
把本地更新代码提交到自己的github上你fork的代码中新建的 fft分支中,命令为:   git push origin fft   

结果又报错了:
fatal: remote error:
You can't push to git://github.com/zouboan/incubator-nuttx-apps.git
Use https://github.com/zouboan/incubator-nuttx-apps.git
zouboan@hp:~/G/NuttX/apps$ git remote add upstream https://github.com/zouboan/incubator-nuttx-apps.git
fatal: remote upstream already exists.

这是由于刚才我们添加upstream,用的是 git remote add upstream git://github.com/zouboan/incubator-nuttx-apps.git因为这个 protocol 是不支持 push 的
解决办法:
    git remote rm origin
    git remote add origin git@github.com:zouboan/incubator-nuttx-apps.git
    git push origin或者打开apps/.git中的config文件将

url = git://github.com/zouboan/incubator-nuttx-apps.git
      fetch = +refs/heads/*:refs/remotes/origin/*改为:

      url = git@github.com:zouboan/incubator-nuttx-apps.git
      fetch = +refs/heads/*:refs/remotes/origin/*再次执行git push origin fft结果又报错了:
Warning: Permanently added the RSA host key for IP address '20.205.243.166' to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights这个应该是很多github新手经常出错的问题,这个就是没有在你github上添加一个公钥,可以用 ssh -T git@github.com去测试一下:
zouboan@hp:~$ ssh -T git@github.com
Permission denied (publickey).果然没有publickey!执行命令:   ssh-keygen -t rsa -C "zouboan"    来生成公钥,一路回车
zouboan@hp:~$ ssh-keygen -t rsa -C "zouboan"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/zouboan/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/zouboan/.ssh/id_rsa.
Your public key has been saved in /home/zouboan/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:aColFvMUXAoXDISRHs8a4k26GjdE5IPwYH3vXxW/Nwg zouboan
The key's randomart image is:
+-------+
|+**++o.          |
|=B +o+       .   |
|o @ + .       o|
|.+ X   o   E . . |
|o X o + S   o . .|
| * + o .   . . o.|
|. = .   . .   o|
| + o   .       |
|o                |
+---------+
cat 一下把出现的key 复制下来:zouboan@hp:~$ cat /home/zouboan/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDDoo9PUluSp18dJtiR2Sfy/2b3p+nsMQYbynFp9Q608ASBe+vZmohiTCLGiTaeZ1CuNLvWaL5seXLVMGDb4tAmCszF/CfqYqAGjPP7ZX6QOSQObsvljxuk3my5DrfH0wVaEcBpeXQFi6SMRovSA1JlWQM+iJzce8HPoof2BoeMY01lnQchngR90lYtZpeDIE+qSJgAtySi6rWG/D+xd4Gb+aPYpEdk71I6kzqHAJ8yVA8mARh2n2s6/WXgJc1YALUb9GUWGdCp4nq/QFe8NlDrzMK/vvRAUwY6K+DuuP3rcJwMJ5T9hTIHWlUxWJ4sTBZ zouboan在github网页你账号上添加刚刚生成的公钥:
Setting ---> SSH and GPG keys ---> New SSH keys
粘贴确认即可
再次再次执行git push origin fft   这次成功了:

zouboan@hp:~/G/NuttX/apps$ git push origin fft
Counting objects: 8, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (8/8), 1.21 KiB | 0 bytes/s, done.
Total 8 (delta 7), reused 0 (delta 0)
remote: Resolving deltas: 100% (7/7), completed with 6 local objects.
remote:
remote: Create a pull request for 'fft' on GitHub by visiting:
remote:      https://github.com/zouboan/incubator-nuttx-apps/pull/new/fft
remote:
To git@github.com:zouboan/incubator-nuttx-apps.git
*       fft -> fft

注意:


[*]新加的文件(即没有被git系统管理的文件)是不能被提交到本地仓库的,
[*]原来没有,本地新增的文件要先执行   git add .   再执行git commit -a -m "new commit"最后再执行git push origin fft才行
[*]一个常见的问题是远程的 upstream (swoole/swoole-src) 有了新的更新, 从而会导致我们提交的 Pull Request 时会导致冲突, 因此我们可以在提交前先把远程其他开发者的commit和我们的commit合并.
[*]使用以下代码切换到 master 分支:
git checkout master
[*]使用以下代码拉出远程的最新代码:
git pull upstream master
[*]切换回 fft:
git checkout fft> 如果忘记自己之前建的分支名可以用 `git branch` 查看,通过git branch查看当前在哪个分支
[*]把 master 的 commit 合并到 fft:

git rebase master



第12步:提交 Pull Request你可以在你的 github 代码仓库页面切换到 branches 页面点击 branch1 分支后点击 New pull request 按钮, 添加相关注释后提交.OR    切换到 branch1 分支的代码仓库点击 Compare & pull request 按钮, 添加相关注释后提交.






页: [1]
查看完整版本: 如何在github上贡献代码