无法推送到 git 存储库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3221859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 08:43:18  来源:igfitidea点击:

cannot push into git repository

git

提问by cards

This is what I have done so far and I will say this procedure worked on Ubuntu 9.10 which perhaps had a different version of git.

这是我到目前为止所做的,我会说这个过程适用于 Ubuntu 9.10,它可能有不同版本的 git。

server: mkdir ~/git

local: scp -r /../project [email protected]:~/git/
server: cd git
        cd project
        git init 
        git add .
        git commit -a -m "initial"

local: git clone [email protected]:/../git/project /home/name/project
   cd project
   capify .  (from the ruby gem capistrano)
   git add .
   git commit -a -m "capified"
   git push

When I try to push this out I get this error message:

当我尝试将其推出时,我收到此错误消息:

   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 ...
   ! [remote rejected] master -> master (branch is currently checked out)
   error: failed to push some refs to

回答by HaveF

At server side, do this:

在服务器端,执行以下操作:

git config receive.denyCurrentBranch ignore

Then you can push at local.

然后你可以在本地推送。

回答by VonC

Pushing to a non-bare repo is now possible (Git 2.3.0 February 2015).
And it is possible when you are pushing the branch currently checked out at the remote repo!

现在可以推送到非裸仓库(Git 2.3.0 2015 年 2 月)。
当您推送当前在远程仓库中检出的分支时,这是可能的!

See commit 1404bcbby Johannes Schindelin (dscho):

提交1404bcb约翰内斯Schindelin( )dscho

receive-pack: add another option for receive.denyCurrentBranch

When synchronizing between working directories, it can be handy to update the current branch via 'push' rather than 'pull', e.g. when pushing a fix from inside a VM, or when pushing a fix made on a user's machine (where the developer is not at liberty to install an ssh daemon let alone know the user's password).

The common workaround – pushing into a temporary branch and then merging on the other machine – is no longer necessary with this patch.

The new option is:

receive-pack: 添加另一个选项 receive.denyCurrentBranch

在工作目录之间同步时,通过 ' push' 而不是 ' pull'更新当前分支会很方便,例如当从 VM 内部推送修复时,或者当推送在用户机器上进行的修复时(开发人员不在那里)安装 ssh 守护进程的自由,更不用说知道用户的密码了)。

这个补丁不再需要常见的解决方法——推送到一个临时分支,然后在另一台机器上合并。

新选项是:

updateInstead

Update the working tree accordingly, but refuse to do so if there are any uncommitted changes.

相应地更新工作树,但如果有任何未提交的更改,则拒绝这样做。

That is:

那是:

git config receive.denyCurrentBranch updateInstead

回答by Dipstick

As I pointed out in this post heroku-like git setup?pushing to working repositories is a bit dangerous as any work in progress is not taken into account by the push, and it is quite easy to subsequently lose any uncommitted changes (basically the working HEAD can get out of step with the working branch HEAD). Git now has a warning to inform you of this - the, gory, details are in the following link:

正如我在这篇文章中指出的类似 heroku 的 git 设置?推送到工作存储库有点危险,因为推送不考虑正在进行的任何工作,并且随后很容易丢失任何未提交的更改(基本上工作 HEAD 可能与工作分支 HEAD 脱节)。Git 现在有一个警告来通知你这一点 - 血腥的细节在以下链接中:

git push to a non-bare repository

git push 到非裸仓库

It is recommended that your published repository should be a bare repo which does not have a checked out tree. Bare repos are created using the "git clone --bare" option.

建议您发布的存储库应该是一个没有检出树的裸存储库。使用“git clone --bare”选项创建裸仓库。

回答by Motra

Try below command:

试试下面的命令:

git config receive.denyCurrentBranch warn

回答by nilsi

Since you ran git initon the server you created a working directoybut I think you wanted to make a bare repositoryinstead.

由于您git init在服务器上运行,因此您创建了一个工作目录,但我认为您想改为创建一个裸存储库

Use a working directory when you want to add, edit and delete files in your project locally on your dev machine.

当您想在开发机器上本地添加、编辑和删除项目中的文件时,请使用工作目录。

When you want to share your project, make a bare repository by git init --bare project.giton the server then clone it to your machine and you will be able to push to it.

当你想共享你的项目时,git init --bare project.git在服务器上创建一个裸仓库,然后将它克隆到你的机器上,你就可以推送到它了。

If you don't want to create a new one now then you can clone your project into a new bare repo by git clone --bare project new-bare-project.git

如果你现在不想创建一个新的,那么你可以将你的项目克隆到一个新的裸仓库中 git clone --bare project new-bare-project.git

回答by J D

VonC answer was helpful but it took me some time to realize that for Windows the command would be the following without the equals.

VonC 的回答很有帮助,但我花了一些时间才意识到,对于 Windows,命令将是以下没有等号的命令。

d:\Repositories\repo.git>git config receive.denyCurrentBranch updateInstead

My version is Git for Windows v2.11.1

我的版本是 Git for Windows v2.11.1

回答by AllenBooTung

Both of client and server side

客户端和服务器端

d:\Repositories\repo.git>git config receive.denyCurrentBranch updateInstead

d:\Repositories\repo.git>git config receive.denyCurrentBranch updateInstead

Didn't work for me.Both of client and server's version are 2.16.2.windows.1

对我不起作用。客户端和服务器的版本都是 2.16.2.windows.1

I added this to \\File-server\projectFolder\.git\config

我将此添加到 \\File-server\projectFolder\.git\config

[receive]
    denyCurrentBranch = updateInstead

This works for windows.Don't need init --bare

这适用于 windows.Don 不需要 init --bare