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

$ 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不总是push到我克隆的存储库吗?


当前回答

在你的alice存储库中试试这个(在push之前):

git config push.default tracking

或者,使用git config将其配置为默认值global ....


Git push默认指向的是源存储库(通常是您从中克隆出当前存储库的存储库),但它默认不推送当前分支——它默认只推送源存储库和目标存储库中同时存在的分支。

push.default配置变量(参见git-config(1))控制git push在没有给定任何“refspec”参数(即存储库名称后面的内容)时将推送什么。默认值提供上述行为。

下面是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.

在您的场景中,一旦建立了bare的master,匹配设置将更新它。要建立它,你可以使用git push origin master一次。

然而,上游设置(或当前设置)似乎更符合你的期望,所以你可能想尝试一下:

# 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,你将需要使用跟踪而不是上行)。

其他回答

是的,问题是在“bare”中没有提交。如果你在顺序(bare,alice)中创建repo,这是第一次提交时才会出现的问题。试着做:

git push --set-upstream origin master

只有第一次才需要这样做。之后就可以正常工作了。

正如Chris Johnsen指出的,如果你的push.default是定制的,你就不会有这个问题。我喜欢上游/跟踪。

如果你:

 git push origin master

它将推到裸回购。

听起来你的爱丽丝回购没有正确跟踪。

cat .git/config

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

如果你

 git push -u origin master

你应该开始追踪遥控器和分支。我不确定git中是否一直有这个选项。

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

记得先提交!

https://stackoverflow.com/a/7572252

如果您还没有提交到本地回购,则没有什么可推的,但是您返回的Git错误消息对您没有太大帮助。

git push --all

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

另一种方法是创建新的非裸库,然后使用

git clone --bare

然后使用

git remote add origin <new-remote-repo>

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

在你的alice存储库中试试这个(在push之前):

git config push.default tracking

或者,使用git config将其配置为默认值global ....


Git push默认指向的是源存储库(通常是您从中克隆出当前存储库的存储库),但它默认不推送当前分支——它默认只推送源存储库和目标存储库中同时存在的分支。

push.default配置变量(参见git-config(1))控制git push在没有给定任何“refspec”参数(即存储库名称后面的内容)时将推送什么。默认值提供上述行为。

下面是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.

在您的场景中,一旦建立了bare的master,匹配设置将更新它。要建立它,你可以使用git push origin master一次。

然而,上游设置(或当前设置)似乎更符合你的期望,所以你可能想尝试一下:

# 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,你将需要使用跟踪而不是上行)。