Home > Blockchain >  Git - Delete commits where half changeset is valid and other half is invalid
Git - Delete commits where half changeset is valid and other half is invalid

Time:10-24

So I did something stupid with a git project of mine. I accidentally committed and pushed a svg asset directory (~2000 svg files) that shouldn't have been checked into Git. I deleted the assets and then pushed again to remove it from the source code however I am not sure how to remove it from my git commit history.

  • 17a3534 - Last valid commit
  • 2129212 - Valid changes Invalid SVG Directory
  • 9c41416 - Deleted the Invalid SVG Directory

I'm not exactly sure how to fix this, I basically want to revert back to 17a3534 with the pending changes from 2129212 so I can delete the SVG directory and not have any of the .svg in the git commit history but keep the valid changes in the 2129212 commit.

Any ideas on how I would accomplish this?

If I can fix it locally I don't mind doing a force push and overriding the remote server.

Messed up git

CodePudding user response:

Following the advice in Dai's comment:

git reset --soft 2129212
# at this point you'll have all of the svg files staged for deletion

git commit --amend
# now you'll rewrite this commit with only the valid files committed

git push --force-with-lease

Side Note: in Git, the word revert has a slightly different meaning than it does in English. Git revert will effectively undo a commit by creating a new commit that undoes the changes in the original commit. This is normally the preferred method of undoing changes because it enables you to push to a shared branch without re-writing it. The downside, as you've witnessed, is that it doesn't purge the history. If you wish to rewrite the history, instead of revert you would use for example reset, rebase, or amend a commit, all of which will usually require a force push afterwards.

  •  Tags:  
  • git
  • Related