为什么我无法推送到这个裸仓库?

287

你能解释一下这个工作流有什么问题吗?

$ git init --bare bare
Initialized empty Git repository in /work/fun/git_experiments/bare/
$ git clone bare alice
Cloning into alice...
done.
warning: You appear to have cloned an empty repository.
$ cd alice/
$ touch a
$ git add a
$ git commit -m "Added a"
[master (root-commit) 70d52d4] Added a
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a
$ git push
No refs in common and none specified; doing nothing.
Perhaps you should specify a branch such as 'master'.
fatal: The remote end hung up unexpectedly
error: failed to push some refs to '/work/fun/git_experiments/bare'

git push 命令总是将代码推送到我克隆的仓库吗?


你应该指定要推送的分支吗? - Rekin
3
问题解决后,它的运行非常好,不需要指定分支...只有在首次检出空存储库时才会出现此问题,非常非常令人烦恼...他们应该解决这个问题。请注意,不是克隆后! - Dean Hiller
希望这篇文章对那些尝试完成以下操作的人有所帮助:http://samranga.blogspot.com/2015/07/create-git-bitbucket-repository-from.html?view=sidebar。即使尝试从已经存在的本地项目创建git BitBucket存储库,问题中的错误也可能会出现。 - Samitha Chathuranga
6个回答

487

是的,问题在于 "bare" 中没有提交记录。如果按照(bare, alice)的顺序创建仓库,则只有第一个提交存在问题。尝试执行以下操作:

git push --set-upstream origin master

这只需要在第一次时进行。之后应该正常工作。

正如Chris Johnsen所指出的,如果您自定义了push.default,则不会遇到此问题。我喜欢upstream/tracking。


1
我正在执行 sudo apt-get upgrade git-coresudo apt-get upgrade git,但它认为没有任何更新。git --version 返回 1.7.3.1。有任何想法缺少什么吗?我承认目前 apt-get update 对我来说无法使用,但不久前它还是可以的。 - ripper234
1
@ripper234:当前的git版本是1.7.5.3。你可以忍受这种不便,使用不同的工作流程,或者手动安装最新的git而不使用debian/ubuntu包装。 - Seth Robertson
9
关于 "Recent version don't have this problem":即使在最近的版本中,push 的默认设置似乎仍然没有从 matching 改变;也许你在你的 ~/.gitconfig 文件中将 push.default 设置为 upstream / tracking(或 current)? - Chris Johnsen
@SethRobertson 当我运行你的命令时,我得到以下错误:错误: src refspec master does not match any. error: failed to push some refs to 'git@bitbucket.org:username/myproject.git'。 - IgorGanapolsky
4
尝试使用git push origin master:master来明确指定推送分支。如果这种方法不起作用,检查一下你所处的分支:可以使用git branch命令查看,也许你还没有进行第一次提交,或者你是在除了master以外的分支上进行了第一次提交。 - Seth Robertson
显示剩余7条评论

42

如果您:

 git push origin master

它将推送到裸库。

听起来你的alice仓库没有正确跟踪。

cat .git/config
这将显示默认的远程分支。 如果你...
 git push -u origin master

你应该开始跟踪那个远程分支。我不确定这个选项是否一直存在于git中。


33

这个相关问题的答案为我提供了解决方案...只是一个愚蠢的错误:

记得先提交!

https://dev59.com/vm855IYBdhLWcg3wuG0M#7572252

如果您还没有提交到本地repo,那么就没有需要推送的内容,但您所获得的Git错误信息并不能帮助您太多。


19
git push --all

推送所有内容到一个新的裸仓库,是一种规范的方法。

另一种实现相同功能的方式是创建你的新非裸仓库,然后使用以下命令创建一个裸克隆:

git clone --bare

然后使用

git remote add origin <new-remote-repo>

在原始(非裸)仓库中。


那么你给答案投了反对票?这确实是将所有内容推送到新的裸仓库的标准方法。如果它对你不起作用,那就有其他问题了。 - ebneter
你说得对,我可能不应该这样做,我知道你只是想帮忙。如果你编辑它,我会撤销我的踩。 - ripper234
谢谢,我用另一种方式编辑了它以完成相同的任务。 - ebneter
你的回答帮了我,谢谢 ;) 但是在命令的结尾,路径必须像这样出现:git push --all ../test_repo 命令的结尾需要加上repo的URL ;) - Metafaniel
@Metafaniel 这取决于你的设置方式。如果你的本地repo已经正确配置了remote,那么 "git push --all" 就可以正常工作。 - ebneter

7

在推送之前,尝试在您的alice存储库中执行以下操作:

git config push.default tracking

或者,您可以使用git config --global …将其配置为用户的默认设置。


git push默认推送到origin仓库(通常是当前仓库所克隆的仓库),但它不会默认推送当前分支——它默认只推送在源仓库和目标仓库中都存在的分支。

push.default配置变量(参见git-config(1))控制当未给出任何“refspec”参数(即在仓库名称之后的内容)时,git push将推送什么。默认值为上述行为。

以下是push.default可能的取值:

  • nothing
    This forces you to supply a “refspec”.

  • matching (the default)
    This pushes all branches that exist in both the source repository and the destination repository.
    This is completely independent of the branch that is currently checked out.

  • upstream or tracking
    (Both values mean the same thing. The later was deprecated to avoid confusion with “remote-tracking” branches. The former was introduced in 1.7.4.2, so you will have to use the latter if you are using Git 1.7.3.1.)
    These push the current branch to the branch specified by its “upstream” configuration.

  • current
    This pushes the current branch to the branch of the same name at the destination repository.

    These last two end up being the same for common cases (e.g. working on local master which uses origin/master as its upstream), but they are different when the local branch has a different name from its “upstream” branch:

    git checkout master
    # hack, commit, hack, commit
    
    # bug report comes in, we want a fix on master without the above commits
    
    git checkout -b quickfix origin/master  # "upstream" is master on origin
    # fix, commit
    git push
    

    With push.default equal to upstream (or tracking), the push would go to origin’s master branch. When it is equal to current, the push would go to origin’s quickfix branch.

在您的情况中,matching设置将在建立后更新bare。要建立它,您可以使用git push origin master

然而,upstream设置(或者可能是current)似乎更适合您期望发生的情况,因此您可能想尝试它:

# try it once (in Git 1.7.2 and later)
git -c push.default=upstream push

# configure it for only this repository
git config push.default upstream

# configure it for all repositories that do not override it themselves
git config --global push.default upstream

如果你还在使用1.7.4.2版本之前的Git,你需要使用tracking而不是upstream


1
我使用 SourceTree git 客户端,发现他们的初始提交/推送命令为:
git -c diff.mnemonicprefix=false -c core.quotepath=false push -v --tags --set-upstream origin master:master

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