为什么我不能推送到这个裸仓库?


283

您能解释一下此工作流程有什么问题吗?

$ 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总是推送到我从中克隆的存储库吗?


您不应该指定要推送的分支吗?
雷金

3
没有克隆后!!!在解决问题之后,它可以很好地工作并且无需指定分支...仅在第一次检出空存储库时才会发生,这非常非常烦人...他们应解决此问题。
迪恩·希勒

希望这篇文章对尝试进行上述操作对某人有用-samranga.blogspot.com/2015/07/… 即使尝试从已经存在的本地项目
Samitha

Answers:


483

是的,问题在于“裸”中没有提交。如果您按顺序(裸机,驴友)创建存储库,那么这仅是第一次提交的问题。尝试做:

git push --set-upstream origin master

仅在第一次时才需要。之后,它应该可以正常工作。

正如Chris Johnsen指出的那样,如果push.default是自定义的,则不会出现此问题。我喜欢上游/跟踪。


1
我正在做sudo apt-get upgrade git-coresudo apt-get upgrade git它认为不需要更新。git --version返回1.7.3.1。知道缺少什么吗?我承认目前apt-get update不适用于我,但不久前还没有。
ripper234 2011年

1
@ ripper234:git的当前版本是1.7.5.3您可以忍受不便,使用其他工作流程,或者手动安装最新的git,而无需安装debian / ubuntu软件包。
塞斯·罗伯逊

是的,我忘记了软件打包之前需要花费一些时间。我是来自Windows的linux noob,习惯单击此处安装最新版本。
ripper234 2011年

9
关于“最新版本没有此问题”:即使在最新版本中,推送的默认值似乎也没有从更改matching。也许您已将/ (或)push.default设置为您的?upstreamtrackingcurrent~/.gitconfig
克里斯·约翰森

4
尝试git push origin master:master使其明确。如果那不起作用,请检查您所在的分支:git branch也许您尚未进行第一次提交,或者您是在master以外的其他分支上进行了该提交。
塞斯·罗伯森

43

如果你:

 git push origin master

它会推送到裸仓库。

听起来您的爱丽丝仓库未正确跟踪。

cat .git/config

这将显示默认的远程和分支。

如果你

 git push -u origin master

您应该开始跟踪该遥控器和分支机构。我不确定该选项是否一直在git中。


30

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

请记住先提交!

https://stackoverflow.com/a/7572252

如果您尚未提交本地存储库,则没有任何可推送的内容,但是您收到的Git错误消息并没有太大帮助。


17
git push --all

是将所有内容推送到新的裸存储库的规范方法。

另一种执行相同操作的方法是创建新的非裸存储库,然后使用

git clone --bare

然后使用

git remote add origin <new-remote-repo>

在原始(非裸露)存储库中。


...所以您否决了答案?这将所有内容推送到新的裸存储库的标准方法。如果对您不起作用,则还有其他问题。
ebneter 2011年

您是对的,我可能不应该知道,我知道您只是在尝试提供帮助。如果您对其进行编辑,我将撤消我的反对意见。
ripper234

谢谢,用另一种方式编辑它来完成相同的任务。
ebneter 2011年

您的回答对我有所帮助;)但是,在命令末尾,必须这样显示路径:git push --all ../test_repo命令末尾的存储库URL;)
Metafaniel 2012年

@Metafaniel这取决于您如何设置。如果您的本地存储库已经正确配置了远程服务器,则“ git push --all”应该可以正常工作。
ebneter

7

在您的alice存储库中尝试此操作(在推送之前):

git config push.default tracking

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


git push确实默认为origin存储库(通常是您从中克隆当前存储库的存储库),但是它不默认为推送当前分支-默认为仅推送源存储库和目标存储库中都存在的分支。

push.default配置变量(见的git-配置(1) )控制什么git push时候(存储库名称后,即东西)没有给出任何“的Refspec”参数将推动。默认值提供上述行为。

以下是可能的值push.default

  • nothing
    这迫使您提供“ refspec”。

  • matching(默认)
    这将推送源存储库和目标存储库中都存在的所有分支。
    这完全独立于当前签出的分支。

  • upstreamtracking
    (两个值表示相同的含义。不推荐使用后者,以避免与“远程跟踪”分支混淆。前者是在1.7.4.2中引入的,因此,如果您使用的是Git 1.7.3.1。,则必须使用后者。 )
    这些将当前分支推送到其“上游”配置指定的分支。

  • current
    这会将当前分支推送到目标存储库中具有相同名称的分支。

    对于通常情况(例如在使用Origin / master作为上游的本地master上工作),这最后两个最终是相同的,但是当本地分支的名称不同于其“上游”分支时,它们是不同的:

    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
    

    随着push.default等于upstream(或tracking),推送会去origin分支。当等于时current,推送将转到originquickfix分支。

建立后,该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客户端,看到它们的初始commit / push命令是:

git -c diff.mnemonicprefix=false -c core.quotepath=false push -v --tags --set-upstream origin master:master
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.