Git推送错误:“[remote rejected] master -> master(分支当前被检出)”

1056

昨天我在stackoverflow上发布了一个问题,询问如何从一台计算机克隆Git仓库到另一台计算机,如何从另一台机器上执行'git clone'命令?

现在我成功地从源(192.168.1.2)克隆了一个Git仓库到目标(192.168.1.1)。

但是当我编辑一个文件后,执行git commit -a -m "test"git push时,在我的目标(192.168.1.1)上出现了这个错误:

git push                                                
hap@192.168.1.2's password: 
Counting objects: 21, done.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (11/11), 1010 bytes, done.
Total 11 (delta 9), reused 0 (delta 0)
error: refusing to update checked out branch: refs/heads/master
error: By default, updating the current branch in a non-bare repository
error: is denied, because it will make the index and work tree inconsistent
error: with what you pushed, and will require 'git reset --hard' to match
error: the work tree to HEAD.
error: 
error: You can set 'receive.denyCurrentBranch' configuration variable to
error: 'ignore' or 'warn' in the remote repository to allow pushing into
error: its current branch; however, this is not recommended unless you
error: arranged to update its work tree to match what you pushed in some
error: other way.
error: 
error: To squelch this message and still keep the default behaviour, set
error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To git+ssh://hap@192.168.1.2/media/LINUXDATA/working
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'git+ssh://hap@192.168.1.2/media/LINUXDATA/working'

我使用两个不同版本的Git(1.7在远程,1.5在本地机器上)。这可能是一个原因吗?


5
有没有老手能够更改接受答案为https://dev59.com/LXE85IYBdhLWcg3wXCMv#9283833并将帖子移到存档或其他地方?或者更改所有权或其他什么? - Jacek Krysztofik
13
现在,使用Git 2.3.0(2015年2月)和“git config receive.denyCurrentBranch=updateInstead”,您实际上已经拥有了一种安全地推送到非裸仓库的方法。 - VonC
这是@stigi提到的书的新链接: https://git-scm.com/book/en/v1/Git-on-the-Server - Abdelilah El Aissaoui
可能是一个重复问题,参考这篇文章:当向远程存储库推送更改时,什么是Git警告消息? - Trevor Boyd Smith
但我不明白为什么以及它是如何工作的?它能够运行,是的,但仅此而已。 - Tilak Madichetti
显示剩余2条评论
33个回答

-1

使用Git时,两个常规(非裸)存储库不能直接相互推送/拉取文件。必须有一个中介的裸存储库。显然,这有点像一对已婚夫妇有了孩子,而夫妻正在离婚。父母不会互相交谈,但他们会通过孩子进行沟通。

因此,您有一个存储库,将此存储库克隆到一个裸存储库中,然后将其克隆到第三个存储库中。第一个和第三个可以通过第二个存储库,即裸存储库,交换信息。我想这是有道理的,因为您不希望有人能够在未经您同意的情况下检查您的存储库,因为这可能会导致合并冲突等问题。

因此,这里有一个例子:

在PC上,在~/workspace中

git init
echo "line 1" > afile.txt
git add .
git commit -m ‘initial import’
git clone --bare . ../remote-repository.git
git remote add origin ../remote-repository.git
git push --set-upstream origin master

在笔记本电脑上,在~/workspace目录下(不要执行git init等操作)

git clone //LJZ-DELLPC/remote-repository.git/ .

// 然后进行各种提交,并将它们推送:

echo "line 2" > afile.txt
git add afile.txt
git commit -m 'added line 2'
git push    

然后回到电脑,在~/workspace目录下

git pull

// 然后进行各种提交并将它们推送:

git push

在笔记本电脑上执行 git pull 等操作。

以下是一个绝对具体的示例,全部在一台机器上完成,直接从命令窗口复制,以便我们知道没有遗漏任何步骤,确实可以工作等:

lylez@LJZ-DELLPC ~
$ cd gitdir
/home/lylez/gitdir

lylez@LJZ-DELLPC ~/gitdir
$ ls

lylez@LJZ-DELLPC ~/gitdir
$ mkdir repo1

lylez@LJZ-DELLPC ~/gitdir
$ cd repo1
/home/lylez/gitdir/repo1

lylez@LJZ-DELLPC ~/gitdir/repo1
$ git init
Initialized empty Git repository in /home/lylez/gitdir/repo1/.git/

lylez@LJZ-DELLPC ~/gitdir/repo1
$ echo "line 1" > afile.txt

lylez@LJZ-DELLPC ~/gitdir/repo1
$ git add afile.txt

lylez@LJZ-DELLPC ~/gitdir/repo1
$ git commit -m 'initial import'
[master (root-commit) f407e12] initial import
 1 file changed, 1 insertion(+)
 create mode 100644 afile.txt

lylez@LJZ-DELLPC ~/gitdir/repo1
$ git clone --bar . ../repo1-bare-clone
Cloning into bare repository '../repo1-bare-clone'...
done.

lylez@LJZ-DELLPC ~/gitdir/repo1
$ git remote add origin ../repo1-bare-clone

lylez@LJZ-DELLPC ~/gitdir/repo1
$ git push --set-upstream origin master
Branch master set up to track remote branch master from origin.
Everything up-to-date

lylez@LJZ-DELLPC ~/gitdir/repo1
$ cd ..

lylez@LJZ-DELLPC ~/gitdir
$ ls
repo1  repo1-bare-clone

lylez@LJZ-DELLPC ~/gitdir
$ mkdir repo1-remote

lylez@LJZ-DELLPC ~/gitdir
$ cd repo1-remote
/home/lylez/gitdir/repo1-remote

lylez@LJZ-DELLPC ~/gitdir/repo1-remote
$ git clone ../repo1-bare-clone .
Cloning into '.'...
done.

lylez@LJZ-DELLPC ~/gitdir/repo1-remote
$ ls
afile.txt

lylez@LJZ-DELLPC ~/gitdir/repo1-remote
$ cat afile.txt
line 1

lylez@LJZ-DELLPC ~/gitdir/repo1-remote
$ echo "line 2" >> afile.txt

lylez@LJZ-DELLPC ~/gitdir/repo1-remote
$ git add afile.txt

lylez@LJZ-DELLPC ~/gitdir/repo1-remote
$ git commit -m 'added line 2'
[master 5ad31e0] added line 2
 1 file changed, 1 insertion(+)

lylez@LJZ-DELLPC ~/gitdir/repo1-remote
$ git push
Counting objects: 3, done.
Writing objects: 100% (3/3), 260 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To /home/lylez/gitdir/repo1-remote/../repo1-bare-clone
   f407e12..5ad31e0  master -> master

lylez@LJZ-DELLPC ~/gitdir/repo1-remote
$ cd ../repo1

lylez@LJZ-DELLPC ~/gitdir/repo1
$ ls
afile.txt

lylez@LJZ-DELLPC ~/gitdir/repo1
$ cat afile.txt
line 1

lylez@LJZ-DELLPC ~/gitdir/repo1
$ git pull
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From ../repo1-bare-clone
   f407e12..5ad31e0  master     -> origin/master
Updating f407e12..5ad31e0
Fast-forward
 afile.txt | 1 +
 1 file changed, 1 insertion(+)

lylez@LJZ-DELLPC ~/gitdir/repo1
$ cat afile.txt
line 1
line 2

lylez@LJZ-DELLPC ~/gitdir/repo1
$ echo "line 3" >> afile.txt

lylez@LJZ-DELLPC ~/gitdir/repo1
$ git add afile.txt

lylez@LJZ-DELLPC ~/gitdir/repo1
$ git commit -m 'added line 3'
[master 3fa569e] added line 3
 1 file changed, 1 insertion(+)

lylez@LJZ-DELLPC ~/gitdir/repo1
$ git push
Counting objects: 3, done.
Writing objects: 100% (3/3), 265 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To ../repo1-bare-clone
   5ad31e0..3fa569e  master -> master

lylez@LJZ-DELLPC ~/gitdir/repo1
$ cd ../repo1-remote/

lylez@LJZ-DELLPC ~/gitdir/repo1-remote
$ ls
afile.txt

lylez@LJZ-DELLPC ~/gitdir/repo1-remote
$ cat afile.txt
line 1
line 2

lylez@LJZ-DELLPC ~/gitdir/repo1-remote
$ git pull
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From /home/lylez/gitdir/repo1-remote/../repo1-bare-clone
   5ad31e0..3fa569e  master     -> origin/master
Updating 5ad31e0..3fa569e
Fast-forward
 afile.txt | 1 +
 1 file changed, 1 insertion(+)

lylez@LJZ-DELLPC ~/gitdir/repo1-remote
$ cat afile.txt
line 1
line 2
line 3

lylez@LJZ-DELLPC ~/gitdir/repo1-remote
$ git --version
git version 2.1.1

lylez@LJZ-DELLPC ~/gitdir/repo1-remote

这是一个工作示例和原因说明。除了你的评论外,这里没有任何错误信息。 - Lyle Z
1
使用Git,两个普通(非裸)仓库不能直接相互推送/拉取文件。 - 除非它们可以。 - Andrew C
请提供一个具体的例子,而不是攻击。 - Lyle Z
你可以通过谷歌搜索吗?https://dev59.com/KnI-5IYBdhLWcg3wm5zN - Andrew C
没有人说在非裸露远程上推送到已检出的分支是一个好主意,尽管这是可能的。断章取义是不好的形式。 - Andrew C
显示剩余7条评论

-2

当我在我的NAS上克隆了一个repo,然后将该repo克隆到我的机器上时,我遇到了这个问题。

设置如下:

原始(github):

  • 克隆到我私人家庭网络中的网络存储(我的家庭网络)
    • 分支检出:DEVELOPMENT
  • 克隆到其他机器(笔记本电脑、办公室的小型数据中心服务器等)
    • 分支检出:DEVELOPMENT

当我尝试从笔记本电脑提交到NAS服务器时,会出现以下错误:

! [remote rejected]   development -> development (branch is currently checked out)

根本原因是在NAS服务器上检出了DEVELOPMENT分支。我的解决方案是在NAS存储库中切换到任何其他分支。这让我提交了我的更改。


-3

我的解决方案(正在使用)

  1. 在远程服务器上检出“master”分支
  2. 在本地工作于“dev”分支
  3. 将更改推送到远程的“dev”分支
  4. 将“dev”合并到远程的“master”分支

完成!


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