简体   繁体   English

恢复到特定提交但无法推送

[英]Revert to specific commit but can't push

I want to reset to a specific commit.我想重置为特定的提交。 I use the command git reset --hard Head~5 This has reset my local.我使用命令git reset --hard Head~5这已经重置了我的本地。 I make some changes and now I want to push.我做了一些改变,现在我想推动。 However I get Updates were rejected because the tip of your current branch is behind .但是我得到Updates were rejected because the tip of your current branch is behind . I don't want to pull though otherwise I'm back to the most recent commit.我不想拉,否则我会回到最近的提交。 How do I reset?如何重置?

You can forcibly push but this is potentially problematic.您可以强行推送,但这可能存在问题。

Are there other people working on this repo or is it just you?是否有其他人在处理这个 repo 还是只有你? Are you absolutely sure you want to erase those commits from the remote?您确定要从远程删除这些提交吗?

git push -f origin branch-name will update the remote branch to match the changes you've just made. git push -f origin branch-name将更新远程分支以匹配您刚刚所做的更改。

But you need to take care when doing this.但是在执行此操作时您需要小心。 If you have colleagues working on the same branch you should tell them in case they have already pulled in those changes.如果您有同事在同一个分支上工作,您应该告诉他们,以防他们已经进行了这些更改。 Plus they may have pushed other changes which you'd be overriding.此外,他们可能已经推动了您将要覆盖的其他更改。

You need to do a git push --force (not always advisable, read below).您需要执行git push --force (并不总是可取的,请阅读下文)。

If you don't have permission to do a push force then it means you cannot reset commits, instead try reverting them.如果您无权强制执行,则意味着您无法重置提交,而是尝试还原它们。

Note that if the branch is a main development branch, or it is already pulled by other developers, then resetting commits and pushing them (regardless of permission issues) is a dangerous thing to do, and git revert would be a better option.请注意,如果该分支是主要的开发分支,或者它已经被其他开发人员拉取,那么重置提交并推送它们(无论权限问题)是一件危险的事情,而git revert将是更好的选择。

This is the git revert equivalent of your example ( git reset --hard HEAD~5 ):这是 git revert 等效于您的示例( git reset --hard HEAD~5 ):

git revert Head~5..HEAD

This will revert commits represented by HEAD~4 , HEAD~3 , ..., and HEAD .这将恢复由HEAD~4HEAD~3 、 ... 和HEAD表示的提交。 It will keep your existing commits, and then will add extra revert commits on top.它将保留您现有的提交,然后在顶部添加额外的还原提交 There will be 1 revert commit per commit reverted which adds up to 5 revert commits in your case.每个还原提交将有 1 次还原提交,在您的情况下,这将增加 5 次还原提交。

If you prefer to manage the commits yourself (eg, revert them all in 1 commit instead of 5) use the -n option so that it will revert the changes in your local branch without committing them:如果您更喜欢自己管理提交(例如,在 1 次提交而不是 5 次提交中全部还原它们),请使用-n选项,以便在不提交它们的情况下还原本地分支中的更改:

git revert -n Head~5..HEAD

Then it'll be up to you to commit the outstanding changes in your local branch.然后由您在本地分支中提交未完成的更改。

git reset is safe when you're resetting local commits that are not yet pushed.当您重置尚未推送的本地提交时, git reset是安全的。 If you have push force permission, and you're absolutely sure no one else has pulled the commit you are about to reset, you can reset them locally and then do a push force.如果您有推力权限,并且您绝对确定没有其他人撤回您将要重置的提交,则可以在本地重置它们,然后执行推力。

When it comes to commits already pushed, git revert is safer than git reset because you're not changing history, or removing commits already pulled and relied on by other team members.当涉及到已经推送的提交时, git revertgit reset更安全,因为您不会更改历史记录,或者删除已经被其他团队成员拉取和依赖的提交。 Note that if things get out of hand during reverting (eg, you get a conflict that you're not sure how to resolve) you can always abort the operation by git revert --abort .请注意,如果在恢复期间事情失控(例如,您遇到不确定如何解决的冲突),您始终可以通过git revert --abort中止操作。 For more information see the git revert documentation page .有关更多信息,请参阅git revert 文档页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM