![]() ![]() Some example regular expressions you can use in push rules: Which already limits users from pushing directly. The default branch also defaults to being a protected branch, If you have other target branches, include them in your regex. GitLab is configured this wayīecause merges typically have the default branch as their target. Note that the name of your default branch is always allowed, regardless of the branch naming By restricting theīranch names globally in Push Rules, such mistakes are prevented.Īny branch name that doesn't match your push rule is rejected. Various branches, and CI pipelines might not work as expected. Your developers may not remember that policy, so they might push to GitLab CI/CD jobs (such as feature, hotfix, docker, android) that rely on the If your company has a strict policy for branch names, you may want the branches to start Only pushing commits with messages like Bugfix according to JIRA-123 Now when a user tries to push a commit with a message Bugfix, their push isĭeclined. Of a Jira issue in the commit message, like JIRA\-\d . Write a regular expression that requires the mention users should not be able to remove Git tags with git push.every commit should reference a Jira issue, for example: Refactored css.Let's assume you have the following requirements for your workflow: Commit messages with a specific reference Per project, so you can have different rules applied to differentĮvery push rule could have its own use case, but let's consider some examples.Some common scenarios: preventing Git tag removal, orĮnforcing a special format for commit messages.Ĭan enable in a user-friendly interface. GitLab already offers protected branches, but there areĬases when you need some specific rules. ![]() ![]() Regular expressions to reject pushes based on commit contents, branch names or file details. xargs takes input from standard input (basically, a string of text - or a list of git branches in this case), and splits the string where there are spaces into separate arguments that can be used in a command.įor each of the branches returned by egrep above, the script will run git branch -d, which will delete the branch on your local machine.Īnd here's the script in action in the terminal.Gain additional control over what can and can't be pushed to your repository by using Read more about egrep.įinally, we pipe the result of the egrep search to the xargs command (short for extended arguments). The | (or pipe) at the beginning of the line means we're "piping" - or sending - the output of the previous command ( git branch -merged) into egrep to perform the regular expression search. So with the -v flag, we're asking egrep to find every branch that isn't named "main". The regular expression we're passing in - "(^\*|main)" - matches the characters "main" from the start of the line. The -v flag instructs egrep to print lines that do not match a given regular expression. We use egrep (or extended grep) to search using extended regular expressions. We're not specifying a commit, so the command will use the HEAD - the current local state of the main branch. The official git documentation writes this as "list branches whose tips are reachable from the specified commit ( HEAD if not specified)". This command checks out the main branch, and lists all branches that can be detected as being merged into the main branch. In short, the code above checks out the main branch, lists all merged branches, searches for and lists branches that are not named exactly "main", and uses git to delete those branches on your local machine. Enter fullscreen mode Exit fullscreen mode ![]()
0 Comments
Leave a Reply. |