我不明白这个Git :-(

时间:2014-03-14 05:49:12

标签: git push

我正在使用子模块并且无法让它们工作,所以我认为我只是尝试一些简单的事情,绝对不会失败。确实如此。并且错误消息对我来说听起来像是有人说:默认情况下,您无法进入非空的存储库,因为我们会搞砸repo和您的工作目录。我无法想象可能出现的问题

我运行这个简单的脚本来创建两个Repos。 '主'使用单个文件V,其中包含'版本1',以及克隆是Main的克隆。然后它将V更改为版本2'并承诺改变。然后它会尝试推送它并且它会发出错误信息。

mkdir Main
cd Main
git init
echo Version 1 > V
git add V
git commit -m "Initial"
mkdir ../Clone
cd ../Clone
git clone ../Main .
echo Version 2 > V
git commit -m "V2" -a
git push

输出是:

Initialized empty Git repository in /private/tmp/T/X/Main/.git/
[master (root-commit) a94775b] Initial
 1 file changed, 1 insertion(+)
 create mode 100644 V
Cloning into '.'...
done.
[master 385cf21] V2
 1 file changed, 1 insertion(+), 1 deletion(-)
Counting objects: 5, done.
Writing objects: 100% (3/3), 237 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: error: is denied, because it will make the index and work tree inconsistent
remote: error: with what you pushed, and will require 'git reset --hard' to match
remote: error: the work tree to HEAD.
remote: error: 
remote: error: You can set 'receive.denyCurrentBranch' configuration variable to
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into
remote: error: its current branch; however, this is not recommended unless you
remote: error: arranged to update its work tree to match what you pushed in some
remote: error: other way.
remote: error: 
remote: error: To squelch this message and still keep the default behaviour, set
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To /tmp/T/X/Clone/../Main
 ! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to '/tmp/T/X/Clone/../Main'

真的?更新当前分支是否被拒绝? Riiggghhht ......版本控制究竟是什么意思?

我意识到git是被人们使用的,有些人认为它甚至可以促进。所以,显然,我做某事的整个方式肯定是错的,但我不能为我的生活看到这可能是错的。

我做的每件事都没有像我期望的那样工作。

1 个答案:

答案 0 :(得分:1)

git push不是为了更新工作树而设计的。 git push将更新远程索引但不更新工作树。因此,如果您推送到工作树当前分支的分支,工作树将不同步。这可能令人困惑。如果Git让你无论如何推动,你可能会认为工作树会更新,但它不会成功。所以为了拯救你的痛苦,它不会让你全力以赴。

但是如果你仍然想要推送,你可以,并且错误消息的第二部分解释了你可以做的一些选项。

通常,存储库以裸体形式位于服务器上,没有工作树。您只在开发环境中使用具有工作树的存储库,并且通常不会推送到这些存储库。

我想知道你是来自Bazaar还是类似的。在Bazaar,在本地克隆和分支之间推送和拉动是正常的。与Git不同,它在推送到本地克隆时更新工作树。我在Git之前认识了Bazaar,起初我很困惑(以及其他事情)。