GitHub:怎样在参与开发时同步你的远程代码仓库

2 篇文章 0 订阅

本文中的最初作为例子的开源代码库smartdevicelink是一个车联网真实的开源代码库,并且已经在量产车中有使用,欢迎有能力且有兴趣的开发者参与,也可以联系我。不过发现之前发表文章后有哥们在上面误操作,导致别的开发人员的正常pull request出现问题! 基于担心可能别人也这样操作,所以改为GitHub自身提供的一个例子。

引言:

随着越来越多的互联网企业宣布自己在GitHub上开源项目,越来越多的开发人员可以参与进来开发,贡献自己的一份力量。但是这样由于大家互不谋面可能沟通交流,写代码的方式,思维都会不一样,所以需要一系列约束来遵守,最主要的就是贡献代码时会有很多人同时开发的情况,这样需要经常同步上游仓库到自己本地仓库和自己的远程服务器中。

准备:

1. 创建自己的GitHub账号

https:github.com
https://github.com

2. Fork一个你准备参与开发的代码库

GitHub上专门有一个供大家练习的Repository
这里写图片描述

这样在自己的GitHub仓库中就会有该代码库:
这里写图片描述

3. Clone 到你本地

$git clone https://github.com/<user>/Spoon_Knife.git

4.添加远程仓库

$cd Spoon_Knife
$git remote -v
#git remote 可以查看我们配置了那些远程仓库服务器,并列出简写
#-v 可以帮助查看远程仓库服务器相对应的URL
origin https://github.com/<user>/Spoon_Knife.git(fetch)
origin https://github.com/<user>/Spoon_Knife.git(push)
$git remote add upstream https://octocate/Spoon_Knife
#git remote add <shortname> <url> 
#可以添加一个新的远程Git仓库,同时制定自定义的简写名称
$git remote -v
origin https://github.com/<user>/Spoon_Knife.git(fetch)
origin https://github.com/<user>/Spoon_Knife.git(push)
upstream    https://github.com/smartdevicelink/Spoon_Knife.git(fetch)
upstream    https://github.com/smartdevicelink/Spoon_Knife.git(push)

参与开发

一般的开源代码库,都有master分支和develop分支,master分支比较稳定,用来发布。develop分支一般用来开发。
强烈建议参与开发前每个开发者都仔细看一下代码库中的CONTRIBUTING.md文件,这样会省很多没必要的沟通,也会对该开源项目的要求做一个简单的了解。
如果想添加一个新的需求,则可以基于原来代码的develop分支新建一个feature分支
如果发现了某个问题,并想解决这个问题,则应先提出issue,然后基于develop分支新建一个hotfix分支

$git checkout develop
$git checkout -b newFeature develop
$git checkout -b hotfix_bug# develop

然后在新建的分支上修改代码

同步

由于开源的项目,会有很多人同时参与开发,所以需要经常同步其他人的开发。从远程上游(upstream)仓库同步代码到本地和自己fork来的GitHub仓库中。需要三步:

1. Fetch 上游仓库的新的提交

$git fetch upstream

会看到比较详细的输出信息,从远程仓库拉取其新的分支和各自的提交,保存到本地仓库。
可以通过以下命令查看本地的所有分支:

$git branch -va
* develop                                                  71493a0 Merge pull request #409 from LuxoftSDL/fix/Use_AppName_if_VR_or_TTS_synonyms_Empty_in_QueryApps
  master                                                   e76f8e7 Merge pull request #404 from smartdevicelink/hotfix/fix_pt_sendMessagetoSDK
  release/4.0.0                                            4d6cf97 Merge pull request #220 from LuxoftSDL/hotfix/SystemRequest_crash_sdl4.0
  remotes/origin/HEAD                                      -> origin/master
  remotes/origin/UPS-release                               4d00dc0 Merge branch 'hotfix/error-in-PI' into UPS-release
  remotes/origin/develop                                   71493a0 Merge pull request #409 from LuxoftSDL/fix/Use_AppName_if_VR_or_TTS_synonyms_Empty_in_QueryApps
  remotes/origin/feature/experimental/remote_control       61393c8 HMI Get URls Update
  remotes/origin/hotfix/change-device-info-to-string       d7f3cc3 Changed device_info id to string and mac_address
  remotes/origin/hotfix/cla                                a80368a Added instructions to sign CLA
  remotes/origin/hotfix/get_urls                           6e304f2 Get Urls Fix
  remotes/origin/hotfix/hybrid_service_to_mobile           9429759 Send hybrid service 0x0F if outgoing message has binary data
  remotes/origin/hotfix/local_pt_update                    44da661 Policy Table Update HMI Button
  remotes/origin/master                                    e76f8e7 Merge pull request #404 from smartdevicelink/hotfix/fix_pt_sendMessagetoSDK
  remotes/origin/release/4.0.0                             4d6cf97 Merge pull request #220 from LuxoftSDL/hotfix/SystemRequest_crash_sdl4.0
  remotes/upstream/UPS-release                             4d00dc0 Merge branch 'hotfix/error-in-PI' into UPS-release
  remotes/upstream/develop                                 71493a0 Merge pull request #409 from LuxoftSDL/fix/Use_AppName_if_VR_or_TTS_synonyms_Empty_in_QueryApps
  remotes/upstream/feature/experimental/remote_control     61393c8 HMI Get URls Update
  remotes/upstream/hotfix/bt_device_scanner_leak           36ba4ad Fixes #314     

2. Merge从上游仓库拉取的变更

比如我们要合并master分支

$git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
$git merge upstream/master
Updating e76f8e7..6808a52
Fast-forward
 .travis.yml                                                                          |  15 ++
 README.md                                                                            |   4 +-
 src/components/application_manager/CMakeLists.txt                                    |   2 +
 .../application_manager/include/application_manager/application_manager_impl.h       |   5 +
 .../include/application_manager/commands/hmi/dial_number_request.h                   |  75 +++++++
 .../include/application_manager/commands/hmi/dial_number_response.h                  |  76 +++++++
 .../include/application_manager/commands/mobile/dial_number_request.h                |  27 ++-
 .../include/application_manager/commands/mobile/dial_number_response.h               |  72 ++++++
 src/components/application_manager/include/application_manager/smart_object_keys.h   |   1 +
 src/components/application_manager/src/application_manager_impl.cc                   |  10 +-
 .../application_manager/src/commands/hmi/button_get_capabilities_response.cc         |   8 +
 src/components/application_manager/src/commands/hmi/dial_number_request.cc           |  57 +++++
 src/components/application_manager/src/commands/hmi/dial_number_response.cc          |  57 +++++
 src/components/application_manager/src/commands/mobile/dial_number_request.cc        |  96 +++++++-
 src/components/application_manager/src/commands/mobile/dial_number_response.cc       |  53 +++++
 .../application_manager/src/commands/mobile/register_app_interface_request.cc        |  21 +-
 src/components/application_manager/src/commands/mobile/slider_request.cc             |   3 +-
 src/components/application_manager/src/commands/mobile/system_request.cc             | 489 +++++++++++++++++++++++++++++++++++------
 src/components/application_manager/src/hmi_command_factory.cc                        |  10 +
 src/components/application_manager/src/message_helper.cc                             |   1 -
 src/components/application_manager/src/mobile_command_factory.cc                     |  11 +
 .../connection_handler/include/connection_handler/connection_handler_impl.h          |   5 +-
 src/components/connection_handler/src/connection_handler_impl.cc                     |  21 +-
 src/components/interfaces/HMI_API.xml                                                |  14 ++
 src/components/interfaces/MOBILE_API.xml                                             | 144 ++++++------
 src/components/policy/src/policy/src/cache_manager.cc                                |  10 +
 26 files changed, 1108 insertions(+), 179 deletions(-)
 create mode 100644 .travis.yml
 create mode 100644 src/components/application_manager/include/application_manager/commands/hmi/dial_number_request.h
 create mode 100644 src/components/application_manager/include/application_manager/commands/hmi/dial_number_response.h
 create mode 100644 src/components/application_manager/include/application_manager/commands/mobile/dial_number_response.h
 create mode 100644 src/components/application_manager/src/commands/hmi/dial_number_request.cc
 create mode 100644 src/components/application_manager/src/commands/hmi/dial_number_response.cc
 create mode 100644 src/components/application_manager/src/commands/mobile/dial_number_response.c

如果本地没有独特的提交,git会自动fast-forward.

3. Push 本地更新到自己的远程服务器上

$git push origin master
Username for 'https://github.com': 
Password for 'https://user@github.com': 
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/user/Spoon_Knife.git
   98b34b9..6808a52  master -> master
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值