【问题标题】:! [rejected] master -> master (fetch first)! [rejected] master -> master(先获取)
【发布时间】:2015-04-10 09:01:25
【问题描述】:

有没有很好的方法来解释如何在 Git 中解析“! [rejected] master -> master (fetch first)'”?

当我使用此命令$ git push origin master 时,它会显示一条错误消息。

! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@github.com:zapnaa/abcappp.git'

【问题讨论】:

  • git push origin master --force
  • 它对我有用。我得到了错误,但回购是空的。没有什么可以先拉...

标签: git github git-push


【解决方案1】:

答案就在那里,git告诉你先获取。

可能其他人已经推送到 master 了,而你的提交落后了。因此,您必须获取、合并变更集,然后才能再次推送。

如果你不这样做(或者更糟糕的是,如果你使用 --force 选项强制它),你可能会弄乱提交历史。

编辑:我更详细地了解最后一点,因为这里的一个人刚刚给出了使用 --force 选项的非常糟糕的建议。

由于 git 是 DVCS,理想情况下,许多其他开发人员正在与您在同一个项目上工作,使用同一个存储库(或它的一个分支)。如果你用你的变更集强行覆盖,你的存储库将与其他人的不匹配,因为“你重写了历史”。你会让其他人不开心,存储库也会受到影响。大概世界上一只小猫也会哭吧。

TL;DR

  1. 如果要解决,先获取(然后合并)。
  2. 如果您想破解,请使用--force 选项。

不过,您要求的是前者。始终坚持 1),即使您将始终自己使用 git,因为这是一种很好的做法。

【讨论】:

  • 无法获取删除本地文件中的重要更改?
  • 抓取后不会改变
  • @dhein 正如我所写,提取之后必须进行合并-关键是您必须将本地树与远程树“对齐”(因此要合并)-但是谢谢,我在 TL 中写过;DR 也是
  • 我不明白,但没关系,谢谢
【解决方案2】:

试试这个 git 命令

git push origin master --force

或力不足-f

git push origin master -f

【讨论】:

  • 这会覆盖 git push 限制。不推荐团队合作。来自 git push 文档:如果在你变基时其他人建立在你的原始历史之上,远程分支的尖端可能会随着她的提交而前进,并且用 --force 盲目推送会 失去她的工作。
【解决方案3】:

您应该使用git pull,即执行git fetch 的命令,然后执行git merge

如果你使用git push origin master --force命令,你以后可能会遇到问题。

【讨论】:

  • 仅当您是项目中唯一的人并且您在尝试进行第一次推送时感到沮丧时才应该使用 --force 是否正确?
【解决方案4】:

pull 始终是正确的方法,但当您尝试将非 Git 文件系统转换为 Github 存储库时可能会有一个例外。在那里你必须强制第一次提交。

git init
git add README.md
git add .
git commit -m "first commit"
git remote add origin https://github.com/userName/repoName.git
git push --force origin master

【讨论】:

  • 对我有用,我又开始了一个新项目(相同的仓库),我想替换它。
  • 哇!谢谢!几个月来我一直在寻找这个答案!!!
【解决方案5】:

试试:

git fetch origin master
git merge origin master

写完这段代码后,我收到了其他错误:(非快进)

我写了这段代码:

git fetch origin master:tmp
git rebase tmp
git push origin HEAD:master
git branch -D tmp

解决了我的问题

【讨论】:

  • 对我来说也一样。这解决了我的问题。有几个警告。我搞砸了一个子存储库,但解决了这个问题:stackoverflow.com/questions/19584255/…
  • @Aurelio A 你的合并命令不正确,应该是git merge master
  • 为什么我们需要使用 git branch -D tmp ?
【解决方案6】:

很可能其他人(例如您的同事)已将不在您本地 master 分支中的提交提交到 origin/master,并且您正试图将一些提交从本地分支推送到服务器。在 99% 的情况下,假设您不想从 origin 删除他们的工作,您有两种选择:

2) 将他们的更改合并到您的本地分支中,然后推送合并的结果。 git checkout master git pull # resolve conflicts here git push

(请注意,在这种情况下,git pull 本质上只是一个git fetch 和一个git merge。)

1) 重新定位您的本地分支,这样看起来您的同事首先提交了他们的提交,然后您提交了您的提交。这使提交历史保持良好和线性 - 并避免“合并提交”。但是,如果您与同事的更改发生冲突,那么在最坏的情况下,您可能必须为每个提交(而不是一次)解决这些冲突。本质上,这对其他人来说更好,但对你来说更努力。 git pull --rebase # resolve conflicts here git push

(注意git pull --rebase本质上是git fetchgit rebase origin/master。)

【讨论】:

    【解决方案7】:

    有时当你复制文件时会发生这种情况,通常是自述文件之类的。

    【讨论】:

      【解决方案8】:

      您可以使用以下命令: 首先使用 --mirror 标志克隆你的 repo 的新副本:

      $ git clone --mirror git://example.com/some-big-repo.git
      

      然后相应地遵循代码:

      Adding an existing project to GitHub using the command line

      即使这不起作用,您也可以简单地编写代码:

      $ git push origin master --force 
      

      $ git push origin master -f
      

      【讨论】:

        【解决方案9】:

        您的错误可能是由于合并分支造成的。
        请按照以下说明操作:

        第 1 步:git pull origin master(如果您收到任何消息,请忽略它)
        第 2 步:git add .
        第 3 步:git commit -m 'your commit message'
        第 4 步:git push origin master

        【讨论】:

          【解决方案10】:

          这对我有用:

          $ git add .
          $ git commit -m "commit"
          $ git push origin master --force
          

          【讨论】:

            【解决方案11】:

            正如错误消息中所述,您必须“首先获取”。这对我有用。 使用命令:

            1. git fetch origin master

            然后按照以下步骤进行合并:

            1. git pull origin master
            2. git add .
            3. git commit -m 'your commit message'
            4. git push origin master

            【讨论】:

              【解决方案12】:

              按照下面给出的步骤,因为我也遇到了同样的问题:

              $ git pull origin master --allow-unrelated-histories 
              

              (查看本地分支是否可以轻松与远程分支合并)

              $ git push -u origin master 
              

              (现在将本地 git 存储库的全部内容推送到您的在线存储库)

              【讨论】:

                【解决方案13】:

                我通过像这样签出一个新分支来克服这个问题:

                # git checkout -b newbranch <SHA of master>
                
                # git branch
                * newbranch
                  master
                
                # git push -u <repo_url_alias> newbranch
                

                您剩下 2 个分支:Master 和 newbranch,您可以稍后合并。

                【讨论】:

                  【解决方案14】:

                  您只需提及您的分支名称和远程名称即可。

                  git fetch origin
                  git merge origin/master
                  

                  【讨论】:

                    【解决方案15】:

                    在我的案例中发生这种情况的原因是在创建 GitHub 代表链接时,我使用 README 文件

                    对其进行了初始化

                    创建 Git 远程时不要使用 README 文件对其进行初始化,否则会显示错误

                    不要那样做,它肯定能正常工作 如果您希望在推送到主分支后使用自述文件初始化它

                    【讨论】:

                      【解决方案16】:

                      这对我有用,因为其他解决方案都不适合我。 甚至不强迫!

                      https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/resolving-a-merge-conflict-using-the-command-line

                      只需要通过 Git Bash

                      cd REPOSITORY-NAME
                      git add .
                      git commit -m "Resolved merge conflict by incorporating both suggestions."
                      

                      然后回到我的 cmd,我可以:git push heroku master 在我的情况下是问题所在。

                      【讨论】:

                        【解决方案17】:

                        最快的解决方案-

                        1. 做一个 git 日志-> 您会看到有人可能在您提取最新代码库时推送了一些代码。
                        2. 执行 git pull --rebase,这将首先倒回 head 以在其之上重播您的工作,然后将您提交的更改应用到相同的位置。
                        3. 您现在已准备好进行 git push。

                        【讨论】:

                          【解决方案18】:

                          下面的命令对我有用
                          `it push --force origin master

                          【讨论】:

                          • 不是 git 命令。
                          【解决方案19】:

                          我在按下 git push 命令时遇到了这个错误。

                          • git push 后简单添加 -force 即可解决问题
                          • 解决办法: git push -fource

                          【讨论】:

                            【解决方案20】:

                            当我们尝试使用下面提到的命令在 Github 上推送文件夹时

                              $ git push origin master
                            

                            并得到如下错误:

                            To https://github.com/Raushan1156/QR-Code.git
                             ! [rejected]        master -> master (fetch first)
                            error: failed to push some refs to 'https://github.com/Raushan1156/QR-Code.git'
                            hint: Updates were rejected because the remote contains work that you do
                            hint: not have locally. This is usually caused by another repository pushing
                            hint: to the same ref. You may want to first integrate the remote changes
                            hint: (e.g., 'git pull ...') before pushing again.
                            

                            试试这个命令来解决你的问题,它解决了我的错误。

                            $ git push origin master --force
                            

                            已附上一张图片以进行视觉解释。

                            【讨论】:

                              【解决方案21】:

                              当你的队友已经提交时,就会发生这种情况。所以他的承诺是最重要的。 为了避免你做 rebase (merge , fetch)。

                              我用以下方法解决了我的问题。

                              1. git pull --rebase origin master
                              2. git push -u origin master

                              你可以看到日志git log

                              【讨论】:

                                猜你喜欢
                                • 2021-11-23
                                • 2015-02-28
                                • 2020-12-03
                                • 1970-01-01
                                • 2015-10-20
                                • 2021-11-11
                                • 1970-01-01
                                • 1970-01-01
                                • 2019-07-29
                                相关资源
                                最近更新 更多