Skip to content

GIT 示例

为 PX4 贡献代码

Adding a feature to PX4 follows a defined workflow. In order to share your contributions on PX4, you can follow this example. 为了在 px4 上分享您的贡献, 您可以遵循此示例。

  • 如果您还没有注册,请先Sign up Github 账户

  • Fork the PX4-Autopilot repo (see here)

  • 将分支克隆到本地计算机

    sh
    cd ~/wherever/
    git clone https://github.com/<your git name>/PX4-Autopilot.git
  • Go into the new directory, initialize and update the submodules, and add the original upstream Firmware

    sh
    cd PX4-Autopilot
    git submodule update --init --recursive
    git remote add upstream https://github.com/PX4/PX4-Autopilot.git
  • You should have now two remote repositories: One repository is called upstream that points to PX4/PX4-Autopilot, and one repository origin that points to your forked copy of the PX4 repository.

  • 这可以通过以下命令进行检查:

    sh
    git remote -v
  • Make the changes that you want to add to the current main.

  • 使用代表您的功能的有意义的名称创建一个新分支

    sh
    git checkout -b <your feature branch name>

    You can verify that the push was successful by going to your forked repository in your browser: https://github.com/<your git name>/Firmware.git
    There you should see the message that a new branch has been pushed to your forked repository.

  • 通过添加相应的文件添加您希望成为提交的一部分的更改

    sh
    git add <file name>

    If you prefer having a GUI to add your files see Gitk or git add -p.

  • 提交添加的文件, 并顺便记录一条有意义的消息, 解释您的更改

    sh
    git commit -m "<your commit message>"

    For a good commit message, please refer to the Source Code Management section.

  • Some time might have passed and the upstream main has changed. PX4 prefers a linear commit history and uses git rebase. To include the newest changes from upstream in your local branch, switch to your main branch

    sh
    git checkout main

    Then pull the newest commits from upstream main

    sh
    git pull upstream main

    Now your local main is up to date. Switch back to your feature branch and rebase on your updated main

    sh
    git checkout <your feature branch name>
    git rebase main
  • 现在, 您可以将本地提交推送到分支版本库

    sh
    git push origin <your feature branch name>
  • You can verify that the push was successful by going to your forked repository in your browser: https://github.com/<your git name>/PX4-Autopilot.git

    There you should see the message that a new branch has been pushed to your forked repository.

  • 现在是时候创建一个拉取请求 (PR) 了。 Now it's time to create a pull request (PR). On the right hand side of the "new branch message" (see one step before), you should see a green button saying "Compare & Create Pull Request". Then it should list your changes and you can (must) add a meaningful title (in case of a one commit PR, it's usually the commit message) and message (explain what you did for what reason. Check other pull requests for comparison) 然后, 它应该列出你的更改,你必须添加一个有意义的标题 (在提交 PR 的情况下, 它通常是提交消息) 和消息 (解释你做了这些更改的原因 , 检查 其他拉取请求 进行比较)。

  • 搞定! You're done! Responsible members of PX4 will now have a look at your contribution and decide if they want to integrate it. Check if they have questions on your changes every once in a while. 每过一段时间,他们会检查你的更改,以确保没有疑义。

Changing Source Trees

We recommend using PX4 make commands to switch between source code branches. This saves you having to remember the commands to update submodules and clean up build artifacts (build files that are not removed will result in "untracked files" errors after the switch).

To switch between branches:

  1. Clean up the current branch, de-initializing submodule and removing all build artifacts:

    sh
    make clean
    make distclean
  2. Switch to a new branch or tag (here we first fetch the fictional branch "PR_test_branch" from the upstream remote):

    sh
    git fetch upstream PR_test_branch
    git checkout PR_test_branch
  3. Get the submodules for the new branch:

    sh
    make submodulesclean

更新子模块

Specific PX4 point releases are made as tags of the release branches, and are named using the format v<release>. These are listed on Github here (or you can query all tags using git tag -l).

To get the source code for a specific older release (tag):

  1. Clone the PX4-Autopilot repo and navigate into PX4-Autopilot directory:

    sh
    git clone https://github.com/PX4/PX4-Autopilot.git
    cd PX4-Autopilot

    INFO

    You can reuse an existing repo rather than cloning a new one. In this case clean the build environment (see changing source trees):

    sh
    make clean
    make distclean

:::

  1. Checkout code for particular tag (e.g. for tag v1.13.0-beta2)

    sh
    git checkout v1.13.0-beta2
  2. Update submodules:

    sh
    make submodulesclean

Get a Release Branch

Releases branches are branched of main, and used to backport necessary changes from main into a release. The branches are named using the format release/<release_number> (e.g. release/v1.13). The are listed here.

To get a release branch:

  • Clone the PX4-Autopilot repo and navigate into PX4-Autopilot directory:

    sh
    git clone https://github.com/PX4/PX4-Autopilot.git
    cd PX4-Autopilot

    INFO

    You can reuse an existing repo rather than cloning a new one. In this case clean the build environment (see changing source trees):

    sh
    make clean
    make distclean

:::

  • Fetch the desired release branch. For example, assuming you want the source for PX4 v1.14:

    sh
    git fetch origin release/1.14
  • Checkout the code for the branch

    sh
    git checkout release/1.14
  • Update submodules:

    sh
    make submodulesclean

更新子模块

有几种方法可以更新子模块。 There are several ways to update a submodule. Either you clone the repository or you go in the submodule directory and follow the same procedure as in Contributing code to PX4.

为子模块更新执行 PR

This is required after you have done a PR for a submodule X repository and the bug-fix / feature-add is in the current main of submodule X. Since the Firmware still points to a commit before your update, a submodule pull request is required such that the submodule used by the Firmware points to the newest commit.

sh
cd Firmware
  • 创建一个分支,描述子模块更新的 bug 修复/功能:

    sh
    git checkout -b pr-some-fix
  • 进入子模块的子目录

    sh
    cd <path to submodule>
  • PX4 submodule might not necessarily point to the newest commit. Therefore, first checkout master and pull the newest upstream code. Therefore, first checkout main and pull the newest upstream code.

    sh
    git checkout main
    git pull upstream main
  • 回到 Firmware 目录,如往常一样添加、提交和上推更改。

    sh
    cd -
    git add <path to submodule>
    git commit -m "Update submodule to include ..."
    git push upstream pr-some-fix

查看拉取请求

You can test someone's pull request (changes are not yet merged) even if the branch to merge only exists on the fork from that person. Do the following 执行以下指令::

sh
git fetch upstream  pull/<PR ID>/head:<branch name>

PR ID is the number right next to the PR's title (without the #) and the &lt;branch name&gt; can also be found right below the PR ID, e.g. &lt;the other persons git name&gt;:&lt;branch name&gt;. After that you can see the newly created branch locally with 之后, 您可以看到新创建的分支在本地

sh
git branch

然后切换到那个分支

sh
git checkout <branch name>

常见错误

强制推送到分叉存储库

做完第一个 PR 后, 来自 PX4 社区的人将回顾你的更改。 在大多数情况下, 这意味着您必须根据评审来修复本地分支。 After changing the files locally, the feature branch needs to be rebased again with the most recent upstream/main. 但是, 在重新建立基础后, 不再可能将特征分支直接推送到分叉存储库, 而是需要使用强制推送:

sh
git push --force-with-lease origin <your feature branch name>

重新建立合并冲突

If a conflict occurs during a git rebase, please refer to this guide.

拉取合并冲突

If a conflict occurs during a git pull, please refer to this guide.

Build error due to git tags out of date

After having done the first PR, people from the PX4 community will review your changes. In most cases this means that you have to fix your local branch according to the review. After changing the files locally, the feature branch needs to be rebased again with the most recent upstream/master. However, after the rebase, it is no longer possible to push the feature branch to your forked repository directly, but instead you need to use a force push:

This can be solved by fetching the upstream repository tags:

sh
git add -p](http://nuclearsquid.com/writings/git-add/). * Commit the added files with a meaningful message explaining your changes