为什么git变基之后需要git push -f才能提交上去不报错

如题,dev,master两个分支,将dev变基到master上,然后dev再去add,commit,push,会报如下错
`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.`

使用git push -f则会成功提交,且提交线变成一条

阅读 4.9k
2 个回答

git-push

--force

  • Usually, the command refuses to update a remote ref that is not an ancestor of the local ref used to overwrite it. Also, when --force-with-lease option is used, the command refuses to update a remote ref whose current value does not match what is expected.
  • This flag disables these checks, and can cause the remote repository to lose commits; use it with care.

当远程分支的提交不是本地分支的提交的祖先的时候,git-push 会失败。

变基(git-rebase)会改变提交的之间的祖先关系。从而造成上述情况。

--force 会强制提交,会导致远程仓库丢失提交。

比如:


A -- B -- D (master; origin/master)
     `-- E (dev; origin/dev)

# 在 dev 分支     
git rebase master

A -- B -- D (master; origin/master)
     |    `-- E' (dev)
     `-- E (origin/dev)

这时,远程的 origin/dev 的提交是 E ,本地在 E' ,E 并不是 E' 的祖先,于是提交失败。

因为git只有一个主分支。 也就是第一分支(我是这么叫的),然后我们可以在主分支上新建各个分支(我就叫它二级分支),这些二级分支是可以直接push到master分支的。 但是呢,如果你在二级分支上新建分支(三级分支),那么git文件的master主分支是不关联这些分支的。也就是说,每一个分支都只关联直系分支。其他分支要想推到自己分支的时候,git直接拒绝了。

所以。 我猜测你的dev分支并不只在master上新建的。如果想不用-f强制推送。你可以手动关联这俩个分支。 命令:git remote


附加:

一般我们开发时,我们在master主分支上创建一个自己的分支。每个开发者一个属于自己的分支,然后本地分支关联该分支。只在自己分支改动。 bug阶段,每个BUG(或者类别)新建一个分支,该分支与自己分支进行关联。不与主分支master关联。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题