-1

Let's say I have a large project with a large team and a fast pace of committing. I write some code, make a commit, and push that commit to origin.

Then I decided I no longer want that commit. If I were to

git reset --hard HEAD~1

or something similar, my history goes back, and GitHub keeps insisting on making me run a pull, since my local is now behind GitHub.

Instead, what I want to do is go back a commit, but have the history appear as though I made the original commit, and then made another commit afterwards, but the second commit is actually just the removal of the code in the first. That way other team members can pull at any time without the history getting screwed up.

How can I achieve this?

isherwood
  • 58,414
  • 16
  • 114
  • 157
John Kealy
  • 1,503
  • 1
  • 13
  • 32
  • 2
    Did you try... [`revert`](https://git-scm.com/docs/git-revert)? – jonrsharpe Aug 10 '21 at 15:16
  • You seem to want to hide the reversion, which is understandable but not usually necessary or important. You won't do that without a reset, which creates divergence requiring more than just a pull from other devs. – isherwood Aug 10 '21 at 15:18
  • Does this answer your question? [How to revert last commit and remove it from history?](https://stackoverflow.com/questions/8903953/how-to-revert-last-commit-and-remove-it-from-history) – isherwood Aug 10 '21 at 15:22
  • I literally wrote "keeping the history moving foward" in the title. Why does everyone think I want to change the history? – John Kealy Aug 10 '21 at 15:31
  • 1
    Because you said "go back a commit". And in particular, "go back a commit, but". This implies that you want a difference between visible and history and reality. – matt Aug 10 '21 at 15:48
  • 2
    It sounds like you simply didn't know about `git revert`, which does *exactly* what you want: make a *new* commit which completely undoes another commit. Now that you know this, I'm not sure this question has much value (IMHO). Maybe consider deleting it? – TTT Aug 10 '21 at 17:13

1 Answers1

1

Instead, what I want to do is go back a commit, but have the history appear as though I made the original commit, and then made another commit afterwards, but the second commit is actually just the removal of the code in the first

That's silly. You can't go backwards and pretend that you went forward. Instead of having the history appear a certain way, have the history be that way — namely, that you in fact made the original commit and then made another commit afterwards that removes the changes introduced by the first. That's exactly what git revert does, so just use it. Nothing dishonorable about that; mistakes do get made, and fixing them is part of the job.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I obviously phrased this question really badly. I don't care what the history looks like. I just want to remove the changes while keeping the history linear, so git and github don't complain. `reset` causes this kind of problem. I'll check out `revert`, thanks. – John Kealy Aug 10 '21 at 15:29
  • Yes, `revert` does exactly that: it keeps the history linear. It makes things go `A -- B -- C -- Ooops -- Unoops -- D`. – matt Aug 10 '21 at 15:48