Git Push失败:拒绝更新签出的分支

时间:2012-07-09 12:51:58

标签: git

  

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

我试图将我的存储库推送到“origin”,但是当我运行“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'

有什么问题?

1 个答案:

答案 0 :(得分:17)

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

您似乎试图让git push部署您的网站。以下是如何做到这一点:

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

    git config receive.denyCurrentBranch ignore
    

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

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

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

  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
    

    请注意,这假设您只希望实时跟踪更改 - 直接在实际网站上更改的内容将在您下次推送时消失(除非未跟踪的文件,但您不应该拥有这些更改)。

  3. 我在最后添加一个git status,这样我就可以看到推送后的土地是什么(因为它被传回客户端) - 这尤其是我可以捕获未跟踪的文件,或者将它们添加到.gitignore,或跟踪它们。

    不要忘记将post-receive标记为可执行文件!


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

    1. 能够回滚:这是用于部署实时网站。如果您希望能够真正回滚失败的部署,那么您需要一起进行部署的所有内容。

      这绝对包括核心CMS。现在,它只在服务器上,而且没有跟踪,所以你没有找到错误的希望。

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

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

      如果git status 默认情况下清理,则弹出后会立即发现意外未跟踪的文件。