I have a pushed commit on branch (it's not main repo branch). It's a last commit I did. I want to delete it from repo. After pushing I've already done some local changes (at same branch).
I want to keep all my changes locally.
I'd go git revert <commit>
, than git add .
-> git commit
-> git push origin my-branch
?
-
1why do you want to delete the commit? . If you already have solved the problem in your local just check in that – karthick Feb 09 '18 at 18:20
-
@karthick that commit has an error in my code – Feb 09 '18 at 18:21
-
1you can always fix it and check the fix as a new commit. But if you are fixated on reverting the commit yeah the steps you wrote are right. – karthick Feb 09 '18 at 18:23
-
Possible duplicate of [How to add a file to the last commit in git?](https://stackoverflow.com/questions/40503417/how-to-add-a-file-to-the-last-commit-in-git) – Vinay Prajapati Feb 10 '18 at 16:56
-
@DoIGetAnything have you got your answer? – Vinay Prajapati Feb 12 '18 at 18:43
3 Answers
Please correct me if I'm wrong, but this is my understanding of your situation:
- You made some changes, and committed them locally with
git commit
. - You pushed your changes to a remote repository (Github?) with
git push
. - Now, you've realized there is a problem with the changes you just pushed, and you'd like to correct it. You made these changes locally, but have not committed them yet.
If that's correct, from this point, you have two options. You can either:
- Simply make a new commit, and push it up. This is the simplest option, but it does leave your mistake in the git history. To do this, you'd simply
git add
,git commit
andgit push
, as you did before. - OR, you can amend the commit you made, and push the amended commit (essentially deleting the first (incorrect) commit you pushed). This option rewrites history, which is fine as long as no one else has done work based on this branch yet. And this option will remove your bad commit completely from the history.
If you want to do option 2, you need to:
git add
to stage your files, just like you normally would when creating a new commit.git commit --amend
Amend the previous commit with your current local changes. After this, it will have a completely new commit hash (as far as git is concerned, it bears no relation to the previous bad commit).git push -f origin
Force push your changes. The force is required because you're changing branch history.

- 16,022
- 3
- 42
- 46
-
Can you please explain how could new files be added using `git comit --amend` – Vinay Prajapati Feb 10 '18 at 16:54
-
1Same way as you'd add them without the `--amend` flag - you can either run `git add path/to/file` and then `git commit --amend`, or you can do both at once with `git commit -a --amend`. – Jim Redmond Feb 12 '18 at 08:05
-
The easiest way is to do a reset HEAD
.
Approach 1:
- Stash your local changes.
git stash
- reset your branch to the commit number last_commit-1 as follows.
git reset HEAD~1
or
git reset HEAD^
- Now pop changes from stash.
git stash pop
Resolve if any conflict occurs during
stash pop
.Now commit all your changes together and you are done.
The advantage with this approach is that you will save 1 commit hash for yourself.
Approach 2:
This approach is that
you just prepare another commit with your other local changes that you have.
Now squash the changes as follows.
git rebase -i HEAD~2
replace pick
with squash
for the recent commit that you made in step 1.
- Now save the changes. Use Ctrl+O for this. Next you will be prompted for the commit message. You could modify the commit message as per your requirement.
We are done!
My personal preference would always be to use squashing
. But with one precaution i.e. use it usually when fewer people are working on a branch else it might turn into hell if somebody novice did a git pull rather than a git reset --hard origin/branch_name
on local.
Hope it helps!

- 54,987
- 21
- 291
- 190

- 7,199
- 9
- 45
- 86
Here are the steps you can do that.
Take back up of your local branch. Let say if your branch is master and your back brach name is like master_backup. You can use below git command.
git branch master-backup
if you are on the same branch. or you can also usegit branch master-backup master
git revert <your commit id>
Resolve if there is any conflict.
git push origin master
(Push your changes after revert)git reset --hard master-backup
(Get your changes back to local master)

- 7,075
- 1
- 19
- 42