【问题标题】:Git Push failing: refusing to update checked out branch [duplicate]Git Push失败:拒绝更新签出的分支[重复]
【发布时间】:2012-07-08 20:55:38
【问题描述】:

可能重复:
git push error '[remote rejected] master -> master (branch is currently checked out)'

我正在尝试将我的存储库推送到“原点”,但是当我运行“git push”时出现此错误

Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 485 bytes, done.
Total 4 (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
Auto packing the repository for optimum performance.
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'.
/usr/local/git/libexec/git-core/git-sh-setup: Zeile 235: uname: Kommando nicht gefunden.
warning: There are too many unreachable loose objects; run 'git prune' to remove them.
To ssh://XXXX@XXXXXX.typo3server.info/html/typo3
 ! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'ssh://XXXXXXX@XXXXXX.typo3server.info/html/typo3'

怎么了?

【问题讨论】:

  • 邮件中有什么不清楚的地方?您正在推送到一个非裸仓库(另一端有一个结帐),并且没有特殊配置可以让另一端自动更新其工作副本。默认情况下,git 不会让你这样做。
  • 它没有工作树。这可能有助于sitaramc.github.com/concepts/bare.html
  • 但我的“来源”实际上是显示网站的网络服务器。使其成为一个裸仓库不会使实时服务器不可见?
  • 是的,所以你需要做一些特殊的配置来让另一端更新它的工作副本。注意它在谈论receive.denyCurrentBranch。我会发布答案,请稍等。

标签: git


【解决方案1】:

您正试图推送到一个非裸仓库(即一个仓库带有一个工作树,就像您的本地克隆一样),但在另一端没有做任何事情来解决这个问题特别是。

您看起来正试图让git push 部署您的网站。这样做的方法如下:

  1. 忽略 git 现在给你的错误消息,在你服务器上的 repo 中运行它:

    git config receive.denyCurrentBranch ignore
    

    这告诉 git “我会解决的,相信我。”

    现在,如果您推送,它将更新 repo,但索引和工作副本将保持不变。这意味着它总是看起来像您已经进行了更改以恢复您推送的所有内容。

    我们需要一种在推送发生时更新索引和工作副本的方法。听起来是时候……

  2. 设置post-receive 挂钩。在服务器的存储库中添加文件.git/hooks/post-receive

    #!/bin/bash
    
    # Drop the env var given to post-receive by default, as it'll mess up our
    # attempts to use git "normally."
    export -n GIT_DIR
    
    # Move back to the base of the working tree.
    cd ..
    
    # *Drop all changes* to index and working tree.
    git reset --hard
    

    请注意,这假设您只希望您的跟踪更改生效 - 您在实时站点上直接更改的任何内容都会在您下次推送时消失(未跟踪文件除外,但您也不应该拥有这些文件)。

我在末尾添加了一个git status,这样我就可以看到推送后的情况(因为它被传输回客户端)——这特别是我可以捕获未跟踪的文件并添加它们到.gitignore,或跟踪他们。

别忘了将post-receive 标记为可执行文件!


旁白:为什么不应该有未跟踪的文件?

  1. 回滚能力:这是用于部署实时站点。如果您希望能够真正回滚失败的部署,那么您需要将所有东西放在一起进行部署。

    这绝对包括核心 CMS。目前,它只是在服务器上,并且没有被跟踪,所以你没有希望检测到错误。

  2. 重新部署的能力:如果您的服务器硬盘出现故障,您可以解压缩核心 CMS,再次将您的 git 存储库分层,并希望它能正常工作。

  3. 能够注意到意外未跟踪的内容:如果您有几十个未跟踪的文件,并且它们都“注定”不被跟踪,那么新文件很容易潜入并迷失在噪音中。

    如果您的git status默认情况下是干净的,那么您会在弹出未跟踪的文件时立即注意到它们。

【讨论】:

  • 你为什么说我不应该有未跟踪的文件?我正在使用 CMS,但我没有跟踪 CMS 核心文件,因为它没用而且它们很大。
  • @Dbugger:我添加了一个旁白来处理这个问题。此外,这没什么大不了的。它们将被存储并打包在一个物体中并离开那里。
猜你喜欢
  • 2012-02-17
  • 2019-07-11
  • 2019-06-17
  • 1970-01-01
  • 1970-01-01
  • 2017-09-02
  • 1970-01-01
相关资源
最近更新 更多