Deke
Deke

Reputation: 4649

Unable to delete unwanted commits made on github

I had two unwanted commits that had to be removed. So used this command git push -f origin HEAD^:master and git push -f origin HEAD^^:master to remove the commits. Which removed successfully. After that I went ahead like usual git push. But when I git push new commit, the deleted commits from the github are pushed back again to the github repo. How to push fresh commits without pushing the deleted commits to the github repo.

This is what's happening:

|
|----fresh-commit
|
|----unwanted-commit(deleted but pushed back)
|
|----unwanted-commit(deleted but pushed back)
|
|----previous-commits

what I want is this way:

|
|----fresh-commit
|
|----previous-commits
|

Upvotes: 3

Views: 179

Answers (3)

Masih
Masih

Reputation: 1710

You pushed the previous commits to remote successfully but when you do git push, you push your local changes again. If you don't want to see them you can either remove your local changes (clone your project from scratch after you removed unwanted commits) or do these:

git checkout HEAD~2
git branch -D master
git checkout -b master
git push -f origin master

which make a new master branch as you want!

Upvotes: 1

Filip Kokorevs
Filip Kokorevs

Reputation: 1

given:

  • commit_D
  • commit_C (unwanted #1)
  • commit_B (unwanted #2)
  • commit_A

steps:

  1. git log shows you hashes of all recent commits

  2. git reset --soft HASH_OF_commit_C (unwanted-commit #1) it will remove last commit, but keeps your changes un-staged.

  3. git stash it will stash your changes for now.

  4. git reset --hard HASH_OF_commit_A resets the branch on commit_A state without keeping your changes.

  5. git stash apply applies your changes from stash (commit_D)

  6. git add .; git commit -am "fresh-commit" add files and commit

  7. git log please ensure unwanted commits are no longer there

  8. git push --force updates remote branch tree with no commit_B and commit_C included.

Upvotes: 0

David Neiss
David Neiss

Reputation: 8237

After you force pushed to backup origin's master, it should have move your local remote tracking branch origin/master to your old HEAD~2 commit, after which you can just git rebase -i HEAD~3 and removed the offending commits and then pushed that (and no force required, since its fast forwardable).

Upvotes: 1

Related Questions