git ![拒绝] master -> master(先获取)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28429819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:55:19  来源:igfitidea点击:

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

gitgithubgit-push

提问by Symfony

Is there a good way to explain how to resolve "! [rejected] master -> master (fetch first)'" in Git?

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

When I use this command $ git push origin masterit display an error message.

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

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

回答by linuxbandit

The answer is there, git is telling you to fetch first.

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

Probably somebody else has pushed to master already, and your commit is behind. Therefore you have to fetch, merge the changeset, and then you'll be able to push again.

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

If you don't (or even worse, if you force it by using the --forceoption), you can mess up the commit history.

如果您不这样做(或者更糟的是,如果您使用该--force选项强制执行),您可能会弄乱提交历史记录。

EDIT: I get into more detail about the last point, since a guy here just gave the Very Bad Advice of using the --forceoption.

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

As git is a DVCS, ideally many other developers are working on the same project as you, using the same repository (or a fork of it). If you overwrite forcefully with your changeset, your repository will mismatch other people's, because "you rewrote history". You will make other people unhappy and the repository will suffer. Probably a kitten in the world will cry, too.

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

TL;DR

TL; 博士

  1. If you want to solve, fetch first (and then merge).
  2. If you want to hack, use the --forceoption.
  1. 如果要解决,请先获取(然后合并)。
  2. 如果您想破解,请使用该--force选项。

You asked for the former, though. Go for 1) always, even if you will always use git by yourself, because it is a good practice.

不过,你要求的是前者。1) 总是,即使你总是自己使用 git,因为这是一个很好的做法。

回答by Aurelio A

try:

尝试:

git fetch origin master
git merge origin master

After to wrote this code I received other error: (non-fast-forward)

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

I write this code:

我写这个代码:

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

And resolved my problem

并解决了我的问题

回答by Unai Sainz de la Maza

You should use git pull, that′s command do a git fetchand next do the git merge.

你应该使用 git pull,那是命令 do agit fetch和 next do the git merge

If you use a git push origin master --forcecommand, you may have problems in the future.

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

回答by J D

pull is always the right approach but one exception could be when you are trying to convert a none-Git file system to a Github repository. There you would have to force the first commit in.

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

回答by J D

Try this git command

试试这个 git 命令

git push origin master --force

or short of force -f

或缺乏力量 -f

git push origin master -f

git push origin master -f

回答by Amila Weerasinghe

As it is stated in the Error message you have to "fetch first." This worked for me. Use the command:

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

  1. git fetch origin master
  1. git fetch origin master

Then follow these steps to merge:

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

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

回答by Tanuj Chakraborty

You can use the following command: First clone a fresh copy of your repo, using the --mirror flag:

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

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

Then follow the codes accordingly:

然后相应地遵循代码:

Adding an existing project to GitHub using the command line

使用命令行将现有项目添加到 GitHub

Even if that doesn't work, you can simply code:

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

$ git push origin master --force 

or

或者

$ git push origin master -f

回答by Chrispher

It's likely that someone else (e.g. your colleague) has put commits onto origin/masterthat aren't in your local masterbranch, and you are trying to push some commits from your local branch to the server. In 99% of cases, assuming you don't want to erase their work from origin, you have two options:

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

2) Merge their changes into your local branch, and then push the merged result. git checkout master git pull # resolve conflicts here git push

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

(Note that git pullis essentially just a git fetchand a git mergein this case.)

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

1) Rebase your local branch, so that it looks like your colleague made their commits first, and then you made your commits. This keeps the commit history nice and linear - and avoids a "merge commit". However, if you have conflicts with your colleague's changes, you may have to resolve those conflicts for each of your commits (rather than just once) in the worst case. Essentially this is nicer for everyone else but more effort for you. git pull --rebase # resolve conflicts here git push

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

(Note that git pull --rebaseis essentially a git fetchand a git rebase origin/master.)

(请注意,git pull --rebase本质上是 agit fetch和 a git rebase origin/master。)

回答by Jaros?aw Cichoń

Sometimes it happens when you duplicate files typically README sort of.

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

回答by Bhavdeep Kaur

Follow the steps given below as I also had the same problem:

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

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

(To see if local branch can be easily merged with remote one)

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

$ git push -u origin master 

(Now push entire content of local git repository to your online repository)

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