What's Actually Happening

Git push is rejected by the remote repository. Local branch is behind remote, push rules are violated, or force push is restricted.

The Error You'll See

Non-fast-forward error:

```bash git push

To github.com:org/repo.git ! [rejected] main -> main (non-fast-forward) error: failed to push some refs to 'github.com:org/repo.git' ```

Protected branch error:

bash
remote: error: GH013: Repository rule violations found for refs/heads/main.
remote: Reviewing push: required status checks not satisfied.

Force push rejected:

bash
remote: error: refusing to update checked out branch: refs/heads/main

Why This Happens

  1. 1.Behind remote - Local commits behind remote commits
  2. 2.Diverged history - Local and remote have different commits
  3. 3.Protected branch - Branch has protection rules
  4. 4.Required checks - CI/CD checks not passed
  5. 5.Force push blocked - Force push not allowed
  6. 6.Permission denied - No push permission
  7. 7.Hook rejection - Pre-receive hook rejected

Step 1: Check Remote Status

```bash git fetch --all

git status

git log --oneline origin/main..HEAD

git log --oneline HEAD..origin/main

git log --graph --oneline --all ```

Step 2: Pull and Merge

```bash git pull origin main

git pull --rebase origin main

# If conflicts: git status

# Resolve conflicts: # Edit conflicted files git add . git rebase --continue

# Or merge: git commit -m "Merge remote changes" ```

Step 3: Check Branch Protection

```bash # On GitHub: gh api repos/OWNER/REPO/branches/main/protection

gh api repos/OWNER/REPO/branches/main/protection/required_status_checks

# On GitLab: glab api projects/PROJECT_ID/protected_branches/main

# Check if direct push allowed: gh api repos/OWNER/REPO/branches/main/protection --jq '.allow_force_pushes' ```

Step 4: Handle Protected Branch

```bash # Create feature branch: git checkout -b feature/my-change

git push origin feature/my-change

# Create pull request: gh pr create --title "My change" --body "Description"

# Or request temporary access: # Contact repository admin ```

Step 5: Check CI/CD Status

```bash gh pr checks

gh run list --branch main

gh run view RUN_ID

# Wait for checks to pass: gh run watch RUN_ID ```

Step 6: Handle Force Push

```bash # Force push (with caution): git push --force-with-lease origin main

git push --force origin main

# If force push blocked: # Use feature branch and PR instead ```

Step 7: Check Push Permissions

```bash gh auth status

git remote -v

# Check if you have write access: gh api repos/OWNER/REPO --jq '.permissions.push'

# Fork workflow: gh repo fork OWNER/REPO git remote add fork https://github.com/YOUR/REPO.git git push fork main ```

Step 8: Check Hooks

```bash # Check for pre-receive hooks: # On server side, hooks may reject push

# Check local hooks: ls -la .git/hooks/

# Check commit message format: git log --oneline -1

# Check signed commits: git log --show-signature -1 ```

Step 9: Resolve Conflicts

```bash git status

# List conflicting files: git diff --name-only --diff-filter=U

# View conflict: cat conflicted-file.txt

# Accept ours/theirs: git checkout --ours conflicted-file.txt git checkout --theirs conflicted-file.txt

# After resolving: git add . git commit ```

Step 10: Verify Push

```bash git push origin main

git log --oneline origin/main -5

gh repo view --web

# Check CI status: gh run list --limit 5 ```

Git Push Rejected Checklist

CheckCommandExpected
Remote statusgit fetchUp to date
Behind remotegit logNo commits behind
Branch protectiongh apiRules satisfied
Permissionsgh authWrite access
CI/CD checksgh runAll passed
Conflictsgit statusNo conflicts

Verify the Fix

```bash git push origin main

git log --oneline origin/main -3

git status

gh pr list

gh run list --limit 3 ```

  • [Fix Git Merge Conflict](/articles/fix-git-merge-conflict)
  • [Fix Git Authentication Failed](/articles/fix-git-authentication-failed)
  • [Fix Git Branch Not Found](/articles/fix-git-branch-not-found)