Git 推送失败 : refusing to update checked out branch

标签 git

<分区>

Possible Duplicate:
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 push 部署您的网站。以下是如何做到这一点:

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

    git config receive.denyCurrentBranch ignore
    

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

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

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

  2. 设置一个post-receive Hook 。在服务器的存储库中添加文件.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 默认情况下是干净的,那么您会在 pop 时立即注意到意外的未跟踪文件。

关于Git 推送失败 : refusing to update checked out branch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11395398/

相关文章:

git - 保持 git 功能分支最新的更简单方法

ruby-on-rails - 带有鸣鸟的销售/子模块 gem

git - 如何忽略所有文件,但不忽略子文件夹

Git Tower 无法重新应用存储 : Conflicts in index

eclipse - 如何让eclipse记住ssh key 密码?

eclipse - Git、SVN 和 Eclipse 工作流程

windows - 无法在 git bash 中运行 'git' ... "bash:/mingw32/bin/git: Bad address"

git push 被拒绝 : error: failed to push some refs

python - 如何组织GAE Modules app结构和代码?

git - 在交互式 rebase 期间如何执行 git add --patch?