【问题标题】:git push fails: `refusing to update checked out branch: refs/heads/master`git push 失败:`拒绝更新签出的分支:refs/heads/master`
【发布时间】:2012-02-17 15:18:16
【问题描述】:

我想将我对 JBoss 配置的本地修改存储在 git 中。为此,我设置了以下结构:

lrwxrwxrwx  1 jboss jboss        19 Jan 24 11:53 current -> jboss-as-7.1.0.CR1b
drwxr-xr-x 11 jboss jboss      4096 Jan 24 12:13 jboss-as-7.1.0.CR1b
-rw-r--r--  1 jboss jboss 108211143 Jan 23 16:02 jboss-as-7.1.0.CR1b.tar.gz
drwxr-xr-x  6 jboss jboss      4096 Jan 24 11:36 local

local 是 git 存储库,应该是“源”。我的想法是,一旦有更新可用,我希望能够轻松地更新我的 JBoss 发行版。我想在 git 中存储所有对分布式 JBoss 包的本地修改。

所以,目前我这样做:

jboss@tpl0:~/jboss-as-7.1.0.CR1b$ git init
Initialized empty Git repository in /opt/jboss/jboss-as-7.1.0.CR1b/.git/
jboss@tpl0:~/jboss-as-7.1.0.CR1b$ git remote add origin ../local/   
jboss@tpl0:~/jboss-as-7.1.0.CR1b$ git pull origin master 
From ../local
 * branch            master     -> FETCH_HEAD

到目前为止一切顺利,我所有的本地修改都在我想要的地方。

但是,一旦我进行了本地修改并想要将它们返回到 local 存储库,我就会收到错误消息:

jboss@tpl0:~/jboss-as-7.1.0.CR1b$ vim standalone/configuration/standalone.xml   
jboss@tpl0:~/jboss-as-7.1.0.CR1b$ git commit -a -m "renamed database to project2_core,   to distinguish from other projects"
[master 3e54f34] renamed database to project2_core, to distinguish from other projects
Committer: jboss <jboss@tpl0.(none)>
 1 files changed, 1 insertions(+), 1 deletions(-)

jboss@tpl0:~/jboss-as-7.1.0.CR1b$ git push origin master 
Counting objects: 9, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 447 bytes, done.
Total 5 (delta 3), reused 0 (delta 0)
Unpacking objects: 100% (5/5), done.
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 t
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing int
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 som
remote: error: other way.
remote: error: 
remote: error: To squelch this message and still keep the default behaviour, se
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To ../local/
 ! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to '../local/'

对此我能做些什么?非常感谢任何帮助!

编辑

这就是我所做的,它解决了问题:

cd ~/current
git init
vim .gitignore                   # set up a basic .gitignore file
git add .gitignore
git commit -a -m "initial commit"
cd ~/local
git clone ~/current
git branch -m master current     # rename master branch to 'current'
git branch repo
git checkout repo

现在,~/local 目录中的分支 current 始终是最新的,但它没有被签出,所以我可以推入它。

【问题讨论】:

标签: git


【解决方案1】:

我知道这是一个很老的问题,但如果您使用 git init --bare,请注意“GIT_DIR=”。如果在推送后使用钩子签出仓库,它将被设置为一个裸仓库钩子。在你的钩子例程中使用'export GIT_DIR=.git',让它识别你正在拉入的回购......

【讨论】:

    【解决方案2】:

    或,

    当你初始化你的远程项目时,使用

    git init --bare
    

    【讨论】:

    • 我会这样做的。不知何故,当我创建远程仓库时,我忘记了这是这样做的方法。
    【解决方案3】:

    我设法通过从“当前”中的“本地”拉取而不是从“本地”推送到“当前”来解决这个问题。

    【讨论】:

      【解决方案4】:

      远程站点的主分支已签出。如果您有权访问远程存储库,请签出任何其他分支,然后从您的存储库推送。

      【讨论】:

      • 谢谢,这为我指明了正确的方向。更新了我的帖子以提供完整的解决方案。
      • 但是你能给出一些命令吗..?如果我只有一个分支怎么办..?远程存储库是裸露的。
      • @vpatil。同样的问题在这里。我希望能够推送到一个新的分支
      【解决方案5】:

      推送是针对裸仓库的。对于非裸仓库,您应该拉入它们。

      如果您想强制执行此操作,您可以按照错误消息的说明进行操作,并将 receive.denyCurrentBranch 设置为忽略。 SSH 到您要推送到的存储库的位置并运行:

      git config receive.denyCurrentBranch ignore
      

      【讨论】:

      • 我认为这比检查不同的分支更好。
      • 我正在使用 sourcetree。我在哪里进行更改?
      【解决方案6】:

      将原始(本地)存储库创建为裸存储库(即 git init --bare),或在其中签出不是主存储库的分支。

      【讨论】:

        猜你喜欢
        • 2012-07-08
        • 1970-01-01
        • 2019-10-26
        • 2019-07-11
        • 1970-01-01
        • 2017-05-10
        • 1970-01-01
        相关资源
        最近更新 更多