Push rule settings

更新时间:
复制 MD 格式

To standardize developer commit formats, Codeup provides a check service that validates commits using custom push rules. This topic describes how to create a push rule and explains what happens when it is triggered.

Create a push rule

  1. As a repository administrator, click Settings in the lower-left corner and navigate to Push Rule Settings.

  2. Click Create Push Rule and configure the rule parameters.

    In the Create Push Rule dialog box, configure the following parameters: Commit Message (a regular expression that the commit message must match, for example, ^fix #[A-Z]+\-[0-9]+), Committer Email (a regular expression that the committer's email must match, for example, @example\.com$), and Restricted Files (prevents pushes of files whose names match the regular expression; multiple rules can be added, separated by newlines). You can also select the Prevent force push and Code owner check options. The check rule can be applied to the author or the committer. The restriction level can be set to Block Push or Warn but Allow Push. When you are finished, click OK.

    Important

    Push rules apply to all branches in a repository.

  • Commit Message: A push is allowed only if the commit message matches the specified regular expression. If this field is empty, all commit messages are allowed.How to write regular expressions.

  • Committer Email: The committer's email address, configured with git config, must match the specified regular expression. If this field is empty, all email addresses are allowed.How to write regular expressions.

  • Prevent force push: A force push overwrites remote code and can cause permanent loss of code and commit history. Use this command with caution. If you suspect that a force push has caused commit history loss, you can filter for force push events in the repository's Activity feed to trace the operator and view force push records.

    When enabled, this option blocks git push -f if your local branch has diverged from the remote, preventing the server's code from being overwritten.

    Note

    If Prevent force push is enabled but your local branch has not diverged from the remote branch, using git push -f has the same effect as git push, and the push will succeed.

  • Code owner check: The Code owner check feature relies on your configured Git user information.

    git config --global user.name "Your Name"
    git config --global user.email "your.email@example.com"

Author and committer

  • Git allows you to rewrite history or commit on behalf of others. To ensure accurate contribution statistics, the commit author should match the logged-in user.

  • The committer is not typically displayed. It is only shown when you use a specific git log --format command (for example, format=fuller). The committer is the person who finalizes the commit, based on the git config user.name and git config user.email settings at commit time.

  • When using the git commit command, you can use the author parameter to specify the contributor. In open-source projects, it is a common practice to credit someone as an author even if you only adopted their idea without using their code directly. For an initial commit, the author and committer are usually the same. However, when you rewrite commit history with commands like git cherry-pick or git rebase, the author remains the same, but the committer becomes the person performing the operation. In short, the author is the original contributor, and the committer is the person who applies the commit.

  • Codeup can check the author and committer email addresses against the logged-in user's verified primary email. This ensures accurate ownership attribution and prevents inaccurate contribution statistics caused by user mismatches.

    In the upper-right corner of Alibaba Cloud DevOps, go to Personal Settings. From the left navigation pane, select Personal Information. In the Email section, you can view and manage your linked email addresses. Codeup uses the address marked as the primary email to match the author or committer. To add a new email address, click Bind Email and complete the verification process.

Inherit organization-level rules

Rules inherited from your organization are read-only and cannot be edited or deleted at the repository level. To modify these rules, contact your organization administrator; see the organization-level repository management settings.

Push-time checks

Consider a rule with the following configuration: Check Rule: Check Author; Restriction Level: Warn but Allow Push.

For this push rule, Commit Message is set to feature-*, Committer Email is restricted to @alibaba.com, Restricted Files is empty, and Prevent force push is enabled.

UI operations

If a commit message does not match the rule, the push is rejected, and the error Push rule check failed appears. The message specifies that the commit message does not match the specified regular expression (for example, feature-*) and displays the rejected commit ID.

Similarly, the push is rejected if the author's email does not match the rule.

A red error message, Push rule check failed, appears at the top of the page. It indicates that the author's email does not match the specified regular expression (for example, @alibaba.com) and displays the rejected commit ID.

The same logic applies to merge requests.

CLI operations

If the commit message does not match the rule, the push fails with the following error:

misha-MacBookPro:Codeup-Demo-123 teambition$ git push
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 235 bytes | 235.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: master: Push rule check failed! Commit message does not match the regular expression: feature-*.
remote: For instructions on how to fix this, see: http://t.tb.cn/30VwoIifScyCkxCeltF4UP
remote: To modify the check rule, contact the repository administrator.
To codeup.teambition.com:codeup/Codeup-Demo-123.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'git@codeup.teambition.com:codeup/Codeup-Demo-123.git'
misha-MacBookPro:Codeup-Demo-123 teambition$

If the author's email does not match the rule, the push fails with the following error:

misha-MacBookPro:Codeup-Demo-123 teambition$ git push
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 238 bytes | 238.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: master: Push rule check failed! The author's email xxx@alibaba-inc.com does not match the regular expression: @alibaba.com.
remote: For instructions on how to fix this, see: http://t.tb.cn/30VwoIifScyCkxCeltF4UP
remote: To modify the check rule, contact the repository administrator.
To codeup.teambition.com:codeup/Codeup-Demo-123.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'git@codeup.teambition.com:codeup/Codeup-Demo-123.git'
misha-MacBookPro:Codeup-Demo-123 teambition$

A failed code owner check returns the following error:

remote: master: Push rule check failed! The commit author email (xxx@alibaba-inc.com) does not match the push user email (xxx@alibaba-inc.com).
remote: For instructions on how to fix this, see: xxx
remote: To modify the check rule, contact the repository administrator.

Modify previous commit messages

For more information, see How to modify commit history?