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

来源:互联网 发布:网络维修基础知识 编辑:程序博客网 时间:2024/05/21 09:23

本文中的最初作为例子的开源代码库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 可以帮助查看远程仓库服务器相对应的URLorigin 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 -vorigin 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 masterSwitched to branch 'master'Your branch is up-to-date with 'origin/master'.$git merge upstream/masterUpdating e76f8e7..6808a52Fast-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 masterUsername 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
0 0
原创粉丝点击