[git] How can I remove a commit on GitHub?

I "accidentally" pushed a commit to GitHub.

Is it possible to remove this commit?

I want to revert my GitHub repository as it was before this commit.

This question is related to git github git-commit

The answer is


To preserve the branching and merging structure is important to use the --preserve-merges option when doing the rebase:

git rebase --preserve-merges -i HEAD^^

Save your local changes first somewhere on the side ( backup )

You can browse your recent commits, then select a commit hash by clicking on "Copy the full SHA" button to send it to the clipboard.

If your last commit hash is, let's say g0834hg304gh3084gh ( for example )

You have to run:

git push origin +g0834hg304gh3084gh:master

Using the hash that you've copied earlier to make it the "HEAD" revision.

Add your desired local changes. Done ;)


Find the ref spec of the commit you want to be the head of your branch on Github and use the following command:

git push origin +[ref]:[branchName]

In your case, if you just want to go back one commit, find the beginning of the ref for that commit, say for example it is 7f6d03, and the name of the branch you want to change, say for example it is master, and do the following:

git push origin +7f6d03:master

The plus character is interpreted as --force, which will be necessary since you are rewriting history.

Note that any time you --force a commit you could potentially rewrite other peoples' history who merge your branch. However, if you catch the problem quickly (before anyone else merges your branch), you won't have any issues.


Run this command on your terminal.

git reset HEAD~n

You can remove the last n commits from local repo e.g. HEAD~2. Proceed with force git push on your repository.

git push -f origin <branch>

Hope this helps!


You'll need to clear out your cache to have it completely wiped. this help page from git will help you out. (it helped me) http://help.github.com/remove-sensitive-data/


In case you like to keep the commit changes after deletion:

Note that this solution works if the commit to be removed is the last committed one.


1 - Copy the commit reference you like to go back to from the log:

git log

2 - Reset git to the commit reference:

 git reset <commit_ref>

3 - Stash/store the local changes from the wrong commit to use later after pushing to remote:

 git stash

4 - Push the changes to remote repository, (-f or --force):

git push -f

5 - Get back the stored changes to local repository:

git stash apply

7 - In case you have untracked/new files in the changes, you need to add them to git before committing:

git add .

6 - Add whatever extra changes you need, then commit the needed files, (or use a dot '.' instead of stating each file name, to commit all files in the local repository:

git commit -m "<new_commit_message>" <file1> <file2> ...

or

git commit -m "<new_commit_message>" .

Use git revert for reverting your push.

git-revert - Revert some existing commits

git revert [--edit | --no-edit] [-n] [-m parent-number] [-s] <commit>...
git revert --continue
git revert --quit
git revert --abort

Revert the changes that the related patches introduce, and record some new commits that record them. This requires your working tree to be clean (no modifications from the HEAD commit).


You need to know your commit hash from the commit you want to revert to. You can get it from a GitHub URL like: https://github.com/your-organization/your-project/commits/master

Let's say the hash from the commit (where you want to go back to) is "99fb454" (long version "99fb45413eb9ca4b3063e07b40402b136a8cf264"), then all you have to do is:

git reset --hard 99fb45413eb9ca4b3063e07b40402b136a8cf264
git push --force

1. git reset HEAD^ --hard
2. git push origin -f

This work for me.


In GitHub Desktop you can just right click the commit and revert it, which will create a new commit that undoes the changes.

The accidental commit will still be in your history (which may be an issue if, for instance, you've accidentally commited an API key or password) but the code will be reverted.

This is the simplest and easiest option, the accepted answer is more comprehensive.


In case you like to keep the commit changes after deletion:

Note that this solution works if the commit to be removed is the last committed one.


1 - Copy the commit reference you like to go back to from the log:

git log

2 - Reset git to the commit reference:

 git reset <commit_ref>

3 - Stash/store the local changes from the wrong commit to use later after pushing to remote:

 git stash

4 - Push the changes to remote repository, (-f or --force):

git push -f

5 - Get back the stored changes to local repository:

git stash apply

7 - In case you have untracked/new files in the changes, you need to add them to git before committing:

git add .

6 - Add whatever extra changes you need, then commit the needed files, (or use a dot '.' instead of stating each file name, to commit all files in the local repository:

git commit -m "<new_commit_message>" <file1> <file2> ...

or

git commit -m "<new_commit_message>" .

Run this command on your terminal.

git reset HEAD~n

You can remove the last n commits from local repo e.g. HEAD~2. Proceed with force git push on your repository.

git push -f origin <branch>

Hope this helps!


You need to know your commit hash from the commit you want to revert to. You can get it from a GitHub URL like: https://github.com/your-organization/your-project/commits/master

Let's say the hash from the commit (where you want to go back to) is "99fb454" (long version "99fb45413eb9ca4b3063e07b40402b136a8cf264"), then all you have to do is:

git reset --hard 99fb45413eb9ca4b3063e07b40402b136a8cf264
git push --force

if you want to remove do interactive rebase,

git rebase -i HEAD~4

4 represents total number of commits to display count your commit andchange it accordingly

and delete commit you want from list...

save changes by Ctrl+X(ubuntu) or :wq(centos)

2nd method, do revert,

git revert 29f4a2 #your commit ID

this will revert specific commit


git push -f origin HEAD^:master

That should "undo" the push.


For an easy revert if it's just a mistake (perhaps you forked a repo, then ended up pushing to the original instead of to a new one) here's another possibility:

git reset --hard 71c27777543ccfcb0376dcdd8f6777df055ef479

Obviously swap in that number for the number of the commit you want to return to.

Everything since then will be deleted once you push again. To do that, the next step would be:

git push --force

Add/remove files to get things the way you want:

git rm classdir
git add sourcedir

Then amend the commit:

git commit --amend

The previous, erroneous commit will be edited to reflect the new index state - in other words, it'll be like you never made the mistake in the first place

Note that you should only do this if you haven't pushed yet. If you have pushed, then you'll just have to commit a fix normally.


Use git revert for reverting your push.

git-revert - Revert some existing commits

git revert [--edit | --no-edit] [-n] [-m parent-number] [-s] <commit>...
git revert --continue
git revert --quit
git revert --abort

Revert the changes that the related patches introduce, and record some new commits that record them. This requires your working tree to be clean (no modifications from the HEAD commit).


1. git reset HEAD^ --hard
2. git push origin -f

This work for me.


You'll need to clear out your cache to have it completely wiped. this help page from git will help you out. (it helped me) http://help.github.com/remove-sensitive-data/


git push -f origin HEAD^:master

That should "undo" the push.


If you are doing this because you have sensitive data in a commit, using the other answers here is not safe (excepting subutux's, which I'll expand on).

The github guide on this recommends using a external tool, but I prefer using the built-in one.

Firstly, make a backup of your repository. Then:

git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA' \
--prune-empty --tag-name-filter cat -- --all

After this, make sure the repository is in the state you want. You might want to diff against the backup.

If you're sure it's correct, then:

#get rid of old unreferenced commits (including the data you want to remove)
git gc --prune=now
git push origin --force --all

You might want to keep the local backup for a while, just in case.


In GitHub Desktop you can just right click the commit and revert it, which will create a new commit that undoes the changes.

The accidental commit will still be in your history (which may be an issue if, for instance, you've accidentally commited an API key or password) but the code will be reverted.

This is the simplest and easiest option, the accepted answer is more comprehensive.


git push -f origin HEAD^:master

That should "undo" the push.


Delete the most recent commit, keeping the work you've done:

git reset --soft HEAD~1

Delete the most recent commit, destroying the work you've done:

git reset --hard HEAD~1

To delete the commit from the remote repository:

 git push -f origin last_known_good_commit:branch_name

In order delete the commit from your local repository:

git reset --hard HEAD~1

link


Find the ref spec of the commit you want to be the head of your branch on Github and use the following command:

git push origin +[ref]:[branchName]

In your case, if you just want to go back one commit, find the beginning of the ref for that commit, say for example it is 7f6d03, and the name of the branch you want to change, say for example it is master, and do the following:

git push origin +7f6d03:master

The plus character is interpreted as --force, which will be necessary since you are rewriting history.

Note that any time you --force a commit you could potentially rewrite other peoples' history who merge your branch. However, if you catch the problem quickly (before anyone else merges your branch), you won't have any issues.


  1. git log to find out the commit you want to revert

  2. git push origin +7f6d03:master while 7f6d03 is the commit before the wrongly pushed commit. + was for force push

And that's it.

Here is a very good guide that solves your problem, easy and simple!


git push -f origin HEAD^:master

That should "undo" the push.


If you are doing this because you have sensitive data in a commit, using the other answers here is not safe (excepting subutux's, which I'll expand on).

The github guide on this recommends using a external tool, but I prefer using the built-in one.

Firstly, make a backup of your repository. Then:

git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA' \
--prune-empty --tag-name-filter cat -- --all

After this, make sure the repository is in the state you want. You might want to diff against the backup.

If you're sure it's correct, then:

#get rid of old unreferenced commits (including the data you want to remove)
git gc --prune=now
git push origin --force --all

You might want to keep the local backup for a while, just in case.


For an easy revert if it's just a mistake (perhaps you forked a repo, then ended up pushing to the original instead of to a new one) here's another possibility:

git reset --hard 71c27777543ccfcb0376dcdd8f6777df055ef479

Obviously swap in that number for the number of the commit you want to return to.

Everything since then will be deleted once you push again. To do that, the next step would be:

git push --force

To delete the commit from the remote repository:

 git push -f origin last_known_good_commit:branch_name

In order delete the commit from your local repository:

git reset --hard HEAD~1

link


It is not very good to re-write the history. If we use git revert <commit_id>, it creates a clean reverse-commit of the said commit id.

This way, the history is not re-written, instead, everyone knows that there has been a revert.


It is not very good to re-write the history. If we use git revert <commit_id>, it creates a clean reverse-commit of the said commit id.

This way, the history is not re-written, instead, everyone knows that there has been a revert.


To preserve the branching and merging structure is important to use the --preserve-merges option when doing the rebase:

git rebase --preserve-merges -i HEAD^^

For GitHub

  • Reset your commits (HARD) in your local repository
  • Create a new branch
  • Push the new branch
  • Delete OLD branch (Make new one as the default branch if you are deleting the master branch)

Delete the most recent commit, keeping the work you've done:

git reset --soft HEAD~1

Delete the most recent commit, destroying the work you've done:

git reset --hard HEAD~1

For GitHub

  • Reset your commits (HARD) in your local repository
  • Create a new branch
  • Push the new branch
  • Delete OLD branch (Make new one as the default branch if you are deleting the master branch)

Add/remove files to get things the way you want:

git rm classdir
git add sourcedir

Then amend the commit:

git commit --amend

The previous, erroneous commit will be edited to reflect the new index state - in other words, it'll be like you never made the mistake in the first place

Note that you should only do this if you haven't pushed yet. If you have pushed, then you'll just have to commit a fix normally.


if you want to remove do interactive rebase,

git rebase -i HEAD~4

4 represents total number of commits to display count your commit andchange it accordingly

and delete commit you want from list...

save changes by Ctrl+X(ubuntu) or :wq(centos)

2nd method, do revert,

git revert 29f4a2 #your commit ID

this will revert specific commit


Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio?

Examples related to github

Does the target directory for a git clone have to match the repo name? Issue in installing php7.2-mcrypt How can I switch to another branch in git? How to draw checkbox or tick mark in GitHub Markdown table? How to add a new project to Github using VS Code git clone error: RPC failed; curl 56 OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054 How to add empty spaces into MD markdown readme on GitHub? key_load_public: invalid format git - remote add origin vs remote set-url origin Cloning specific branch

Examples related to git-commit

How to undo the last commit in git git status (nothing to commit, working directory clean), however with changes commited Fix GitLab error: "you are not allowed to push code to protected branches on this project"? How to rollback everything to previous commit Troubleshooting misplaced .git directory (nothing to commit) Pushing empty commits to remote Please enter a commit message to explain why this merge is necessary, especially if it merges an updated upstream into a topic branch How do I commit case-sensitive only filename changes in Git? How to save a git commit message from windows cmd? Remove files from Git commit