0

I have a tricky situation. I need to remove and re-commit some commits that I already pushed to github remote repo. But these commits are not HEAD anymore (people pushed theirs on top of it).

I understand I can do revert commits, therefore preserving the commit history. But the requirement is to let wrong commits disappear.

So:

D <- other people's commit
|
C <- B and C are wrong commits! They need to be removed
| <- The changes that should be preserved from B and C will be
B <- committed on top of D
|
A <- this was the initial state

To give more explanation, I'm using 2 different GitHub accounts for work/personal. I accidentally committed B and C to company repo using personal GitHub account. So, I need to remove these commits from non-employee account

Boyang
  • 2,520
  • 5
  • 31
  • 49

1 Answers1

0

You can't do that.

The problem is that commit D has inside it the ID of commit C. Anything you do to get rid of B and C will, if it's effective at all, also get rid of commit D.

You can copy commit D to a new D' that is a lot like D but:

  1. removes the changes that were in B and C, and
  2. uses A as its parent commit

but if and when you do that, you must coördinate with everyone else who also has commit D now, especially whoever wrote it, and get all of them to switch to the new D' copy that has A as its parent.

Usually this is not worth doing. It's much too hard to get 47 other people to undo and redo all their work, just to compensate for a mistake you made earlier. Just live with the mistake.

If the mistake is worse than getting everyone else mad at you for making them re-do all their work, see How to modify existing, unpushed commits?

Community
  • 1
  • 1
torek
  • 448,244
  • 59
  • 642
  • 775