git push失败:拒绝更新已检出的分支:refs/heads/master。

58

我希望将本地对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是将要成为“origin”的git仓库。我的想法是,一旦有更新可用,我希望能够轻松地更新我的JBoss分发版本。我想将所有对分发的JBoss软件包的本地修改存储在git中。

所以,目前我这样做:

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始终是最新的,但未签出,因此我无法将其推送。

我刚从Bitbucket带一个repo到Linux虚拟服务器后遇到了这个问题。在远程repo中执行git init --bare解决了我的问题。 - Droppy
https://dev59.com/LXE85IYBdhLWcg3wXCMv#3251126 对我有用(即在远程仓库上运行 git config --bool core.bare true)。 - ergohack
7个回答

68

推送是针对裸仓库的。对于非裸仓库,你应该将内容拉取至其中。

如果你仍然想强制进行推送,可以按照错误提示所述设置receive.denyCurrentBranch为ignore。SSH到要推送到的仓库位置,并运行:

git config receive.denyCurrentBranch ignore

1
我认为这比检出不同的分支更好。 - jjmontes
1
我正在使用sourcetree。我应该在哪里进行更改? - SearchForKnowledge

60
远程网站的主分支已经被检出。如果您可以访问远程存储库,请检出其他任何分支,然后从您的存储库推送。
注:保留HTML标记

1
谢谢,这指引了我正确的方向。我更新了我的帖子,提供了完整的解决方案。 - andreas-h
1
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - shashwat
@vpatil。我也有同样的问题。我想要能够推送到一个新分支。 - SearchForKnowledge

15

将本地仓库作为一个裸仓库(即 git init --bare),或者在其中检出非主分支。


14

或者,

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

git init --bare

1
我知道这是一个很老的问题,但如果你使用git init --bare命令,请注意在使用钩子将存储库检出到推送后,'GIT_DIR=.'将被设置为裸存储库钩子。在您的钩子例程中使用“export GIT_DIR=.git”来使其识别您正在拉取的存储库。请注意保留HTML标签。

1
我通过从"current"中的"local"拉取而不是从"local"推送到"current"来解决了这个问题。

0

我的孤立情况

与其先推送到源,我更倾向于将分支拉到同一网络上的其他计算机上。

  • 计算机A)仓库的来源
  • 计算机B)从计算机A克隆的仓库

错误

% git push local feat/async-updates
Enumerating objects: 25, done.
Counting objects: 100% (25/25), done.
Delta compression using up to 8 threads
Compressing objects: 100% (13/13), done.
Writing objects: 100% (13/13), 1.34 KiB | 1.34 MiB/s, done.
Total 13 (delta 12), reused 0 (delta 0), pack-reused 0
remote: Checking connectivity: 13, done.
remote: error: refusing to update checked out branch: refs/heads/feat/async-updates
remote: error: By default, updating the current branch in a non-bare repository
remote: is denied, because it will make the index and work tree inconsistent
remote: with what you pushed, and will require 'git reset --hard' to match
remote: the work tree to HEAD.
remote: 
remote: You can set the 'receive.denyCurrentBranch' configuration variable
remote: to 'ignore' or 'warn' in the remote repository to allow pushing into
remote: its current branch; however, this is not recommended unless you
remote: arranged to update its work tree to match what you pushed in some
remote: other way.
remote: 
remote: To squelch this message and still keep the default behaviour, set
remote: 'receive.denyCurrentBranch' configuration variable to 'refuse'.

最终问题源于在计算机A(原始仓库源)上检出了相同的分支。我在那里切换到“dev”,问题迎刃而解。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接