【问题标题】:Cannot push to a remote branch [duplicate]无法推送到远程分支[重复]
【发布时间】:2016-02-24 14:17:19
【问题描述】:

我阅读了有关该问题的几篇 SO 帖子,但我无法弄清楚我做错了什么。我尝试将我的提交推送到远程分支,但我得到error: failed to push some refs to 'k:/home/projects/tmp/rest-backend'

工作目录

我有一个名为rest-backend 的WD 和一个名为rest-backend-branch 的WD。第二个是使用git clone rest-backend rest-backend-branch 创建的。

k:\home\projects\tmp\rest-backend-branch>git remote -v
origin  k:/home/projects/tmp/rest-backend (fetch)
origin  k:/home/projects/tmp/rest-backend (push)

存储库日志

k:\home\projects\tmp\rest-backend>git log --pretty=oneline --abbrev-commit -4
8526101 Fix sort order
57bbc5b Add favicon
f7a2a24 New client
4f01419 Handle deep link

k:\home\projects\tmp\rest-backend-branch>git log --pretty=oneline --abbrev-commit -4
5294da8 New client
8526101 Fix sort order
57bbc5b Add favicon
f7a2a24 New client

rest-backend-branchorigin/master 早 1 次提交,如 git status 所示。

k:\home\projects\tmp\rest-backend-branch>git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working directory clean

将我的提交推送到远程

如果我尝试将我的提交推送到 origin/master,我会收到该错误。

k:\home\projects\tmp\rest-backend-branch>git push origin master
Counting objects: 29, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (11/11), 101.71 KiB | 0 bytes/s, done.
Total 11 (delta 3), reused 0 (delta 0)
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 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 k:/home/projects/tmp/rest-backend
 ! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'k:/home/projects/tmp/rest-backend'

我该如何解决这个问题?

【问题讨论】:

    标签: git


    【解决方案1】:

    要修复它,您只需按照错误消息中的说明进行操作即可。将 receive.denyCurrentBranch 设置为适合您的任何值 - ignorewarn

    但我们知道结果。

    您在这里所做的是克隆了非裸存储库,即该存储库有一个工作目录。

    如果此工作目录现在设置为 master 分支,并且您将更改推送到此分支 - 您的工作目录将与分支分离,您必须在那里运行 git reset --hard

    其他一些适合您的解决方案是:

    • 仅克隆裸存储库。 (或者至少,不要推送到非裸仓库)
    • 将远程存储库上的工作目录更改为另一个分支。

    【讨论】:

    • 我通过创建一个裸存储库来修复它。感谢您指出我不应该克隆非裸存储库。
    猜你喜欢
    • 1970-01-01
    • 2020-06-11
    • 2011-09-10
    • 2016-07-08
    • 2021-10-02
    • 2020-05-22
    • 1970-01-01
    • 2012-03-17
    • 2012-07-25
    相关资源
    最近更新 更多