Reputation: 4649
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
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
Reputation: 1
given:
steps:
git log
shows you hashes of all recent commits
git reset --soft HASH_OF_commit_C (unwanted-commit #1)
it will remove last commit, but keeps your changes un-staged.
git stash
it will stash your changes for now.
git reset --hard HASH_OF_commit_A
resets the branch on commit_A state without keeping your changes.
git stash apply
applies your changes from stash (commit_D)
git add .; git commit -am "fresh-commit"
add files and commit
git log
please ensure unwanted commits are no longer there
git push --force
updates remote branch tree with no commit_B and commit_C included.
Upvotes: 0
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