MohammadAli
MohammadAli

Reputation: 3466

How to remove commit on GitHub

I followed this link to remove a commit on GitHub:

And tried this to remove the commit:

$ git push -f origin HEAD^:master

But it showed me this error:

error: src refspec HEAD^ does not match any.
error: failed to push some refs to 'origin'

How can I fix this?

Upvotes: 0

Views: 104

Answers (1)

Mark Adelsberger
Mark Adelsberger

Reputation: 45819

Let's start with a better understanding of what the command you're trying to use would do. It does not "delete a commit"; it edits the history of master on origin, with the intent that the new history won't include the most recent commit.

Or to say it differently, it moves origin's master to the commit before the one you currently have checked out - which, if you currently have master checked out, and if your local master is in sync with origin's master, has the effect of removing the most recent commit from the history of master.

But the error you're getting indicates that there is no commit before the current one. If you have nothing to move master to, then you can't move master.

Of course, if what you're trying to do is remove the only commit that's on the remote, the easiest thing is to destroy and re-create the repository.

Now based on all that, you might wonder "then what is the command to just delete a commit from the remote?"... Well, there isn't one. At best you could remove a commit from history - but before you do that, you need to understand the consequences of doing so - and then git might eventually delete the commit in the course of periodic maintenance on the database.

The consequences of editing a branch's history, when that branch has been pushed, are that you will put the repository in a broken state for all other users who share it (if there are any). They'll get errors trying to do routine operations, and if they do the "obvious" thing to address those errors, it will undo the history edit and put your repo in a broken state. So if there are other users of the repo, you have to coordinate with them in order to do a history edit on the origin. See the git rebase docs under "Recovering from Upstream Rebase" for details.

Upvotes: 3

Related Questions