git push失败 - 我做错了什么?

Win*_*Win 3 git

在本地配置和使用git似乎工作正常:

~/sb> mkdir proj1
~/sb> cd proj1
~/sb/proj1> echo "asdf" > file1.txt
~/sb/proj1> git init
~/sb/proj1> git add .
~/sb/proj1> git commit -a -m "Import"
~/sb/proj1> git branch
* master
Run Code Online (Sandbox Code Playgroud)

当我尝试将其送到中央存储库时,问题就出现了:

~/sb/proj1> cd /home
~> mkdir temp-repo
~> cd temp-repo/
~/temp-repo> git init --bare
Initialized empty Git repository in /home/temp-repo/
~/temp-repo> cd ~/sb/proj1/
~/sb/proj1> git clone /home/temp-repo/
Cloning into temp-repo...
done.
warning: You appear to have cloned an empty repository.
~/sb/proj1> git push origin master
fatal: 'origin' does not appear to be a git repository
fatal: The remote end hung up unexpectedly
Run Code Online (Sandbox Code Playgroud)

注:--bareorigin master上面从解决一个问题张贴在这里拍摄:推到一个Git仓库不起作用

然而,我似乎并不知道我错过了什么.一定是微不足道的,但它是什么?

更新: @Firoze Lafeer的答案如下:

~/sb/proj1> cd /home
~> mkdir temp-repo
~> cd temp-repo/
~/temp-repo> git init --bare
Initialized empty Git repository in /home/temp-repo/
~/temp-repo> cd ~/sb/proj1/
~/sb/proj1> git remote add origin /home/temp-repo
~/sb/proj1> git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 230 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /home/temp-repo
 * [new branch]      master -> master
~/sb/proj1>
Run Code Online (Sandbox Code Playgroud)

现在我需要了解原因.特别是,我在这个答案中误解了什么,表明做了git clone以前的事情git push origin master.

Fir*_*eer 5

你这样做的时候:

 git clone /home/temp-repo/
Run Code Online (Sandbox Code Playgroud)

你现在应该在一个新目录中克隆一个repo~/sb/proj1/temp-repo /

如果我正确理解你的问题,那不是你想要的.

如果您已在〜/ sb/proj1中拥有存储库,则不要克隆其他存储库.只需添加另一个作为远程.

所以假设您先在〜/ sb/proj1中创建了本地仓库,然后创建了temp-repo,并且您希望从第一个仓库推送到第二个仓库:

git remote add origin /home/temp-repo
git push origin master
Run Code Online (Sandbox Code Playgroud)

或者只是反过来做.首先在/ home/temp-repo中创建repo,然后克隆它并推送到它.

希望有帮助吗?

编辑

希望进一步解释为什么不想在这里克隆:

如果您尚未创建本地存储库,则表示存在克隆.因此,您要求git复制一些远程存储库,然后将其隐式设置为新本地副本中名为"origin"的远程存储库.在你的情况下,你已经有了一个本地仓库,所以你想要的只是将另一个仓库设置为名为"origin"的远程仓库.

所以现在你的本地仓库知道另一个名为'origin'的回购.然后你可以推动那个"起源".当然,你不必将它称为"起源",如果你愿意,可以称之为'猫'然后'git push cats master'.

或者你可以反过来做.所以你可以先做temp-repo,然后做一些像:

cd ~/sb
git clone /home/temp-repo proj2
cd proj2
# make some changes...
git push origin master
Run Code Online (Sandbox Code Playgroud)

这也有效.将temp-repo复制到新目录proj2中的新repo中.然后在proj2中,新的repo知道一个叫做"origin"(/ home/temp-repo)的远程仓库,所以可以将提交推回给它.