Rolando
Rolando

Reputation: 62674

How to amend or delete a commit on a remote git repo?

If I have commited a change on my local repo, then git push'ed the change. I know you can amend and remove commits to a local repo, but what commands do I enter to do something similar for my remote repo:

1) Delete my last push from the remote repo. 2) Amend my last push to the remote repo.

Upvotes: 0

Views: 160

Answers (3)

Adam Dymitruk
Adam Dymitruk

Reputation: 129724

You have to force push your new reference:

vim yourfile
git add yourfile
git commit --amend --no-edit
git push -f origin yourbranch

Be careful if anyone else is using your repo - especially if it's a submodule to some other repo.

Upvotes: 1

Pablo Fernandez heelhook
Pablo Fernandez heelhook

Reputation: 12543

You would probably be better served reverting that last commit (i.e. introduce a new commit that reverts the previous one). Once you push to a remote repo you shouldn't be rewriting history. With git, revert has a very specific meaning: create a commit with the reverse patch to cancel it out. This way you don't rewrite any history.

git revert sha

Then just push normally, as your revert is a new commit, its not a special case for push, no need to force anything.

The git-revert manpage covers a lot of this in its description. Another useful link from the Git Community Book discussing git-revert is here.

Upvotes: 0

Michael Durrant
Michael Durrant

Reputation: 96554

You have to do stuff locally first.

One option is to do an interactive rebase and change the commits as you wish then push -f it.

git rebase -i HEAD~10 for last 10 commits

Upvotes: 1

Related Questions