Git错误non-fast-forward后的冲突解决

老虎爱测试 2014 年 01 月 12 日22:18:40实力推荐字数 1773阅读5分54秒阅读模式
摘要

当要push代码到git时,出现提示:

error:failed to push some refs to …

Git错误non-fast-forward后的冲突解决

Git错误non-fast-forward后的冲突解决

当要push代码到git时,出现提示:文章源自记得要微笑-https://chenxuehu.com/article/2014/01/1457.html

error:failed to push some refs to ...文章源自记得要微笑-https://chenxuehu.com/article/2014/01/1457.html

Dealing with “non-fast-forward” errors
From time to time you may encounter this error while pushing:文章源自记得要微笑-https://chenxuehu.com/article/2014/01/1457.html

  1. $ git push origin master
  2. To ../remote/
  3.  ! [rejected]        master -> master (non-fast forward)
  4. error: failed to push some refs to '../remote/'

To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again.  See the 'non-fast forward'
section of 'git push --help' for details.
This error can be a bit overwhelming at first, do not fear. Simply put, git cannot make the change on the remote without losing commits, so it refuses the push. Usually this is caused by another user pushing to the same branch. You can remedy this by fetching and merging the remote branch, or using pull to perform both at once.
In other cases this error is a result of destructive changes made locally by using commands like git commit --amend or git rebase. While you can override the remote by adding --force to the push command, you should only do so if you are absolutely certain this is what you want to do. Force-pushes can cause issues for other users that have fetched the remote branch, and is considered bad practice. When in doubt, don’t force-push.文章源自记得要微笑-https://chenxuehu.com/article/2014/01/1457.html

 文章源自记得要微笑-https://chenxuehu.com/article/2014/01/1457.html

Git错误non-fast-forward后的冲突解决文章源自记得要微笑-https://chenxuehu.com/article/2014/01/1457.html

问题(Non-fast-forward)的出现原因在于:git仓库中已经有一部分代码,所以它不允许你直接把你的代码覆盖上去。于是你有2个选择方式:文章源自记得要微笑-https://chenxuehu.com/article/2014/01/1457.html

1,强推,即利用强覆盖方式用你本地的代码替代git仓库内的内容文章源自记得要微笑-https://chenxuehu.com/article/2014/01/1457.html

git push -f文章源自记得要微笑-https://chenxuehu.com/article/2014/01/1457.html

2,先把git的东西fetch到你本地然后merge后再push文章源自记得要微笑-https://chenxuehu.com/article/2014/01/1457.html

$ git fetch文章源自记得要微笑-https://chenxuehu.com/article/2014/01/1457.html

$ git merge文章源自记得要微笑-https://chenxuehu.com/article/2014/01/1457.html

这2句命令等价于文章源自记得要微笑-https://chenxuehu.com/article/2014/01/1457.html

  1. $ git pull文章源自记得要微笑-https://chenxuehu.com/article/2014/01/1457.html

可是,这时候又出现了如下的问题:文章源自记得要微笑-https://chenxuehu.com/article/2014/01/1457.html

Git错误non-fast-forward后的冲突解决文章源自记得要微笑-https://chenxuehu.com/article/2014/01/1457.html

上面出现的 [branch "master"]是需要明确(.git/config)如下的内容文章源自记得要微笑-https://chenxuehu.com/article/2014/01/1457.html

[branch "master"]文章源自记得要微笑-https://chenxuehu.com/article/2014/01/1457.html

remote = origin文章源自记得要微笑-https://chenxuehu.com/article/2014/01/1457.html

merge = refs/heads/master文章源自记得要微笑-https://chenxuehu.com/article/2014/01/1457.html

这等于告诉git2件事:文章源自记得要微笑-https://chenxuehu.com/article/2014/01/1457.html

1,当你处于master branch, 默认的remote就是origin。文章源自记得要微笑-https://chenxuehu.com/article/2014/01/1457.html

2,当你在master branch上使用git pull时,没有指定remote和branch,那么git就会采用默认的remote(也就是origin)来merge在master branch上所有的改变文章源自记得要微笑-https://chenxuehu.com/article/2014/01/1457.html

如果不想或者不会编辑config文件的话,可以在bush上输入如下命令行:文章源自记得要微笑-https://chenxuehu.com/article/2014/01/1457.html

  1. $ git config branch.master.remote origin文章源自记得要微笑-https://chenxuehu.com/article/2014/01/1457.html

  2. $ git config branch.master.merge refs/heads/master文章源自记得要微笑-https://chenxuehu.com/article/2014/01/1457.html

之后再重新git pull下。最后git push你的代码吧。文章源自记得要微笑-https://chenxuehu.com/article/2014/01/1457.html

it works now~文章源自记得要微笑-https://chenxuehu.com/article/2014/01/1457.html

  • 版权声明:本文为原创文章,转载请附上原文出处链接及本声明。
  • 转载请注明:Git错误non-fast-forward后的冲突解决 | https://chenxuehu.com/article/2014/01/1457.html