简体   繁体   English

当“git push”到github时出错

[英]Error when “git push” to github

I have a public repository at github.com with 2 branches : master and test . 我在github.com上有一个公共存储库,有2个分支: mastertest

I created a new directory locally and did: 我在本地创建了一个新目录并执行了:

[ ] git clone git@github.com:{username}/{projectname}.git

Then I created a new branch named my_test with 然后我创建了一个名为my_test的新分支

[ ] git branch my_test

and switched to it. 并切换到它。

[ ] git checkout my_test

Then I merged it from my test branch of my public repository with 然后我将其与我的公共存储库的test分支合并

[ ] git merge origin/test

and it resulted in a fast forward. 它导致了快速前进。

I made some changes and committed them. 我做了一些改变并承诺了。 Then I tried to push the local my_test branch to the public test branch at github with 然后我尝试将本地my_test分支推my_test github上的公共test分支

[ ] git push git@github.com:{username}/{projectname}.git test 

but I got this error: 但是我收到了这个错误:

error: src refspec test does not match any.
fatal: The remote end hung up unexpectedly
error: failed to push some refs to 'git@github.com:{username}/{projectname}.git

What am I doing wrong ? 我究竟做错了什么 ?

Perhaps try: 也许试试:

git push git@github.com:{username}/{projectname}.git HEAD:test

The format of the last parameter on that command line is a refspec which is a source ref followed by a colon and then the destination ref. 该命令行上最后一个参数的格式是refspec ,它是一个源ref,后跟一个冒号,然后是目标ref。 You can also use your local branch name ( my_test ) instead of HEAD to be certain you're pushing the correct branch. 您也可以使用本地分支名称( my_test )而不是HEAD来确定您正在推送正确的分支。

The documentation for git push has more detail on this parameter. git push的文档有关于此参数的更多详细信息。

Also, you don't need to type out the whole url each time you want to push. 此外,每次要推送时都不需要输入整个URL。 When you ran the clone, git saved that URL as 'origin', that's why you can run something like 'merge origin/test' - it means the 'test' branch on your 'origin' server. 当您运行克隆时,git将该URL保存为“origin”,这就是为什么您可以运行“merge origin / test”之类的东西 - 它意味着“origin”服务器上的“test”分支。 So, the simplest way to push to your server in that case would be: 因此,在这种情况下推送到服务器的最简单方法是:

git push origin my_test:test

That will push your local 'my_test' branch to the 'test' branch on your 'origin' server. 这会将您的本地“my_test”分支推送到“origin”服务器上的“test”分支。 If you had named your local branch the same as the branch on the server, then the colon is not neccesary, you can simply do: 如果您将本地分支命名为与服务器上的分支相同,则冒号不是必需的,您只需执行以下操作:

git push origin test

This error also comes up if you try to push to a new repository without having committed anything first. 如果您尝试在没有先提交任何内容的情况下尝试推送到新存储库,也会出现此错误。 Try: 尝试:

git add -A
git commit -am 'Initial commit'

And then try your push again. 然后再试一次。

I think here you will need to set up branch tracking. 我想在这里你需要设置分支跟踪。 Please run the following to enable tracking 请运行以下命令以启用跟踪

git branch --track my_test origin/my_test

To test 去测试

git push -u origin my_test
git pull origin my_test

You need to make sure that your local repository have the same name as your remote repository you're trying to push. 您需要确保本地存储库与您尝试推送的远程存储库具有相同的名称。

First, change repository using git branch -m "test" so that "my_test" would be "test". 首先,使用git branch -m“test”更改存储库,以便“my_test”为“test”。 Second, just git push origin test 第二,只是git push origin test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM