如何修复git推送被拒绝时的错误

如何修复git推送被拒绝时的错误,git,Git,我启动git时首先克隆了一个存储库,然后是“git提交”,但当我执行“git推送”时 我得到这个错误:但它说'拒绝更新结帐分支' $ git push Counting objects: 17, done. Delta compression using up to 4 threads. Compressing objects: 100% (13/13), done. Writing objects: 100% (13/13), 2.72 KiB, done. Total 13 (delta 5

我启动git时首先克隆了一个存储库,然后是“git提交”,但当我执行“git推送”时 我得到这个错误:但它说'拒绝更新结帐分支'

$ git push
Counting objects: 17, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (13/13), done.
Writing objects: 100% (13/13), 2.72 KiB, done.
Total 13 (delta 5), 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 ssh://michael@16.336.22.38/home/michae/scripts
 ! [remote rejected] master -> master (branch is currently checked out)
这是什么意思

我搜索和阅读


我甚至尝试了“git-push-origin-HEAD”,这给了我同样的错误:

目标上的存储库不是“裸”的。也就是说,它已签出文件,而不仅仅是一个.git数据库

约定是使用裸“存储库”作为推送的目标


错误消息告诉您需要知道的一切:您可以更改远程repo中的配置以允许此操作,也可以将其呈现为裸机。

也许您应该将远程repo转换为无工作副本的裸机repo

ssh user@remote
cd /path/containing/repo
git clone --bare repo/ repo.git
现在,在您的工作repo中,编辑.git/config以更新远程服务器的URL。在其末尾添加一个
.git

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = user@remote:/path/to/repo.git
然后跑

git push origin HEAD

推送当前分支。

有两种类型的Git存储库:裸存储库和非裸存储库。裸存储库只包含提交、分支、标记等;非裸存储库还包含当前工作副本。如果您查看存储库,就可以知道它是否是空的。如果您有一个包含项目中文件的目录,以及一个
.git
目录,那么这是一个非裸存储库;
.git
目录之外的文件是当前工作副本。如果您只有存储库的内容,例如
refs
对象
,等等,那么这就是一个空存储库。它没有工作副本

ssh user@remote
cd /path/containing/repo
git clone --bare repo/ repo.git
只能推送到裸存储库(除非覆盖默认配置)。您可以从非裸机存储库中提取,也可以提取到非裸机存储库中,但不能将其推入其中。这是为了防止更新工作副本时出现问题。工作副本是您当前状态的副本;您可能已经编辑了文件,等等。当您拉入存储库时,最新的更改将签出到您的工作副本中;如果您有一些未签入的本地更改,它们将与从中提取的存储库中的更改合并,或者签出被拒绝,您需要修复本地副本才能签出这些新更改

在要推送到的存储库中,如果发生冲突,则无法立即进行更改。工作树将会过时;回购协议中的内容和工作树中的内容将不同步。为了防止这种情况发生,Git拒绝让您进行非裸回购


建议使用裸回购作为您的“中心”回购,推送和拉出,使用非裸回购作为您的工作树;你实际工作的地方。因为看起来您已经有了一个已从中克隆的回购协议,所以您需要从中创建一个裸回购协议,并更新您的
来源
,以指向裸回购协议,而不是非裸回购协议。您可以使用git clone--bare path/to/original/repo path/to/bare/repo.git创建裸回购(通常将裸回购命名为
name.git
)。如果这将在同一台机器上的多人之间共享,您还应传递
--shared
以正确设置权限。然后在工作副本中,运行
git remote set url origin path/to/bare/repo.git
,以及
git remote set url——将origin path/to/bare/repo.git推送到/code>(或
user@host:/path/to/bare/repo.git
如果您通过SSH访问它)。

我做了“git clone--bare path/to/original/repo path/to/bare/repo.git”。但是当我做“git remote设置url origin path/to/bare/repo.git”时,我得到了致命的结果:没有这样的远程“origin”@michael你有什么远程设备?
git-remote
向您展示了什么?通常,当您从回购中克隆时,原始设置为
原始
,但这可能有所不同。