[英]How to delete a commit in Git that is already pushed to a remote branch and present in my coworkers local repos?
How to delete a commit in Git that is already pushed to a remote branch and present in my coworkers local repos? 如何在Git中删除已经推送到远程分支并出现在我的同事本地存储库中的提交?
The first part is relatively easy (deleting a commit locally and from the remote is already well described on stack overflow, for example: Delete commits from a branch in Git ) 第一部分相对容易(在堆栈溢出中已经很好地描述了在本地和远程删除提交 ,例如: 从Git中的分支删除提交 )
But the problem arises when that faulty commit (or a set of commits) has been already downloaded by other coworkers. 但是当其他同事已经下载了错误的提交(或一组提交)时,就会出现问题。 When they execute git pull
command, the removed commit will still appear in their local history so that the commit can be restored...how to avoid it? 当他们执行git pull
命令时,删除的提交仍将出现在其本地历史记录中,以便可以恢复该提交...如何避免呢?
Is there a sort of git pull --force
command that will disregard local commits that have been deleted from remote? 是否有某种git pull --force
命令可以忽略已从远程删除的本地提交?
Forced fetch ( git fetch -f
) or pull ( git pull origin +master
) are dangerous because they discard local commits that haven't been pushed yet. 强制获取( git fetch -f
)或拉取( git pull origin +master
)很危险,因为它们会丢弃尚未推送的本地提交。 So your course is: 所以你的课程是:
You remove the offending commit with git rebase -i
and force-push the branch: git push origin +master
(let's talk about origin
remote repo and master
branch). 您可以使用git rebase -i
删除有问题的提交,然后强制推送分支: git push origin +master
(让我们谈论origin
远程仓库和master
分支)。
All your coworkers do: 您所有的同事都做:
2a. 2a。 git branch save-master master
to create a new branch that points to old master — ie the unpushed commits. git branch save-master master
创建一个指向旧master的新分支-即未推动的提交。
2b. 2b。 Forced pull: git pull origin +master
. 强制拉: git pull origin +master
。
2c. 2c。 Cherry-pick unpushed commits from the old master: git cherry-pick save-master
(adapt the command to the number of unpushed commits). 来自旧主节点的Cherry-pick未推动的提交: git cherry-pick save-master
(使命令适应未推动的提交数量)。
2d. 2d。 Remove old master: git branch -D save-master
. 删除旧的master: git branch -D save-master
。
git pull origin master --rebase
can update your coworkers repos and make their changes on the top of updated master
branch (assume it's master branch here). git pull origin master --rebase
可以更新您的同事git pull origin master --rebase
并在更新的master
分支的顶部进行更改 (假设这里是master分支)。
Since you have already know how to delete a commit which has pushed to remote repo ( git reset
or git rebase -i
), I will only illustrate the second part by graphs: update your coworkers local repos with updated master
branch. 由于您已经知道如何删除已推送到远程存储库的提交( git reset
或git rebase -i
),因此我仅通过图形说明第二部分:使用更新的master
分支更新您的同事本地存储库。
Assume the master
branch original history is: 假设master
分支的原始历史为:
A---B---C---D---E master
And you find the commit D
is a wrong commit and you delete in commit history: 并且您发现提交D
是错误的提交,并在提交历史记录中将其删除:
A---B---C---E' master
While your coworkers still work based on the old master
branch with their local changes: 当您的同事仍基于旧的master
分支及其本地更改来工作时:
A---B---C---D---E---F---G master
After they execute git pull origin master --rebase
, their local commit history will look like: 在执行git pull origin master --rebase
,其本地提交历史记录将如下所示:
A---B---C---E'---F'---G' master
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.