Git repository guidelines for the Robomaster Society

This note is synced from my github blog. Published on 26 May 2024

This is something I wrote for the embedded team back when I was in the squad, based on practical experience, to standardize GIT repositories. Although it ended up not being used after completion, I hope to make it public for learning and exchange. Although its purpose was to be a tutorial, it can also serve as a tutorial or tool manual for beginners.


(Quick overview)

Repository style:

.
├───.git      # git repository
├───.idea     # CLion configuration
├───include   # header files
├───lib       # third-party libraries
├───src       # code
└───test      # tests
  1. When you have finished developing a portion, the code compiles normally, and it does not affect others' code, then commit to the remote repository.
  2. Don't forget to test the committed code on the machine.

For example, Group A needs to develop UI functionality

Suppose at this point the library files needed for the UI have been added to the project, you can commit:

git add -A
git commit -m "func: add ui lib"

Next, Group A uses the library to implement the vehicle status display feature:

git add -A
git commit -m "func: up date car state"

After the feature is added and committed, Group A confirms the code compiles successfully.

At this point the code should be committed to the remote branch:

git pull                        # sync code
git push                        # upload

One final note: commit messages are best written in English (just Strong makes us look a bit more international).


Design philosophy

The repository is designed so that when developers follow the conventions, they will come into contact with other people's (or groups') code as little as possible.

Therefore, in the design, during development you can just pull anything from dev as a "base". This manifests as: the dev branch should be compilable (that is, even if there are bugs, there is a chance they won't affect other feature development), nodes with version numbers should be usable (that is, they have passed testing, but there is no guarantee there are no issues in actual use for now), and nodes without version numbers are stable (that is, they can be trusted).

Second, I want the entire commit tree to be clear, so the development trajectory can be seen, that is, in the dev tree you will see many branches, and see which version a certain feature was developed based on and which versions it spanned (this is also the reason for using merge --no-ff).

Branch conventions and development workflow

The remote repository branches are main and dev. Each developer should create a local feature branch as their "main branch" during development.

Please do not commit any local branch such as `feature` to the remote repository. If you need to sync code, please create a new repository.

Development workflow

The regular workflow consists of the following five steps:

  1. Create a local branch named feature as the main branch for local development. During development, please do not operate on the dev branch, and even less should you perform any operations on main.
  2. When you have finished developing a portion, the code compiles normally, and it does not affect others' code, you can merge the feature branch into the dev branch. When merging, you need to use the git merge --no-ff operation.

    Please try to merge code that has passed testing or that you are very confident about, and do not merge too much or too little. For example, if you finish writing a function that will be called immediately, commit it; but if you write a utility set that may not be used, then wait until it is mostly done before committing.

  3. After merging, please tag the commit you just made and push it to the remote repository.

    Pushing commits and pushing tags are git push and git push --tags respectively, and require two operations.

    It is strongly recommended that after committing you immediately switch back to the feature branch and merge the dev code, to avoid omissions

  4. Test the code committed to the dev branch as soon as possible.
  5. After confirming the feature development is complete, inform the repository administrator, who will commit it to the main branch after code review.

    When merging from dev to main, the --no-ff parameter should be used.

During development, if you need to update your local in-progress code from dev, after updating your local dev, update your local feature branch via git merge. Note: do not use rebase.

First-time development

When starting development for the first time, you need to download the code stored on the server to your local machine:

git clone {repository address}

Next, choose your initial version for development, that is, which commit to pull onto the feature branch you are about to create. Consider what your task needs to depend on: if possible, it is recommended to base it on the latest commit of main. You can find the commit with the following commands.

git switch main
git describe --tags

If the main branch is too old, then the next-best choice is the latest tagged commit on the dev branch.

git switch dev
git describe --tags

If the tagged commit cannot satisfy you, then you may need the latest commit of the dev branch; just switch to the dev branch.

Next, create the feature branch as your main branch for local development:

git switch --detach {the tag or branch you are aiming at}
git switch -c feature

Then just follow the development workflow.

Handling unexpected situations

  • Version out of sync when committing

    When committing to dev, version out of sync may occur. In this case, run git pull to update the local dev code. Normally it can merge automatically without conflicts. Note that at this point a new commit may be generated, and you should tag the newly generated commit.

  • Need to sync code with other team members

    First, choose a code hosting platform and create a private repository.

    Run the following command to link the local repository to your remote repository:

    git remote add {repository link name} {repository address}
    

    Note that the repository link name should avoid existing names. Running git remote -v locally lets you view already linked remote repositories.

    When committing, use git push {repository link name} to specify the repository to commit to.

  • Updating code needs to skip a certain commit

    When updating development code locally from dev, if you need to skip a certain commit, then you need to use cherry-pick to update the local feature:

    First sync the remote dev:

    git switch develop
    git pull
    

    At this point you can use git log to view and record the nodes we want to operate on. Suppose the commit tree at this point is as follows:

sequenceDiagram
	note over develop : c1 (tag: v1.12.3-alpha)
	develop ->> feature : pull
	note over feature : c1 (tag: v1.12.3-alpha)
	note over develop: c3 (tag: v1.13.0-alpha)
	feature ->> feature : commit
	note over feature : c4
	note over develop: c5 (tag: v1.14.0-alpha)
	note over develop: c6 (tag: v1.14.1-alpha)
	develop --> feature : want to skip c3 and update local code
  • We want to skip c3 and only update the code changed by the two commits c5 and c6:

    git switch feature
    git cherry-pick c3..c6
    

    In the code, c3..c6 means: "the path from c3 up to c6, and not including c3", that is, (c3, c6].

    At this point two new commits identical to c5 and c6 are added on feature.

    The current commit tree is as follows:

sequenceDiagram
	note over develop: c5 (tag: v1.14.0-alpha)
	note over develop: c6 (tag: v1.14.1-alpha)
	develop --> feature : want to skip c3 and update local code
	develop ->> feature : git cherry-pick c3..c6
	note over feature : c5'
	note over feature : c6'
	note right of feature: <- HEAD

Local development recommendations

  • During local development, try not to use tags. If you do use them, never commit your own tags to the public repository.
  • When merging local branches, please try to use rebase instead of merge, or add --ff-only to the merge operation. This will optimize everyone's git experience.
  • Before starting development, use the lock feature of your IDE (in CLion it is in the bottom right corner) to lock all files that are not within your development scope, to avoid accidentally modifying other groups' code.

Tag conventions

A tag consists of two parts: the "version number" and the "suffix", in the form v{version number}-{suffix}.

Tags start with "v" to make it easy to quickly filter tags out from hash values and tags when searching records.

All tags committed by members should carry the alpha suffix. Only after testing and code review are completed should a tag without a suffix be committed.

This rule draws heavily on Semantic Versioning 2.0.0, which is recommended reading.

Version number

Version number format: {major version}.{minor version}.{patch version}.

The version number increment rules are as follows:

  • The major version number can only be determined by code that has passed testing, possibly due to incompatible API changes;
  • The minor version number can only be incremented by each group itself after adding new features, but the version numbers each group may increment are decided by the group meeting;
  • The patch version number is incremented when the commit is a patch to a previous version, decided by the group itself, with no restrictions.

The number of minor version numbers should be decided by the group meeting based on the tasks assigned to each group, and each version number should correspond to a specified feature and be written into the documentation promptly.

Version number example:

Developed a table based on 1.10.3, and when completed the latest dev version is 1.10.5

The new version number to commit is 1.11.0

Later there are minor adjustments, 1.12.3 -> 1.12.4

Suffix

The suffix indicates that stability is not guaranteed. All code that has not undergone repeated testing and code review should carry a suffix. Only group leaders may commit tags without a suffix.

The basic suffix is alpha, and other suffixes are to be determined.

Handling unexpected situations

  • Tag was created incorrectly

    First run git tag -d {tag} to delete the local tag,

    If the incorrect tag has already been uploaded to the repository, run

    git push origin --delete tags {tag}  # origin can be replaced with the code name of the remote repository to modify
    

    to delete the remote tag.

    After deleting, just create the tag again.

  • Conflicts when pushing/pulling

    When the API design is reasonable and the development workflow is standardized, pushing and pulling code will not cause conflicts. Of course, in actual operation conflicts will always occur for various reasons.

    First determine the cause of the conflict based on the error message:

    • When committing code, if the prompt contains hint: (e.g., 'git pull ...') before pushing again., you can try pulling code from the remote repository first, resolving the conflict, and then trying to commit again.
    • When pulling code, if the prompt contains hint: your next pull:, please add the --rebase keyword (or related setting) and pull again.

    When a conflict occurs while merging code using rebase:

    • Use git stash to view the conflicting files;
    • After modifying a conflicting file, run git add {file name} to mark that file's conflict as resolved;
    • After all conflicts are resolved, run git rebase --continue to commit the conflict resolution. If an editing interface appears after running it, the edited text will become the new commit message for the commit that just resolved the conflict.
  • Accidentally committed on main/dev
sequenceDiagram
	note over remote/dev : c1 (tag: v1.12.3-alpha)
	remote/dev ->> local/dev : pull
	note over local/dev : c1 (tag: v1.12.3-alpha)
	remote/dev ->> remote/dev : commit
	note over remote/dev: c3 (tag: v1.13.0-alpha)
	local/dev ->> local/dev : commit
	note over local/dev : c4
	note over remote/dev, local/dev : problem discovered
  • If you have not yet pushed the code to the remote repository, don't worry, this problem is easy to solve:

    First, make sure you have not tagged it yet. If you have, please delete it.

    Second, check the most recent tag on the local dev branch:

    git describe --tags
    

    Remember the output tag.

    Move the committed branch:

    git rebase --onto feature {tag} develop
    

    At this point conflicts are very likely to occur; this is your several commits conflicting.

    In this case, first abandon the rebase, run git reset --hard {tag} on the feature branch, and then rebase again.

    This name will also switch you to the dev branch. Next you need to switch back and update the local feature branch:

    git switch feature
    git reset --hard develop
    

    Finally, just reset dev back to the tag:

    git switch develop
    git reset --hard {tag}
    

    At this point, the problem is solved. Just switch to feature and continue development.

    By the way, if you have already pushed the incorrect commit to the remote repository—just leave it as it is.

git conifg pull.rebase   true
git config core.autocrlf false
git config core.safecrlf true
git config merge.ff      only

(Revision history)

Version numberDatePublisherInformation
V1.02022-8-17\Initial version
V1.12022-8-17\Fixed the issue where some HTML table of contents links could not jump
V1.22023-4-28\Removed private information
V2.02023-5-4\Simplified repository requirements
V3.02024-5-16\Reviewed, removed private information
V3.12024-5-22\Fixed errors, optimized language
V3.22024-5-24\Wrote "Design philosophy" and "First-time development"