I want to revert or delete my git commit only in local system , so that while I push my code from local machine it doesn't appear in commits list in bitbucket or github. Please suggest a way.
-
2you can just `git reset --hard HEAD~1` locally and `git push origin master -f` to remove from bitbucket/github history. – sa77 Oct 15 '16 at 06:12
-
3Possible duplicate of [Delete commits from a branch in Git](http://stackoverflow.com/questions/1338728/delete-commits-from-a-branch-in-git) – sashoalm Oct 15 '16 at 07:13
-
Possible duplicate of http://stackoverflow.com/questions/927358/how-to-undo-last-commits-in-git – Deepak Mahakale Oct 15 '16 at 18:33
2 Answers
you can just reset the last commit using git reset
command
rub this command to list all commits
$ git log
commit a65aec279778a25870a2f194c0c91584e9a870c1
Author: Deepak <name@email.com>
Date: Wed Aug 31 19:58:17 2016 +0530
New commit
commit cb1137a1c6cb2ac7bdfe25e9cdda8a7513d599fc
Author: Deepak <name@email.com>
Date: Wed Aug 31 19:48:41 2016 +0530
Old commit
And revert to "Old commit"
state using
git reset cb1137a1c6cb2ac7bdfe25e9cdda8a7513d599fc
Note: This is the commit hash of old commit where you want to revert
To make sure "New commit"
is removed check again
$ git log
commit cb1137a1c6cb2ac7bdfe25e9cdda8a7513d599fc
Author: Deepak <name@email.com>
Date: Wed Aug 31 19:48:41 2016 +0530
Old commit

- 22,834
- 10
- 68
- 88
First of all, if you have done only that commit, I would prefer, delete your local branch and fetch the remote branch, that would be the best way to escape from the force commit or branch diversion.
For deleting the branch git branch -D <branch name>
, you should make sure, you are not on the branch, you need to delete.
After that, you can simply checkout to the branch itself, and you'll notice that the commit is not available there git checkout <branch name>
For sure if you have more than 1 commits which you need to merge to the remote, you should go with git reset --hard <commit-id>
and then push the code, there can be case where it'll not be pushed and it'll ask to take the pull of the branch, if you are sure you wanna push that, you can surely go for git push origin <branch name> --force
Please don't use this if you are working with a team, else your team will suffer because of the force commit. What you need to do there is rebase

- 1,090
- 1
- 9
- 29