【Git】(fetch first)和(non-fast-forward)问题解决

简介: 【Git】(fetch first)和(non-fast-forward)问题解决

1.问题

当在本地main分支上向远程main仓库push时发生如下问题

To github.com:ReturnTmp/study.git
! [rejected] main -> main (fetch first)
error: failed to push some refs to 'github.com:ReturnTmp/study.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.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

解决方案:

git push origin main

但是之后又报下面的错:

To github.com:ReturnTmp/study.git
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to 'github.com:ReturnTmp/study.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

解决方案:

1.先合并之前的历史,再进行提交——提倡使用

(1)先把git的东西fetch到你本地然后merge后再push

$ git fetch origin master
$ git merge origin FETCH_HEAD

先抓取远程仓库的更新到本地,然后与你的本地仓库合并,(如果有冲突就要解决冲突后再合并,冲突问题比较复杂,这里就不详细说了),这样就可以使远程仓库和你本地仓库一致了,然后就可以提交修改了。

(2)这2句命令等价于

$ git pull origin master

但是使用git fetch + git merge 更加安全。

(3)git pull --rebase origin master

重定基,可以是历史更加统一,即使提交历史趋向于一条直线。

补充:他们之间的关系

git pull = git fetch + git merge FETCH_HEAD

git pull --rebase = git fetch + git rebase FETCH_HEAD

2.丢弃之前的历史,强推-不推荐

git push -f origin main

官方文档提示:This flag disables these checks, and can cause the remote repository to lose commits; use it with care.(即:此标志禁用这些检查,并可能导致远程存储库丢失提交;小心使用。)

推荐阅读Dealing with non-fast-forward errors - GitHub Docs

参考博客:Git错误non-fast-forward的解决方法_秦时明月之君临天下的博客-CSDN博客_git non-fast-
forward

Git错误non-fast-forward的解决方法_秦时明月之君临天下的博客-CSDN博客_git non-fast-
forward

目录
相关文章
|
6月前
|
开发工具 git 开发者
Git Pull vs. Git Fetch:深度解析
【2月更文挑战第29天】
1691 0
Git Pull vs. Git Fetch:深度解析
|
开发工具 git
成功解决git rebase问题:First, rewinding head to replay your work on top of it...
成功解决git rebase问题:First, rewinding head to replay your work on top of it...
|
2月前
|
开发工具 git
深入理解Git中的git pull和git fetch命令
深入理解Git中的git pull和git fetch命令
93 0
|
3月前
|
开发工具 git 开发者
Git Fetch 和 Git Pull:理解两者的区别
【8月更文挑战第27天】
333 1
|
6月前
|
开发工具 git
百度搜索:蓝易云【git常用命令之Fetch】
总结:`git fetch`命令用于从远程仓库获取最新的提交历史和分支信息,但不会自动合并或修改本地代码。它是在进行Git协作开发时常用的命令之一,帮助你保持和远程仓库的同步,以便后续操作。
536 0
|
Ubuntu 开发工具 git
apt install git:Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
Failed to fetch http://security.ubuntu.com/ubuntu/pool/main/g/git/git-man_2.17.1-1ubuntu0.11_all.deb 404 Not Found [IP: 91.189.91.39 80]
350 0
apt install git:Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
|
存储 开发工具 git
git stash , git fetch 和 git clear
git stash , git fetch 和 git clear
117 0
|
开发工具 git
Git切换分支时报错:you need to resolve your current index first
Git切换分支时报错:you need to resolve your current index first
774 0
Git切换分支时报错:you need to resolve your current index first
|
jenkins 持续交付 网络安全
Jenkins+GitHub报错hudson.plugins.git.GitException: Failed to fetch from GitHub 443
Jenkins+GitHub报错hudson.plugins.git.GitException: Failed to fetch from GitHub 443
Jenkins+GitHub报错hudson.plugins.git.GitException: Failed to fetch from GitHub 443
|
安全 Linux 开发工具
Git 技术篇 - 同步代码到github失败,提示non-fast-forward、error: failed to push some refs to问题解决方法,git pull的用法
Git 技术篇 - 同步代码到github失败,提示non-fast-forward、error: failed to push some refs to问题解决方法,git pull的用法
736 0
Git 技术篇 - 同步代码到github失败,提示non-fast-forward、error: failed to push some refs to问题解决方法,git pull的用法