git - 如何将重新定位的分支恢复到原点?

标签 git git-rebase

我和我的同事最近实现了 git,并且刚刚尝试实现 rebase。因此,我们有一个本地克隆的远程 master 分支,我有一个名为 changes 的本地(和远程)分支。在本地,我们已经做了一些更改工作,所以它看起来像这样:

master: A
        \
changes: B-C-D

因此,通过来自其他地方的不相关 merge ,master 得到了升级,因此我们:

master: A-E
        \
changes: B-C-D

所以,我做了一个 rebase :

git checkout changes
git rebase E (via a tag)

因此,在我的本地存储库中,我有:

master: A-E
           \
changes:    B-C-D

所以,我尝试进行推送,但得到:

remote: error: denying non-fast-forward refs/heads/changes (you should pull first)

似乎,传统观点是强制 push 我的更改工作回到原点/更改:

git push -f origin changes

回复如下:

remote: error: denying non-fast-forward refs/heads/changes (you should pull first)
To /opt/git/online.git
 ! [remote rejected] changes -> changes (non-fast-forward)
error: failed to push some refs to '/opt/git/online.git'

所以,我现在有点困惑。我假设工作流程是在对本地更改进行 rebase 后,我可以使origin/changes对齐。我是不是理解错了?

最佳答案

使用 push -f 命令请求您的 Git 客户端尝试推送引用,即使它不是当前远程分支的后代。但是,上游存储库仍然可以拒绝该请求。

您可以通过更改上游存储库 config 文件中的配置选项来允许此操作:

[receive]
        denyNonFastforwards = false

Git book关于此选项有以下说法:

receive.denyNonFastForwards
If you rebase commits that you’ve already pushed and then try to push again, or otherwise try to push a commit to a remote branch that doesn’t contain the commit that the remote branch currently points to, you’ll be denied. This is generally good policy; but in the case of the rebase, you may determine that you know what you’re doing and can force-update the remote branch with a -f flag to your push command.

关于git - 如何将重新定位的分支恢复到原点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19042731/

相关文章:

windows - 无法创建 '/git/index.lock' : File exists - but it doesn't

Git rebase 失败, 'Your local changes to the following files would be overwritten by merge'。没有局部变化?

git - "git pull --rebase"导致 "Cannot rebase onto multiple branches"

Git 不准确地检测 merge 冲突

git - 如何让vi重绘屏幕?

git - 为什么git push gerrit HEAD :refs/for/master used instead of git push origin master

git - git 的 "rebase --preserve-merges"到底做了什么(为什么?)

git - 企业情况下如何在Git中管理一个软件的版本?

git - git 服务器构建是否应该不克隆到深度 1?

git - `go get` 和 `git clone` 之间的区别?