如何将推送到Tag的提交同步至其他分支?(npm+Git场景)
npm version) Let’s break down exactly what’s happening here, step by step—this is a common gotcha when mixing npm version with Git branch workflows.
First: Why Did git push origin master Fail?
When you ran npm version 1.0.0, here’s what it did under the hood:
- Updated your
package.jsonto version1.0.0 - Staged and committed that change to the current branch you were on (not
master, presumably—maybe a feature/test branch, or even a detached HEAD state if you’d checked out a tag earlier) - Created the
1.0.0tag pointing to that new commit
The problem? Your local master branch didn’t have that package.json update commit yet. When you tried to push master, either:
- You weren’t actually on the
masterbranch when running the push command, so Git couldn’t find the ref you were trying to push (hence the "src refspec does not match any" error) - Or your local
masterwas still at an older commit, with no new changes to push, but you expected the version update to be there
Let’s Unpack Your Working Command Chain
Each step serves a specific purpose to get that version update into master:
1. git commit -a --amend --no-edit
-aautomatically stages all tracked, modified files (here, just thepackage.jsonupdate fromnpm version).--amendtakes your current changes and replaces the most recent commit with a new one (instead of creating a brand new commit).--no-editkeeps the original commit message from thenpm versioncommit, so you don’t have to edit it manually.- Key use case here: If you were in a detached HEAD state (e.g., your terminal was pointing directly at the
1.0.0tag instead of a branch), this command "reattaches" that version change to a mutable commit that you can branch from.
2. git branch temp-branch
This creates a temporary branch named temp-branch that points to your amended commit (the one with the package.json update). Think of this as "saving" that version change to a branch so you can move it over to master.
3. git checkout master
Switches your working directory over to your master branch—now you’re ready to bring the version update into this branch.
4. git merge temp-branch
Merges the commit from temp-branch into master. Since master doesn’t have any conflicting changes with the version update, this will be a fast-forward merge: Git just moves master’s HEAD pointer to match temp-branch’s HEAD, so master now includes the package.json update.
5. git push origin master
Now your local master branch has the new version commit, which matches the state you want to push to the remote. Git can successfully push this new commit up to origin/master.
A Simpler Alternative (If You Want It)
Instead of the temp branch dance, you could directly cherry-pick the tag’s commit into master:
git checkout master git cherry-pick 1.0.0 git push origin master
This copies the exact commit from the 1.0.0 tag into master, skipping the amend and temp branch steps.
内容的提问来源于stack exchange,提问作者mu_sa

