git - git push 被拒绝时如何修复错误

标签 git

我通过先克隆一个存储库然后“git commit”来启动 git,但是当我执行“git push”时 我收到此错误:但它说“拒绝更新 checkout 分支”?

$ 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)

这是什么意思?

我搜索和阅读 How to cope with "rejected" on git push?

我什至尝试了 'git push origin HEAD' 却给出了同样的错误:

最佳答案

有两种类型的 Git 存储库:裸机和非裸机。裸仓库只包含提交、分支、标签等;非裸存储库还包含当前工作副本。如果您查看存储库,您可以判断它是否是空的。如果你有一个包含项目中文件的目录,以及一个 .git 目录,那是一个非裸存储库; .git 目录之外的那些文件是当前的工作副本。如果您只有存储库的内容,例如 refsobjects 等,那是一个裸存储库。它没有工作副本。

您只能推送到裸存储库(除非您覆盖默认配置)。您可以从非裸存储库中 pull ,然后 pull 入非裸存储库,但不能 push 其中。这是为了防止更新工作副本时出现问题。工作副本是您当前状态的副本;您可能已经编辑过文件,等等。当您 pull 入存储库时,最新的更改将 check out 到您的工作副本中;如果您有一些未 checkin 的本地更改,它们将与您从中提取的存储库中的更改 merge ,或者 checkout 被拒绝,您需要修复本地副本才能 checkout 那些新的变化。

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

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

关于git - git push 被拒绝时如何修复错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13535795/

相关文章:

GitLens 无法在 Visual Studio Code 中 pull 推送

Git: checkout 保留未提交更改的分支

git - 自定义 git diff 以忽略一些冲突

git - 如何处理 git 存储库的元存储库?

node.js - 在 Heroku 推送上设置 `process.env.commit`

java - 如何在给定 commit-sha 的情况下使用 JGit 读取 Git 注释

git 在 merge 冲突后立即显示 "No files need merging"

xcode - 为现有项目创建 git 存储库?

git - 当上游从不完整的 PR 中领先时,正确的处理方式是什么?

git - 允许 PR 构建验证构建而不发布工件