0

How to delete a commit in Git that is already pushed to a remote branch and present in my coworkers local repos?

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)

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?

Is there a sort of git pull --force command that will disregard local commits that have been deleted from remote?

fuggy_yama
  • 607
  • 6
  • 24
  • 1
    Once history has been made publically available, it is essentially inviolate - there is no good way to do what you want. What you _should_ do instead is revert the commits in question using `git revert `, which will generate _new_ commits that exactly undo the ones you don't want. – Sebastian Lenartowicz Jun 28 '17 at 09:25
  • 1
    Try reverting back to and commit the new version and push. A git pull from your co-worker should solve – Srini V Jun 28 '17 at 09:26
  • I read about `git revert ` command but my goal is to erase that commit from the history – fuggy_yama Jun 28 '17 at 09:28

2 Answers2

1

Forced fetch (git fetch -f) or pull (git pull origin +master) are dangerous because they discard local commits that haven't been pushed yet. 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).

  2. All your coworkers do:

2a. git branch save-master master to create a new branch that points to old master — i.e. the unpushed commits.

2b. Forced pull: git pull origin +master.

2c. Cherry-pick unpushed commits from the old master: git cherry-pick save-master (adapt the command to the number of unpushed commits).

2d. Remove old master: git branch -D save-master.

phd
  • 82,685
  • 13
  • 120
  • 165
0

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).

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.

Assume the master branch original history is:

A---B---C---D---E   master

And you find the commit D is a wrong commit and you delete in commit history:

A---B---C---E'   master

While your coworkers still work based on the old master branch with their local changes:

A---B---C---D---E---F---G   master

After they execute git pull origin master --rebase, their local commit history will look like:

A---B---C---E'---F'---G'   master
Marina Liu
  • 36,876
  • 5
  • 61
  • 74