git push 失败:`拒绝更新签出的分支:refs/heads/master`

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8985782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:49:46  来源:igfitidea点击:

git push fails: `refusing to update checked out branch: refs/heads/master`

git

提问by andreas-h

I want to store my local modifications to JBoss config in git. For this, I have set up the following structure:

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

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

localis the git repository which shall be the "origin". The idea is that I want to be able to easily update my JBoss distribution once an update is available. I want to store all local modifications to the distributed JBoss package in git.

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

So, currently I do this:

所以,目前我这样做:

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

So far so good, all my local modifications are there where I want them.

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

However, once I have local modifications and want to bush them back to the localrepository, I get an error:

但是,一旦我进行了本地修改并希望将它们返回到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/'

What can I do about this? Any help is greatly appreciated!

我该怎么办?任何帮助是极大的赞赏!

EDIT

编辑

Here's what I did, which solved the problem:

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

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

Now, the branch currentin directory ~/localis always up-to-date, but it's not cheked out, so that i can push into it.

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

回答by Ilion

Pushing is meant for bare repos. For non-bare repos you should pull into them.

推送适用于裸仓库。对于非裸存储库,您应该拉入它们。

If you want to force this through anyway, you can do as the error message states and set receive.denyCurrentBranch to ignore. SSH to the location of the repo you are pushing to and run:

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

git config receive.denyCurrentBranch ignore

回答by vpatil

The master branch at remote site is checked out. If you have access to remote repository, checkout any other branch and then push from your repository.

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

回答by Hiery Nomus

Create the origin (local) repository as a bare repository (ie. git init --bare), or checkout a branch in there which is not master.

将原始(本地)存储库创建为裸存储库(即 git init --bare),或检出其中的非 master 分支。

回答by Student Hyman

OR,

或者,

when you initialize your remote project, using

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

git init --bare

回答by Simon H

I know this is quite an old question, but if you go with git init --bare, watch out for 'GIT_DIR=.' which will be set for a bare repo hook if using hooks to checkout the repo after pushing. Use 'export GIT_DIR=.git' in your hook routine to make it recognise the repo you are pulling into....

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

回答by Ian Jones

I managed to get round this by pulling from 'local' in 'current' rather than pushing to 'current' from 'local'.

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