gpt4 book ai didi

Git:如何忽略快进并将原点[分支]恢复到较早的提交?

转载 作者:太空狗 更新时间:2023-10-29 12:48:59 24 4
gpt4 key购买 nike

我用过

git reset --hard dc082bc... 
由于一些错误的提交,将分支恢复到所需的先前状态。这已经恢复了我本地的分支机构。但是,我想将“origin”上的分支倒回同一个提交,以便我可以重新开始。谁能告诉我如何将原始分支(不是主分支)恢复到此提交?

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

 ! [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 rejectedMerge the remote changes before pushing again.  See the 'Note aboutfast-forwards' section of 'git push --help' for details.

最佳答案

添加到我的previous answer ,并且为了解决强制 git push 确实会弄乱其他贡献者的本地存储库这一事实,git 1.8.5(即将于 2013 年第四季度发布)将看到一个新选项:

git push --force-with-lease

this thread 中查看该选项的来源:

if something happens at 'origin' to the branch you are forcing or deleting since you fetched to inspect it, you may end up losing other people's work.

Somebody who is unaware of the decision to rewind and rebuild the branch may attempt to push to the branch between the time you fetched to rebase it and the time you pushed to replace it with the result of the rebasing.

We can make these pushes safer by optionally allowing the user to tell "git push" this:

I am forcing/deleting, based on the assumption that the value of 'branch' is still at this object.
If that assumption no longer holds, i.e. if something happened to the branch since I started preparing for this push, please do not proceed and fail this push.

您可以在commit 28f5d17 中查看--force-with-lease 的完整文档

--force-with-lease will protect all remote refs that are going to be updated by requiring their current value to be the same as some reasonable default, unless otherwise specified;

For now, "some reasonable default" is tentatively defined as "the value of the remote-tracking branch we have for the ref of the remote being updated", and it is an error if we do not have such a remote-tracking branch.

解释该选项的“租赁”部分:

"force-with-lease": You assume you took the lease on the ref when you fetched to decide what the rebased history should be, and you can push back only if the lease has not been broken.


这已经在测试中,并在“What's cooking in git.git (Aug 2013, #07; Wed, 28)”中提到:

By the way, the push that overrides the usual "must fast-forward" was done using the "force-with-lease" option that has been cooking in next, like so:

$ git fetch ko next
$ anchor=$(git rev-parse --verify FETCH_HEAD)
$ for remote in ko repo gph github2
do
git push --force-with-lease=refs/heads/next:$anchor $remote next
done

注意:“git push --force-with-lease”已经被教导报告如果推送需要强制(或快进)。

所以这个命令在 git 2.8(2016 年 3 月)的输出中更加详细

push: fix ref status reporting for --force-with-lease

The --force--with-lease push option leads to less detailed status information than --force.
In particular, the output indicates that a reference was fast-forwarded, even when it was force-updated.


当心该选项被忽略/绕过,如explained in Git 2.13 (Q2 2017) .

关于Git:如何忽略快进并将原点[分支]恢复到较早的提交?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3166713/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com