我和几个 friend 在 GitLab 有个项目,当然有 master 分支,还有一些其他的。当我克隆存储库时,我还使用命令 git remote add upstream ...
创建了一个上游。
然后,我发布了 git fetch upstream
。接着是 git checkout upstream/test1
。现在,如果我输入 git branch -a
,我会得到如下输出:
* (HEAD detached at upstream/test1)
master
remotes/origin/HEAD -> origin/master
remotes/origin/master
remotes/upstream/test1
remotes/upstream/master
这一切都很好,但后来我对我的 upstream/test1
分支中的代码做了一些更改,我想将它们推送到 origin/test1
存储库,我在标题上收到错误消息。请注意,我按照以下步骤进行推送:
git add .
git commit -m "Sample message"
git push -u origin test1
如果我发出 git show-ref
,我会得到以下输出:
refs/heads/master
refs/remotes/origin/HEAD
refs/remotes/origin/master
refs/remotes/upstream/test1
refs/remotes/upstream/master
我检查了以下 questions ,但没有发现它有帮助。有什么解决办法吗?
最佳答案
您似乎没有名为 test1
的本地分支。您有一个名为 test1
的 remote 分支与您的 upstream
远程关联。
您不应该直接编辑 upstream/test1
分支。事实上,尝试检查它应该会产生警告:
$ git checkout upstream/test1
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
您应该首先检查跟踪远程分支的本地分支,它应该看起来像这样:
$ git checkout test1
Branch test1 set up to track remote branch test1 from upstream by rebasing.
Switched to a new branch 'test1'
完成此操作后,未修饰的 git push
将推送到 upstream
远程,而 git push origin test1
将推送到您的 origin
远程。在此处添加 -u
标志将切换跟踪分支,这样您的分支将跟踪 origin/test1
而不是跟踪 upstream/test1
,因此 future 的 git pull
和 git push
操作将默认引用该远程分支 (origin/test1
)。
关于git - 错误 : src refspec does not match any,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40202284/