推送到远程分支失败

时间:2014-05-16 18:51:39

标签: git

我在远程linux盒子上有一个git repo,在windows机器上有另一个repo。

我想:

  • 从linux机器上签出一个分支
  • 在本地工作(来自我的Windows机器)
  • 本地提交
  • 然后推送到远程分支。

这就是我的所作所为:

在Linux(远程)

  #currently on master branch
  git checkout -b remote_trunk
  #currently on remote_trunk branch

在Windows(本地)

  git fetch origin 
  git checkout remote_trunk
  #currently on remote_trunk branch
  # then I modify some files
  git commit -a -m"I modified some files"
  git push origin remote_trunk 

结果

Counting objects: 9, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 432 bytes | 0 bytes/s, done.
Total 5 (delta 4), reused 0 (delta 0)
remote: error: refusing to update checked out branch: refs/heads/remote_trunk
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
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'.
To Z:/shadow.git/ets/src/trunk/src
 ! [remote rejected] remote_trunk -> remote_trunk (branch is currently checked out)
error: failed to push some refs to 'Z:/shadow.git/ets/src/trunk/src'

2 个答案:

答案 0 :(得分:2)

默认情况下,git将禁止推送到非裸存储库。

当您推送到远程服务器时,您正在更新其分支引用,如果您在远程repo的工作目录中进行了更改,这可能会导致一些混淆。您可以通过以下方式避免这种混淆:

  • 最好的办法是建立一个单独的裸存储库,并从两个开发区域推送到共享存储库。选中git help init--bare选项。
  • 仅在两个repos之间执行git fetch / pull(并且没有git推送)
  • 您可以设置' receive.denyCurrentBranch'如警告所暗示的那样变量。这将允许推送,但不是推荐选项,除非您知道自己在做什么并且小心。
  • 只要远程Linux存储库当前没有签出remote_trunk,它就会发出警告,它会允许推送。所以在远程仓库上,你可以检查一个不同的分支(例如,在你的情况下git checkout master)或检查一个分离状态(git checkout --detached),同时你从本地仓库进行git推送,然后在推送之后,您可以在远程存储库上git checkout remote_trunk,而不会无声地破坏您的索引。

短篇小说:避免推送到非裸存储库 - 更喜欢git pull将更改合并到其中。如果要将git推送到公共区域,请使用git init --bare

设置裸存储库

答案 1 :(得分:1)

看起来你的远程存储库不是一个裸存储库,它需要。创建时,请使用:

git init --bare