You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Git命令行无法查看staging分支,推送提交遭拒绝求助

Fixing the git push origin dev:staging Rejection Issue

Hey there! Let's break down what's happening here and get your code pushed properly.

What's Going On?

The error ! [rejected] dev -> staging (fetch first) tells us one key thing: the staging branch already exists on your remote repository, but your local repo hasn't synced the latest info about it. That's why git branch -a didn't show it initially—your local only tracks branches it's already fetched from the remote.

Step-by-Step Fixes

1. First, Sync Remote Branch Info

Run this command to pull all the latest branch data from your remote repository:

git fetch origin

Now if you run git branch -a again, you should see remotes/origin/staging listed—this is your local reference to the remote staging branch.

2. Option 1: Safe Merge (Recommended)

If you want to preserve any existing changes on the remote staging branch, merge them into your local dev first to avoid overwriting work:

  • Make sure you're on your local dev branch (skip this if you're already there):
    git checkout dev
    
  • Merge the remote staging changes into your dev branch:
    git merge origin/staging
    
    • If you hit merge conflicts, open your code editor to resolve them, then stage and commit the fix:
      git add .
      git commit -m "Resolve merge conflicts with origin/staging"
      
  • Now push your updated dev branch to the remote staging branch:
    git push origin dev:staging
    

3. Option 2: Force Push (Use With Extreme Caution!)

Only use this if you're 100% certain the remote staging branch's current content is obsolete and can be fully overwritten:

git push origin dev:staging --force

For a slightly safer alternative (it won't overwrite changes you haven't fetched, available in Git 2.30+), use:

git push origin dev:staging --force-with-lease

Quick Tip

Keep in mind: git branch -a only shows branches your local repo knows about. When someone creates or updates a branch on the remote, you need to run git fetch to sync that info locally.

内容的提问来源于stack exchange,提问作者Prithviraj Mitra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:51:23