! [rejected] master -> master(先获取)

! [rejected] master -> master (fetch first)

有没有好的方法来解释如何解析 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告诉你先去取。

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

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

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

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

TL;DR

  1. 如果要解决,先fetch(再merge)
  2. 如果您想破解,请使用 --force 选项。

不过你问的是前者。始终执行 1),即使您总是自己使用 git,因为这是一种很好的做法。

试试这个 git 命令

git push origin master --force

或缺力-f

git push origin master -f

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

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

pull 始终是正确的方法,但当您尝试将 none-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

尝试:

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

并解决了我的问题

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

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

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

1) 重新设置本地分支的基址,这样看起来您的同事先提交了他们的提交,然后您才提交了您的提交。这使提交历史保持良好和线性 - 并避免 "merge commit"。但是,如果您与同事的更改有冲突,在最坏的情况下,您可能必须为每次提交(而不是一次)解决这些冲突。从本质上讲,这对其他人来说更好,但对你来说更努力。 git pull --rebase # resolve conflicts here git push

(注意 git pull --rebase 本质上是一个 git fetch 和一个 git rebase origin/master。)

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

您可以使用以下命令: 首先使用 --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

您的错误可能是因为合并分支。
请按照以下步骤操作:

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

这对我有用:

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

如错误消息中所述,您必须 "fetch first." 这对我有用。 使用命令:

  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

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

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

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

$ git push -u origin master 

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

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

# git checkout -b newbranch <SHA of master>

# git branch
* newbranch
  master

# git push -u <repo_url_alias> newbranch

您还剩下 2 个分支:Master 和 newbranch,稍后您可以设法合并它们。

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

git fetch origin
git merge origin/master

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

对其进行了初始化

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

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

这对我有用,因为 none 的其他解决方案对我有用。 甚至不强制!

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

刚刚经历了GitBash

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

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

最快的解决方案-

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

I had faced this error while pressing the git push command.

  • git push
  • 后加上-force即可解决问题
  • 解决方法: git push -fource

当我们尝试使用下面提到的命令将文件夹推送到 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

已附上一张图片以供直观说明。

当你的队友已经做出承诺时,就会发生这种情况。所以他的承诺是最重要的。 为了避免你做变基(合并,获取)。

我通过以下方式解决了我的问题。

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

您可以看到日志 git log

! [rejected] master -> master(先获取)

如果您首先检查文件夹位置是否正确,此错误会显示时间。然后输入:-

git push origin master --force

当您在 GitHub 和 select README.gitignorelicense 文件中创建存储库时会发生这种情况。 如果 repasotory 为空,则删除它并在没有 select ing README.gitignorelicense 文件的情况下重新创建它。