简体   繁体   English

如何在Git中删除已经推送到远程分支并出现在我的同事本地存储库中的提交?

[英]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: 所以你的课程是:

  1. 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分支)。

  2. 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 resetgit 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.

相关问题 删除在远程分支上推送的git提交 - Delete a git commit pushed on a remote branch 如果您已经将文件推送到远程分支,合并到开发分支并且它不是最新提交,如何从 git 中完全删除文件 - How to completely remove a file from git if you already pushed it to the remote branch, merged to the develop branch and it is not the latest commit 如何恢复已经推送到远程分支的合并提交? - How to revert a merge commit that's already pushed to remote branch? 如何撤消 VS Code 中远程和本地分支的推送提交? - How to undo a pushed commit from remote and local branch in VS Code? 在 Git 中撤消已推送到远程存储库的特定提交 - Undo a particular commit in Git that's been pushed to remote repos 推送到远程然后从远程主分支拉到本地后如何删除最后一个 git commit - How delete the last git commit after pushing to remote then pulling from remote master branch to local Git:是否接受远程 repos.gitignore 文件中存在的文件的推送更改? - Git: Are pushed changes of a file present in a remote repos .gitignore file accepted? 如何删除远程推送中的提交但保留本地代码 - How to delete a commit in remote pushed by mistake but keep local code 如何使用git删除本地和远程分支 - How to delete local and remote branch using git 如果其他人将他们的工作推送到我的远程分支,如何将我的本地分支推送到我的远程分支 - How to push my local branch to my remote branch if other people pushed their work to my remote branch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM