How to fix remote: GitLab: Push was rejected because it contains secrets in Git

GitINTERMEDIATEHIGHUpdated Dec 6, 2025· 236 views

This error occurs when GitLab's Secret Push Protection detects potential secrets (API keys, tokens, passwords, or private keys) in your commits. You must either remove the secrets from your commits, rotate the exposed credentials, or explicitly skip the check for false positives.

Quick fix

Fixes remote: GitLab: Push was rejected because it contains secrets

remote: PUSH BLOCKED: Secrets detected in code changes
remote: -- commit abc123: config/settings.py:15 AWS Access Key
remote: -- commit def456: src/api.js:42 GitLab Personal Access Token
  1. Identify the detected secrets
  2. Determine if the detection is a true or false positive
  3. Remove the secret from your most recent commit
  4. Remove secrets from older commits in your branch
git — error
remote: PUSH BLOCKED: Secrets detected in code changes
remote: -- commit abc123: config/settings.py:15 AWS Access Key
remote: -- commit def456: src/api.js:42 GitLab Personal Access Token
remote: GitLab: Push was rejected because it contains secrets

What this error means

The "Push was rejected because it contains secrets" error is triggered by GitLab's Secret Push Protection feature, a security mechanism that scans commits for sensitive data before allowing them to be pushed to the repository. When you push commits over HTTP(S) or SSH, GitLab analyzes the diffs (changes) in each commit looking for patterns that match known secret formats. If a potential secret is detected, the push is blocked to prevent accidental exposure of credentials. **What GitLab detects:** - **API keys and tokens** - GitLab Personal Access Tokens, AWS Access Keys, Google API Keys, etc. - **Database connection strings** - URLs containing embedded usernames and passwords - **Private keys** - RSA, DSA, EC private keys used for authentication - **High-entropy strings** - Random-looking strings that match secret patterns This is a security feature, not a bug. GitLab is protecting your repository from credential leaks, which are a leading cause of security breaches. Once secrets are pushed to a repository (even a private one), they can be exposed through forks, CI/CD logs, or compromised accounts. **Important:** Secret Push Protection only scans the diffs in your commits, not entire files. If a secret was already present in a file and your commit doesn't modify the line containing it, the secret won't be detected by this feature.

Typical symptoms

  • Git push fails with 'remote: PUSH BLOCKED: Secrets detected in code changes'
  • The error message includes specific commit IDs, file paths, and line numbers
  • The error identifies the type of secret detected (e.g., 'GitLab Personal Access Token')
  • Push works fine to other repositories but fails on GitLab
  • The push was previously working but started failing after adding new code or config files
  • CI/CD pipelines fail at the push step with secret detection errors

Common causes

  1. Accidentally committed a real API key, token, or password to the repository
  2. Hardcoded credentials in configuration files (database URLs, API endpoints with keys)
  3. Committed a private key file (.pem, .key, id_rsa) or its contents
  4. Environment variables or .env file contents were accidentally included
  5. Test files contain realistic-looking mock credentials that trigger false positives
  6. Copy-pasted code from documentation that includes example API keys
  7. Auto-generated code or config that contains placeholder secrets matching real patterns
  8. Third-party library or SDK files containing example credentials

How to fix "remote: GitLab: Push was rejected because it contains secrets"

1Identify the detected secrets

When your push is blocked, GitLab provides detailed information about what was detected. Look at the error message carefully:

bash
remote: PUSH BLOCKED: Secrets detected in code changes
remote: -- commit abc123: config/settings.py:15 AWS Access Key
remote: -- commit def456: src/api.js:42 GitLab Personal Access Token

The message tells you:
- Commit ID - Which commit contains the secret
- File path and line number - Exact location of the detected pattern
- Secret type - What kind of secret was identified

Note down each detection - you'll need to address them all before you can push successfully.

2Determine if the detection is a true or false positive

Before taking action, evaluate whether the detected pattern is an actual secret:

True positive indicators:
- The string is a real credential you use in production or development
- It matches the format of actual service API keys (e.g., AWS keys starting with "AKIA")
- It's a connection string with real database credentials
- It's a private key file or PEM-encoded content

False positive indicators:
- It's example/placeholder text from documentation
- It's a test fixture with fake credentials that happen to match patterns
- It's a high-entropy string that isn't actually a secret (UUID, hash, etc.)
- It's in a file that's meant to be committed (like a public key)

If it's a true positive: Continue to step 3 to remove the secret.
If it's a false positive: Skip to step 5 to bypass the check.

3Remove the secret from your most recent commit

If the secret is in your most recent (unpushed) commit, you can amend it:

Step 1: Remove or replace the secret in the file

bash
# Edit the file to remove the hardcoded secret
nano config/settings.py

# Replace the secret with an environment variable reference
# Before: API_KEY = "sk-abc123..."
# After:  API_KEY = os.environ.get("API_KEY")

Step 2: Stage the changes and amend the commit

bash
git add config/settings.py
git commit --amend --no-edit

Step 3: Try pushing again

bash
git push origin main

Important: If the secret was ever committed (even if you amend), consider it compromised. Rotate the credential immediately - see step 6.

4Remove secrets from older commits in your branch

If the secret is in a commit further back in your unpushed history, you need to rewrite history.

Option A: Interactive rebase (for a few commits)

bash
# Find how many commits back the secret is
git log --oneline

# Start interactive rebase (e.g., last 5 commits)
git rebase -i HEAD~5

In the editor, change pick to edit for the commit containing the secret. Save and close. Then:

bash
# Edit the file to remove the secret
nano config/settings.py

# Stage and amend
git add config/settings.py
git commit --amend --no-edit

# Continue the rebase
git rebase --continue

Option B: Use git-filter-repo (for multiple occurrences)

bash
# Install git-filter-repo
pip install git-filter-repo

# Replace a specific secret string throughout history
git filter-repo --replace-text <(echo 'sk-abc123secretkey==>REMOVED')

# Or remove a file entirely from history
git filter-repo --path secrets.env --invert-paths

Option C: Use BFG Repo-Cleaner (faster for large repos)

bash
# Download BFG (requires Java)
# Create a file listing secrets to remove
echo "sk-abc123secretkey" >> secrets.txt
echo "AKIA12345EXAMPLE" >> secrets.txt

# Run BFG
java -jar bfg.jar --replace-text secrets.txt my-repo.git

# Clean up
cd my-repo.git
git reflog expire --expire=now --all
git gc --prune=now --aggressive

After rewriting history, you'll need to force push (with caution):

bash
git push --force-with-lease origin main
5Skip secret detection for false positives

If GitLab detected a false positive (not an actual secret), you can skip the check.

Method 1: Push option flag

bash
git push -o secret_push_protection.skip_all origin main

Method 2: Commit message directive
Add [skip secret push protection] to a commit message:

bash
# Amend the last commit to add the skip directive
git commit --amend -m "Add configuration file [skip secret push protection]"

# Or create a new commit with the directive
git commit --allow-empty -m "Enable push [skip secret push protection]"

git push origin main

Important considerations:
- Only use this for confirmed false positives
- The skip applies to all secrets in the push, so verify there are no real secrets mixed in
- Consider reporting persistent false positives to your GitLab administrator
- Your organization may have policies against skipping secret detection

6Rotate compromised credentials immediately

Critical: If a real secret was committed, treat it as compromised even if you removed it before pushing successfully. The secret may exist in:
- Your local reflog
- Other developers' local copies
- Backup systems
- Shell history

Rotation checklist by secret type:

API Keys/Tokens:

bash
# Generate a new key in the service's dashboard
# GitLab: Settings > Access Tokens > Revoke old, create new
# AWS: IAM > Security credentials > Create new access key
# Then update your application/environment

Database credentials:

sql
-- Change the password
ALTER USER myuser PASSWORD 'new-secure-password';

Private keys:

bash
# Generate a new key pair
ssh-keygen -t ed25519 -C "your_email@example.com"

# Add the new public key to services
# Remove the old public key from all authorized_keys files

After rotation:
1. Update all systems using the old credentials
2. Verify the old credentials no longer work
3. Monitor for unauthorized access using audit logs

7Prevent future secret commits

Set up preventive measures to avoid this issue in the future:

1. Use environment variables

bash
# .env file (add to .gitignore!)
DATABASE_URL=postgres://user:pass@host/db
API_KEY=sk-abc123...

# In your code
import os
api_key = os.environ.get("API_KEY")

2. Add sensitive patterns to .gitignore

gitignore
# .gitignore
.env
.env.local
*.pem
*.key
id_rsa
secrets.json
credentials.json

3. Install pre-commit hooks

bash
# Using git-secrets (AWS)
brew install git-secrets
git secrets --install
git secrets --register-aws

# Using detect-secrets
pip install detect-secrets
detect-secrets scan > .secrets.baseline

4. Use a secrets manager
Instead of environment variables, consider:
- HashiCorp Vault
- AWS Secrets Manager
- Azure Key Vault
- GitLab CI/CD Variables (for pipelines)

5. Enable GitLab pipeline secret detection
Add to your .gitlab-ci.yml:

yaml
include:
  - template: Security/Secret-Detection.gitlab-ci.yml

This scans your entire repository periodically, catching secrets that bypass push protection.

Advanced notes

How GitLab Secret Push Protection Works:

Secret Push Protection (introduced in GitLab 15.x, GA in 17.11) uses pattern matching to detect secrets. It scans only the diffs of pushed commits, not entire files, which means:

1. Secrets already in the repository aren't detected (use pipeline secret detection for full scans)
2. Only changes pushed via HTTP(S) or SSH are scanned
3. Web IDE commits may bypass push protection (check your version)

Detection Patterns:

GitLab uses high-confidence patterns to minimize false positives. Common patterns include:

| Secret Type | Pattern Example |
|-------------|-----------------|
| GitLab PAT | glpat- prefix |
| AWS Access Key | AKIA prefix, 20 chars |
| GitHub Token | ghp_, gho_, ghs_ prefixes |
| Private Keys | -----BEGIN RSA PRIVATE KEY----- |

Exclusions and Custom Rules:

GitLab administrators can configure:
- Path exclusions (e.g., test/ directories)
- Custom patterns to detect organization-specific secrets
- Allowlists for known false positives

Check with your GitLab admin if you're experiencing persistent false positives.

Enterprise Considerations:

In enterprise environments:
- Secret push protection may be enforced at the group or instance level
- Skipping may require special permissions or be disabled entirely
- Audit logs track when secrets are detected and when protection is skipped
- Consider integrating with SIEM for security monitoring

When Secrets Were Already Pushed:

If secrets were pushed before protection was enabled:
1. Use git log -p --all -S 'secret_string' to find all commits containing the secret
2. Rewrite history using git-filter-repo or BFG
3. Force push the cleaned history
4. Notify all team members to re-clone or rebase
5. Clear CI/CD caches that may contain the old commits
6. Request GitLab support to clear server-side caches if using GitLab.com

Handling Merge Requests:

If your MR is blocked by secret detection:
1. Fix the secret in your feature branch
2. Force push the corrected branch
3. The MR will update automatically
4. CI/CD will re-run on the new commits

CI/CD Pipeline Failures:

If your pipeline fails at the push step:

yaml
deploy:
  script:
    - git push -o secret_push_protection.skip_all https://token@gitlab.com/repo.git

Only use this if the "secrets" are false positives (like generated hashes in build artifacts).

Frequently asked questions

What does "remote: GitLab: Push was rejected because it contains secrets" mean?
The "Push was rejected because it contains secrets" error is triggered by GitLab's Secret Push Protection feature, a security mechanism that scans commits for sensitive data before allowing them to be pushed to the repository. When you push commits over HTTP(S) or SSH, GitLab analyzes the diffs (changes) in each commit looking for patterns that match known secret formats. If a potential secret is detected, the push is blocked to prevent accidental exposure of credentials.
How do I fix "remote: GitLab: Push was rejected because it contains secrets"?
This error occurs when GitLab's Secret Push Protection detects potential secrets (API keys, tokens, passwords, or private keys) in your commits. You must either remove the secrets from your commits, rotate the exposed credentials, or explicitly skip the check for false positives.
What causes "remote: GitLab: Push was rejected because it contains secrets"?
Accidentally committed a real API key, token, or password to the repository Hardcoded credentials in configuration files (database URLs, API endpoints with keys) Committed a private key file (.pem, .key, id_rsa) or its contents
Is the "remote: GitLab: Push was rejected because it contains secrets" error serious?
Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.

Official resources & further reading