Learning how to create a branch in GitHub is the first step. However, mastering how to merge branches in GitHub safely is just as critical for every branch GitHub workflow. So, in this post, we’ll walk you through the entire process, starting with how to create a branch in GitHub and then showing you how to merge branches in GitHub safely. We’ll also cover how to cancel a merge if you find any problems, and finally, how Xygeni can help you catch every issue before it gets into your main branch.
Let’s walk through it step by step.
1. How to Create a Branch in GitHub the Right Way
Every secure workflow starts when you create a branch in GitHub. It allows developers to isolate features, fixes, or experiments without impacting the production code.
To create a branch in GitHub:
1. Navigate to your repository
2. Click the branch selector dropdown
3. Type the name of your new branch
4. Click Create branch
From here, your new branch is ready to go. You can now push code, collaborate on changes, and eventually open a pull request. Although this is a basic GitHub action, it sets the stage for secure development.
Importantly, every time you create a branch in GitHub, it should be part of a repeatable and protected workflow.
2. Scan Pull Requests Automatically Before You Merge
When you create a branch in GitHub and prepare for review, the next step is understanding how to merge branches in GitHub securely. Each branch GitHub push should trigger automated checks. But before merging, it is essential to verify that the code does not introduce vulnerabilities. A single unsafe commit can expose your application, leak secrets, or break critical infrastructure.
Merging code into your main branch is a high-impact operation. Without proper checks in place, it can lead to serious consequences such as:
- Zero-day vulnerabilities slipping into production
- Known CVEs introduced through open source packages
- Hardcoded secrets pushed to the repository
- Infrastructure misconfigurations that weaken your defenses
- Malicious or tampered code entering through dependencies
This is where Xygeni makes a real difference
By using GitHub Actions, you can trigger automated Xygeni scans whenever a developer opens a pull request into a protected branch. A protected branch in GitHub is one that requires specific checks or approvals before changes are allowed to merge.
Xygeni analyzes the latest execution of the pull request workflow to validate the security posture of the proposed changes. This includes checking for issues in the code, dependencies, secrets, and CI/CD configurations. The full branch is not re-scanned, but the most recent workflow result is used to enforce policies and block unsafe merges.
These scans validate that the code is secure and production-ready. They detect:
- Code vulnerabilities (SAST)
- Vulnerable open source packages (SCA)
- Hardcoded secrets
- IaC misconfigurations
- Potential malware
Integrating these checks early ensures that every time you create a branch in GitHub and prepare to merge, you do it with full visibility and control.
Here’s a simplified setup:
on:
pull_request:
branches: [ main ]
jobs:
xygeni-scan:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Xygeni Scanner
uses: xygeni/xygeni-action@3.2.0
with:
token: ${{ secrets.XYGENI_TOKEN }}
3. Block Insecure Merges with Guardrails
Guardrails, ensure that when you create a branch in GitHub or attempt how to merge branches in GitHub, only safe changes reach your main branch GitHub setup. Xygeni gives you full control over what gets merged. You can define precise rules tailored to your security policies and risk tolerance. For example:
- Block if a critical secret is found (e.g., AWS keys, tokens)
- Fail the build if a new high-risk open source package is introduced
- Reject pull requests that modify sensitive paths like
.github/workflows/
,infrastructure/
, orsecrets.env
- Prevent merges if a downgrade reintroduces known vulnerabilities
- Block CI/CD configuration changes unless properly labeled
- Stop merges if SAST detects high or critical issues
- Apply stricter Guardrails on production branches while keeping flexibility in development
These rules act as automated gatekeepers. They help your team merge only what’s secure, no surprises, no manual reviews, no last-minute firefighting.
Example Guardrail rule:
guardrail block_critical_secrets
on secrets
when severity = critical
then @fail()
Visual Feedback in the Dashboard
To ensure full visibility, Xygeni shows the result of the latest evaluation of the Guardrail status.
- First of all, a green icon means all policies passed.
- In contrast, a red icon signals that one or more Guardrail conditions were violated.
As a result, both developers and security teams gain immediate insight into why a merge was blocked, without digging into CI logs.
Real Example from the Dashboard:
For example, Let’s say a repository has no protected branches, a common misconfiguration that allows developers to push commits without verification. That’s a serious risk.
Xygeni automatically detects and flags this as a signed_commits issue under the CI/CD category. The dashboard highlights:
- Severity: High
- Type: Signed Commits
- Explanation: The repository has no protected branches
- Status: Open
Therefore, with this context-rich feedback, teams can quickly identify the risk, understand its impact, and take corrective action, all from the Xygeni UI.
Customize Enforcement Behavior
You’re always in control. Choose how strict Guardrails should be:
--fail-on=critical
: Block merges only on severe findings--never-fail
: Run Guardrails in dry-run mode to test policies before enforcing
So next time you create a branch in GitHub, your Guardrails are already there, protecting your pipeline and enforcing your policies automatically.
How Do I Know If a GitHub App Is Safe?
Learn how to evaluate GitHub Apps before installing them.
4. Cancel Merge in GitHub Automatically When Risks Are Found
If risks are detected, the merge is canceled. This safeguard protects every branch GitHub project and enforces best practices on how to merge branches in GitHub.
Xygeni integrates directly into GitHub’s UI. When a risk is found:
- GitHub shows the check as failed
- GitHub’s protections prevent the merge
- The merge queue skips insecure code
Whether it’s a secret, CVE, or dangerous CI/CD pattern, the result is the same: the merge is canceled in GitHub and flagged for review.
You can also view detailed results in Xygeni:
- The security status of each branch
- Why a merge was blocked
- Complete scan history
- Guardrail status shown via green (pass) or red (fail) icons in the project page
This visual feedback makes it easy for developers and security teams to take action with confidence. And when you need deeper context, each issue links to documentation with severity, tags, location, and mitigation guidance.
Tldr Merge Only What’s Safe
To summarize, here’s how to merge securely after you create a branch in GitHub:
- Create a branch in GitHub through the UI
- Trigger automatic scans with every pull request with Xygeni
- Block insecure merges using Guardrails
- Use server-side audit policies for deeper control
- Cancel merge in GitHub when something fails
- Visualize all results in Xygeni’s dashboard
For additional best practices on repository protection, read our GitHub Security FAQs: What Every Developer Should Know.
Although merging is a basic operation, doing it securely requires real visibility and automation. With Xygeni, you don’t just merge code, you merge trust.
Protect Every GitHub Branch with Confidence
A single overlooked issue in a pull request can compromise your main branch. Traditional scanners often run too late, miss critical risks, or fail to enforce meaningful policies.
That is why protecting your GitHub branches takes more than just scanning.
Xygeni provides real enforcement. When a pull request targets a protected branch, Xygeni analyzes the latest execution of your CI/CD workflow. It does not re-scan the entire branch. Instead, it evaluates the most recent results to check for security issues in code, dependencies, secrets, and workflow configurations. You are not just alerted. You are protected.
What makes it different:
- Full-context validation: Guardrails enforce policy using rich context like severity, exploitability, and branch metadata.
- Built-in GitHub integration: Everything from scanning to enforcement runs natively within your GitHub workflows, with no need for custom scripts or glue code.
- Server-side audits: Server-side Guardrails validate the results after upload, adding a second layer of control outside the pipeline.
Instead of relying on your CI setup to catch everything, Xygeni applies automated, policy-based decisions before anything reaches your main branch.
Although merging is a basic operation, doing it securely requires real visibility and automation. With Xygeni, you don’t just protect your repos, you protect every branch GitHub with confidence.