nikivdev/code · error

git push failed

Error message

git push failed

What it means

push_to_origin runs `git push -u origin <branch>` and, if the push exits non-zero, bails with 'git push failed'. This is the final step of GitHub publishing; the real cause (rejected non-fast-forward, auth failure, missing remote, protected branch) is printed by git on stderr just before this message.

Source

Thrown at src/publish.rs:994

    let branch = Command::new("git")
        .args(["rev-parse", "--abbrev-ref", "HEAD"])
        .output()
        .context("failed to get current branch")?;

    let branch = String::from_utf8_lossy(&branch.stdout).trim().to_string();
    let branch = if branch.is_empty() || branch == "HEAD" {
        "main".to_string()
    } else {
        branch
    };

    let status = Command::new("git")
        .args(["push", "-u", "origin", &branch])
        .status()
        .context("failed to push to origin")?;

    if !status.success() {
        bail!("git push failed");
    }

    Ok(())
}

View on GitHub (pinned to a747e741ae)

Solutions

  1. Read git's stderr above the error: 'rejected' → `git pull --rebase` then retry; 'Permission denied (publickey)' → set up SSH keys or gh credential helper
  2. Run `git remote -v` and confirm origin points at the correct repo you can push to
  3. Test auth: `ssh -T git@github.com` or `gh auth status`
  4. If you intend to overwrite, use `git push --force` deliberately (never as a default)

Example fix

// before: rejected non-fast-forward
git push -u origin main  ->  git push failed
// after
git pull --rebase origin main
git push -u origin main
Defensive patterns

Strategy: retry

Validate before calling

let out = std::process::Command::new("git").args(["remote","get-url","origin"]).output()?;
if !out.status.success() {
    eprintln!("origin remote missing; add it before pushing");
    std::process::exit(1);
}
// auth probe
let t = std::process::Command::new("ssh").args(["-T","git@github.com"]).output();

Try / catch

if let Err(e) = publish(opts) {
    if e.to_string() == "git push failed" {
        eprintln!("push rejected — inspect git stderr: rebase if non-fast-forward, fix SSH/credentials if auth failed");
    } else { return Err(e); }
}

Prevention

When it happens

Trigger: `git push -u origin <branch>` exits non-zero: remote contains work you don't have (non-fast-forward), SSH key/credential not authorized for the repo, remote 'origin' missing or wrong URL, branch name rejected by push rules.

Common situations: First push of an SSH-key-less machine to a new repo created via gh; pushing over https without a credential helper; repo URL pointing at a repo the user can't write to; simultaneous divergent pushes from another machine.

Related errors


AI-assisted analysis of nikivdev/code@a747e741ae (2026-09-01). Data as JSON: /api/errors/36dba62dba869f95. Report an issue: GitHub.