前馈科技

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 3492|回复: 0

如何在github上贡献代码

[复制链接]

97

主题

97

帖子

539

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
539
发表于 2022-1-13 00:36:03 | 显示全部楼层 |阅读模式
第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  执行结果如下:
  1. From git://github.com/zouboan/incubator-nuttx-apps
  2. * branch            master     -> FETCH_HEAD
  3. * [new branch]      master     -> upstream/master
  4. Already up-to-date.
复制代码
第9步:  新建一个分支以便修改代码(按照国际惯例, 我们一般不在 master 上提交新代码, 而需要为新增的功能或者fixbug建立新分支, 再合并到 master 上), 新分支可以随便取名,本次取名fft 命令为:git checkout -b fft     执行结果如下:
  1. Switched to a new branch 'fft'
复制代码

第10步: 用文本编辑器打开apps中的源文件随心所欲的修改代码;
第11步: 将修改过的代码提交到本地代码库,命令为:  git commit -a -m "new commit"
执行结果报错了:
  1. *** Please tell me who you are.

  2. Run

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

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

  7. 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"  结果为:
  1. git commit -a -m "new commit"[fft bfd24e3] new commit
  2. 2 files changed, 4 insertions(+), 4 deletions(-)
复制代码

把本地更新代码提交到自己的github上你fork的代码中新建的 fft分支中,命令为:   git push origin fft   

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

这是由于刚才我们添加upstream,用的是 git remote add upstream git://github.com/zouboan/incubator-nuttx-apps.git  因为这个 protocol 是不支持 push 的
解决办法:
  1.     git remote rm origin
  2.     git remote add origin git@github.com:zouboan/incubator-nuttx-apps.git
  3.     git push origin
复制代码
或者打开apps/.git中的config文件将
  1. [remote "origin"]
  2. url = git://github.com/zouboan/incubator-nuttx-apps.git
  3.         fetch = +refs/heads/*:refs/remotes/origin/*
复制代码
改为:
  1. [remote "origin"]
  2.         url = git@github.com:zouboan/incubator-nuttx-apps.git
  3.         fetch = +refs/heads/*:refs/remotes/origin/*
复制代码
再次执行  git push origin fft  结果又报错了:
  1. Warning: Permanently added the RSA host key for IP address '20.205.243.166' to the list of known hosts.
  2. Permission denied (publickey).
  3. fatal: Could not read from remote repository.

  4. Please make sure you have the correct access rights
复制代码
这个应该是很多github新手经常出错的问题,这个就是没有在你github上添加一个公钥,可以用 ssh -T git@github.com去测试一下:
  1. zouboan@hp:~$ ssh -T git@github.com
  2. Permission denied (publickey).
复制代码
果然没有publickey!  执行命令:   ssh-keygen -t rsa -C "zouboan"    来生成公钥,一路回车
  1. zouboan@hp:~$ ssh-keygen -t rsa -C "zouboan"
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/home/zouboan/.ssh/id_rsa):
  4. Enter passphrase (empty for no passphrase):
  5. Enter same passphrase again:
  6. Your identification has been saved in /home/zouboan/.ssh/id_rsa.
  7. Your public key has been saved in /home/zouboan/.ssh/id_rsa.pub.
  8. The key fingerprint is:
  9. SHA256:aColFvMUXAoXDISRHs8a4k26GjdE5IPwYH3vXxW/Nwg zouboan
  10. The key's randomart image is:
  11. +---[RSA 2048]----+
  12. |+**++o.          |
  13. |=B +o+       .   |
  14. |o @ + .       o  |
  15. |.+ X   o   E . . |
  16. |o X o + S   o . .|
  17. | * + o .   . . o.|
  18. |. = .   . .     o|
  19. | + o     .       |
  20. |o                |
  21. +----[SHA256]-----+
复制代码
cat 一下  把出现的key 复制下来:
  1. zouboan@hp:~$ cat /home/zouboan/.ssh/id_rsa.pub
  2. 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   这次成功了:

  1. zouboan@hp:~/G/NuttX/apps$ git push origin fft
  2. Counting objects: 8, done.
  3. Delta compression using up to 4 threads.
  4. Compressing objects: 100% (8/8), done.
  5. Writing objects: 100% (8/8), 1.21 KiB | 0 bytes/s, done.
  6. Total 8 (delta 7), reused 0 (delta 0)
  7. remote: Resolving deltas: 100% (7/7), completed with 6 local objects.
  8. remote:
  9. remote: Create a pull request for 'fft' on GitHub by visiting:
  10. remote:      https://github.com/zouboan/incubator-nuttx-apps/pull/new/fft
  11. remote:
  12. To git@github.com:zouboan/incubator-nuttx-apps.git
  13. * [new branch]      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 按钮, 添加相关注释后提交.






回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|计算机控制

GMT+8, 2024-4-19 21:43 , Processed in 0.049915 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表