【问题标题】:Pushing to a remote empty repository on bitbucket推送到 bitbucket 上的远程空存储库
【发布时间】:2019-12-11 15:44:24
【问题描述】:

在将本地 git 存储库推送到清空远程存储库时出现以下错误。这是一个私人仓库。

$ git push origin master 致https://bitbucket.org/lord_of_codes/uat_api.git ! [拒绝] master -> master(非快进) 错误:未能将一些参考推送到“https://lord_of_codes@bitbucket.org/lord_of_codes/uat_api.git” 提示:更新被拒绝,因为您当前分支的尖端落后 提示:它的远程对应物。集成远程更改(例如 提示:'git pull ...') 在再次推动之前。 提示:有关详细信息,请参阅“git push --help”中的“关于快进的说明”。

我刚刚在 bit bucket 上创建了一个空的 git repo。
我在本地项目中做了git init
我做了git add .
我首先承诺git commit -m "Initial Commit"
我做了git config --global user.namegit config --global user.email
我做了git remote add origin url 我做了git push origin master
然后我得到了那个错误。
我也试过在提交前推送代码,也试过git pull。但它不起作用

【问题讨论】:

标签: git bitbucket


【解决方案1】:

我也有类似的问题:

(! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@it-bucket:some-project/some-repo.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.)

当我将本地存储库推送到远程存储库时。

问题:我在 git-bucket 上创建了新的存储库,我使用 README 文件进行了初始化,该文件实际上与我的本地存储库不匹配。

解决方案:我删除现有存储库或创建一个新存储库,使用 README 文件进行初始化,然后添加以下命令

git remote add origin "repository URL"

然后

git push -u origin master

【讨论】:

    【解决方案2】:

    您可以强制推送到远程分支,这将解决问题。

    git push -u origin master # without --force
    

    如果这不起作用,因为它是您创建的空存储库,您可以这样做

    git push -u -f origin master # with force
    

    【讨论】:

    • 如果remote中的数据很重要怎么办?您应该提及--force 的后果。
    • 如果遥控器很重要,总有办法解决此类问题。根据情况通过拉取冲突解决方案或允许沿途不相关的历史记录,但在这种情况下,遥控器是空的。这很好,但 --force 会删除历史记录。
    • 远程不为空!我想你已经知道了。这可能是一个新的 git 用户,他/她可能不知道 git 是如何工作的。考虑一个由 OP 以外的人使用的 repo,如果 OP 只是复制粘贴您的答案,那么团队之前的提交历史将会丢失。
    • 我知道远程是空的。用户创建了 repo,这是一个新的。
    【解决方案3】:

    这是一个非快进错误。 Github 有一小部分对此进行了解释,请查看 here

    基本上,git 所做的是阻止您写入与您的头部不同的 repo。这意味着您的存储库 (https://bitbucket.org/lord_of_codes/uat_api.git) 的提交与您的 PC/本地代码中的提交不同,并且您正尝试添加更多提交/更改到与您不同的代码库。

    根据 GitHub,您可以通过获取并合并在远程分支上所做的更改与您在本地所做的更改来解决此问题:

        $ git fetch origin
        # Fetches updates made to an online repository
        $ git merge origin YOUR_BRANCH_NAME
        # Merges updates made online with your local work
    

    或者,您可以简单地使用 git pull 一次执行两个命令:

        $ git pull origin YOUR_BRANCH_NAME
        # Grabs online updates and merges them with your local work
    

    在其他情况下,此错误是由于使用 git commit --amendgit rebase 等命令在本地进行的破坏性更改造成的。
    虽然您可以通过将--force 添加到push 命令来覆盖遥控器,但只有在您绝对确定这是您想要做的事情时才应该这样做。
    Source

    更多关于 git 的理解可以参考这个神奇的“A Visual Git Guide

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-03
      • 1970-01-01
      • 2013-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-18
      相关资源
      最近更新 更多