昨天,我发布了一个关于如何从我的一台机器克隆Git存储库到另一台机器的问题,我如何从另一台机器“克隆Git”?

现在我可以成功地将Git存储库从源存储库(192.168.1.2)克隆到目标存储库(192.168.1.1)。

但是当我对文件进行编辑,git commit -a -m "test"和git push时,我在目的地(192.168.1.1)上得到了这个错误:

git push                                                
hap@192.168.1.2's password: 
Counting objects: 21, done.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (11/11), 1010 bytes, done.
Total 11 (delta 9), reused 0 (delta 0)
error: refusing to update checked out branch: refs/heads/master
error: By default, updating the current branch in a non-bare repository
error: is denied, because it will make the index and work tree inconsistent
error: with what you pushed, and will require 'git reset --hard' to match
error: the work tree to HEAD.
error: 
error: You can set 'receive.denyCurrentBranch' configuration variable to
error: 'ignore' or 'warn' in the remote repository to allow pushing into
error: its current branch; however, this is not recommended unless you
error: arranged to update its work tree to match what you pushed in some
error: other way.
error: 
error: To squelch this message and still keep the default behaviour, set
error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To git+ssh://hap@192.168.1.2/media/LINUXDATA/working
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'git+ssh://hap@192.168.1.2/media/LINUXDATA/working'

我正在使用两个不同版本的Git(远程上的1.7版本和本地机器上的1.5版本)。这是一个可能的原因吗?


当前回答

我喜欢在远程机器上仍然有一个可用的存储库的想法,但不是一个虚拟的分支,我喜欢使用:

git checkout --detach

这似乎是Git的一个非常新的特性——我使用的是Git 1.7.7.4版本。

其他回答

我在使用Git同步Android手机和笔记本电脑上的存储库时也遇到了同样的问题。我的解决方案是拉而不是推,就像@CharlesBailey建议的那样。

git在Android存储库上推送origin master失败,与@hap497得到的错误消息相同,因为推送到存储库+工作副本的非裸签出。

Git在笔记本存储库上拉droid master,并为我工作复制。当然,你需要先运行git remote add droid /media/KINGSTON4GB/notes_repo/。

git配置——local receive.denyCurrentBranch updateInstead

https://github.com/git/git/blob/v2.3.0/Documentation/config.txt#L2155

在服务器存储库上使用它,如果没有发生未跟踪的覆盖,它也会更新工作树。

正如VonC在评论中提到的,它是在Git 2.3中添加的。

我已经编译了Git 2.3并尝试了一下。示例用法:

git init server
cd server
touch a
git add .
git commit -m 0
git config --local receive.denyCurrentBranch updateInstead

cd ..
git clone server local
cd local
touch b
git add .
git commit -m 1
git push origin master:master

cd ../server
ls

输出:

a
b

耶,b被推了!

您应该只推送到一个裸露的存储库。裸存储库是指没有检出分支的存储库。如果要cd到一个裸存储库目录,则只能看到.git目录的内容。

只需几个设置步骤,您就可以轻松地使用一行程序部署更改到您的网站

git push production

这很简单,你不需要登录到远程服务器并进行拉取或其他操作。请注意,如果您不将生产检出用作工作分支,这将是最好的工作方式!(OP的工作环境略有不同,我认为@Robert Gould的解决方案很好地解决了这个问题。此解决方案更适合部署到远程服务器。)

首先,你需要在你的服务器上的某个地方建立一个裸库,在你的webroot之外。

mkdir mywebsite.git
cd mywebsite.git
git init --bare

然后创建文件hooks/post-receive:

#!/bin/sh
GIT_WORK_TREE=/path/to/webroot/of/mywebsite git checkout -f

并使文件可执行:

chmod +x hooks/post-receive

在本地机器上,

git remote add production git@myserver.com:mywebsite.git
git push production +master:refs/heads/master

都准备好了!现在,将来您可以使用git push production来部署您的更改!

这个解决方案的功劳归http://sebduggan.com/blog/deploy-your-website-changes-using-git/。在那里可以找到关于正在发生的事情的更详细的解释。

我的解决方案是:

在远程:

git checkout -b some_tmp_name

地方:

git push

在远程:

git checkout master
git branch -d some_tmp_name

但这不是真正的解决方案,这只是一种变通方法。