Git命令行无法查看staging分支,推送提交遭拒绝求助
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
devbranch (skip this if you're already there):git checkout dev - Merge the remote
stagingchanges into yourdevbranch: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"
- If you hit merge conflicts, open your code editor to resolve them, then stage and commit the fix:
- Now push your updated
devbranch to the remotestagingbranch: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

