为什么将新文件推送到裸仓库失败:“error: src refspec master does not match any.”? Why?

3

我正在尝试这个:

mkdir ~/gitremote
cd ~/gitremote
git init --bare

我可以看到像这样的文件名:

HEAD        config      hooks       objects
branches    description info        refs

好的,那么在另一个目录中,

git clone trosky@localhost:/Users/trosky/gitremote
vi readme (add one line)
git add .
git commit -m "1st file"
git push origin master

然后它会显示错误:

$git push origin master
error: src refspec master does not match any.
error: failed to push some refs to 'trosy@localhost:/Users/trosky/gitremote'

我在谷歌上搜索了一下,发现这种错误是由于远程仓库中的空文件夹引起的。但实际上,远程仓库并不为空,而且本地我提交的文件也不是空的。为什么还会出现这个错误呢?
如何解决?谢谢。

操作系统是什么?如果是MAC,请使用“git clone /Users/trosky/gitremote”命令。如果您是Windows用户,请使用“git clone C:/\Users/\trosky/\gitremote”命令。您可以使用“git remote -v”命令检查路径是否正确。 - Marina Liu
谢谢Marina,你的“git clone”建议很有用,但是远程仓库似乎没有接收到任何新文件。我在MAC上,但是“git clone /Users/trosky/gitremoe”和“git clone 'trosy@localhost:/Users/trosky/gitremote'”之间有什么区别? - Troskyvs
@Troskyvs,我回答如下,请在远程文件夹中检查提交的sha-1。 - Marina Liu
2个回答

4

尝试使用以下语法:

git push -u origin master

由于您正在推送到一个空仓库,因此您需要显式地推送 master
否则,默认的 push.policysimple, Git 将在您的远程仓库中查找名为master的分支(由于您的裸仓库为空,它还没有master分支)。
当然,您在远程裸仓库中看不到任何自述文件,因为它是一个裸仓库:没有工作树。

'git clone /Users/trosky/gitremoe' 和 'git clone 'trosy@localhost:/Users/trosky/gitremote'之间有什么区别?

有两种方法进行文件操作,一种是使用文件协议(也称为本地协议),另一种是使用ssh协议。在这里不需要使用ssh。


谢谢,但我仍然面临同样的问题:$git push -u origin master 错误:src refspec master does not match any。 错误:无法将一些引用推送到'x@localhost:/Users/trosky/gitremote'。 - Troskyvs
@Troskyvs你在克隆的仓库的gitremote文件夹里吗?(而不是在/Users/trosky/gitremote中)。执行git status命令后返回了什么? - VonC
好的,我刚刚检查了一下,是的,你已经到了在我的裸仓库的refs/heads/master文件下检查SHA-1的点了。 - Troskyvs

0

你可以使用 git clone /Users/trosky/gitremote

git clone /Users/trosky/gitremote 使用了本地协议。因为你的远程仓库在你的本地电脑上,所以本地协议是适合且快速的。

git clone 'trosy@localhost:/Users/trosky/gitremote' 使用了scp来传输数据,主要用于不同设备之间的数据传输。

当你推送到远程仓库后,你可能会去远程仓库文件夹找不到推送的文件。但实际上它是存在的。因为bare repo没有工作目录。它存储在/Users/trosky/gitremote/objects中。你可以检查/Users/trosky/gitremote/refs/heads/master中的提交SHA-1是否与本地仓库一致。


是的,我在我的回答中已经说明了所有这些。 - VonC
不完全是,我首先在我的评论中提供了它。 - Marina Liu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接