推送到服务器时出现 Git 错误
我在远程服务器上有一个文件夹,我们将其添加到 git 中。
git init
git add .
git commit -m "Initial commit"
然后我们将存储库拉到我本地的 MacBook 上。然后我添加一个文件index.js,添加到git,然后提交。当我尝试将修改后的存储库推送回服务器时,问题就来了。我遇到了一个疯狂的错误:
拒绝更新已签出的分支:refs/heads/master 默认情况下,更新非裸存储库中的当前分支会被拒绝,因为这会使索引和工作树与您推送的内容不一致,并且需要“git reset --hard”将工作树与 HEAD 匹配。
有什么想法吗?
I have a folder on a remote server which we added to git.
git init
git add .
git commit -m "Initial commit"
Then we pull the repository down to my local MacBook. Then I add a file index.js, add to git, and commit. The problem comes, when I try to PUSH the modified repo back to the server. I am getting a crazy error:
Refusing to update checked out branch: refs/heads/master
By default, updating the current branch in a non-bare repository is denied, because it will make the index and work tree inconsistent with what you pushed, and will require 'git reset --hard' to match the work tree to HEAD.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您计划推送到的远程存储库应使用
--bare
选项进行初始化,以防止 Git 创建工作目录。最简单的方法是初始化一个新的裸存储库,更新 MacBook 的遥控器以指向新位置(应该是以 .git 结尾的文件夹)并推送。A remote repository to which you plan to push should be initialized with the
--bare
option to prevent Git from creating a working directory. The easiest way to do this would be to init a new bare repo, update your MacBook's remote to point to the new location (should be a folder ending in .git) and push.发生这种情况是因为您尝试推送到非裸存储库(具有工作目录的存储库)。工作目录本质上是您“签出”并实时编辑文件的位置。当您推送到非裸存储库时,会发生错误消息中描述的情况。
如果您仍然想从 MacBook 和远程服务器编辑文件,一个简单的解决方案是使用 git init --bare 创建第三个存储库。您可以从其他两个存储库中推送和拉取此存储库。
This is occurs because you are trying to push to a non bare repo (a repo with a working directory). The working directory is essentially the spot where the file you "checkout" and edit live. When you push to the non bare repo, the situation described in your error message occurs.
A simple solution to this, if you still want to edit files from both your macbook and the remote server, is to create a third repo with
git init --bare
. You could than push and pull to this repo from your other two repos.