Git:如何忽略快进和恢复原点[分支]提前提交? [英] Git: How to ignore fast forward and revert origin [branch] to earlier commit?

查看:252
本文介绍了Git:如何忽略快进和恢复原点[分支]提前提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用


git reset --hard dc082bc... 

来恢复分支回到所需的以前的状态,由于某些不好的承诺。这已经让我的本地分支倒退了。不过,我想将'origin'上的分支倒回到同一个提交中,以便我可以重新开始。任何人都可以告诉我如何将原始分支(而不是主分支)还原为此提交?

to revert to the branch back to a required previous state, due to some bad commits. This has rewound my local branch fine. However, I want to rewind the branch on 'origin' to the same commit so that I can start again. Could anyone tell me how to revert the origin branch (not master) to this commit?

我尝试了git push origin master,但它给出了以下错误:

I've tried git push origin master, but it gives the following error


 ! [rejected]        branch -> branch (non-fast-forward)
error: failed to push some refs to 'git@github.com:xxx/xxx.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again.  See the 'Note about
fast-forwards' section of 'git push --help' for details.


推荐答案

您可以尝试 git push --force

You can try git push --force to force the push.

--force




通常,该命令拒绝更新远程引用,该引用不是本地引用的祖先,用于覆盖它。此标志禁用检查。

这可能会导致远程存储库丢失提交;

Usually, the command refuses to update a remote ref that is not an ancestor of the local ref used to overwrite it. This flag disables the check.
This can cause the remote repository to lose commits; use it with care.

所以如果很多人已经从原点拉同一个分支,那可能会导致一些重新发布问题
该操作可以在服务器端,如 ebneter 指出(在评论中):

So if lots of people have already pulled the same branch from origin, that can cause some rebase issue on their side.
That operation can be blocked at the server side, like ebneter points out (in the comments):

然而,根据远程配置的方式,这可能不起作用

- 我的所有中央仓库都配置了 接收.denyNonFastForwards = true receive.denyDeletes = true ,在这种情况下,任何此类手术必须在远程服务器上完成。

Depending on how the remote is configured, though, this may not work
-- all of my central repos are configured with receive.denyNonFastForwards = true and receive.denyDeletes = true, in which case any such surgery has to be done on the remote server.

但是,在GitHub的情况下,对于管理其GitHub仓库的用户来说,这些设置不是现成的。 br>
因此,如果您误解了 git push --force ,您只剩下
向GitHub支持开放一个案例,让他们检查本地(即GitHub )reflogs,看看他们是否可以恢复旧提交。

(由于reflog是本地的,,所以在推送过程中被新文件替换的提交 - 强制 / code>仍然只能看到,如果没有' git gc '或' git prune '已经发生,位于 GitHub 服务器端)

However, in the case of GitHub, such settings are not readily available for the user managing its GitHub repo.
So if you git push --force by mistake, all you are left is opening a case to the GitHub support, for them to check their local (i.e. "GitHub") reflogs and see if they can restore old commits.
(Since reflogs are local, like I have been remembered recently. So commits which are replaced by new ones during a push --force are only still visible, if no 'git gc' or 'git prune' already took place, at the GitHub server side)

因此, Marco Ceppi 坚持(在评论中):

So Marco Ceppi insists (in the comments):


如果您强行推送,这可能会让其他投稿者的本地回购更加混乱 - 尽管时代只是一种必然的罪恶​​(我可能必须这样做两次在我使用Git的生命周期中)

this could really mess up other contributors local repos if you force pushes - though are times that it is just a necessary evil (I've maybe had to do this two times in my lifespan of using Git)

这篇关于Git:如何忽略快进和恢复原点[分支]提前提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆