Home > Enterprise >  how to remove old committed changes in git?
how to remove old committed changes in git?

Time:01-04

I was working on something and push the changes, then I realized that my changes didn't go anywhere, so I reset the branch and started doing commits, then I can't push the latest commits because my branch is behind. I don't want the old pushed commits, just the new ones. What do I do? I have tried force push unsuccesfully.

        commit 0
    
       commit 1

commit 2.1  commit 2.2

commit 3.1 commit 3.2

commit 4.1   wanted and local

not wanted

and pushed

forced push didn't work as I don't have permissions

CodePudding user response:

I think the best you can do to "remove" the commit without the ability to rewrite history is to make a "revert" commit by simply undoing the changes of the original commit and then push this like any other commit. IDeally name the commit "Revert: ORIGINAL_COMMIT_MESSAGE" so that its clear they are related. Not much more you can do without the ability to edit commit history, which is almost always the best policy, too easy to accidentally erase someone elses work with a force push.

CodePudding user response:

Check out commit 3.2 (if it isn't checked out already) and change to a totally new branch name:

git switch -c totallyNewBranch

Now push that totally new branch. The push will succeed without force because you are adding new history, not changing existing history.

As for the old branch, if it's a PR, close it, and locally delete the old branch. (If you are allowed to delete the old branch on the remote, do that too.)

  •  Tags:  
  • git
  • Related