Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a git commit which has not been pushed

Tags:

git

I did a git commit but I have not pushed it to the repository yet. So when I do git status, I get '# Your branch is ahead of 'master' by 1 commit.

So if I want to roll back my top commit, can I just do:

git reset --hard eb27bf26dd18c5a34e0e82b929e0d74cfcaab316 

given that when I do git log I get:

 commit eb27bf26dd18c5a34e0e82b929e0d74cfcaab316 Date:   Tue Sep 29 11:21:41 2009 -0700   commit db0c078d5286b837532ff5e276dcf91885df2296 Date:   Tue Sep 22 10:31:37 2009 -0700
like image 806
hap497 Avatar asked Oct 23 '09 03:10

hap497


People also ask

How do you force delete a commit in git?

Forcefully Change Historygit rebase -i HEAD~<number of commits to go back> , so git rebase -i HEAD~5 if you want to see the last five commits. Then in the text editor change the word pick to drop next to every commit you would like to remove. Save and quit the editor. Voila!

How do I abandon a commit?

The easiest way to undo the last Git commit is to execute the “git reset” command with the “–soft” option that will preserve changes done to your files. You have to specify the commit to undo which is “HEAD~1” in this case. The last commit will be removed from your Git history.


2 Answers

IF you have NOT pushed your changes to remote

git reset HEAD~1 

Check if the working copy is clean by git status.

ELSE you have pushed your changes to remote

git revert HEAD 

This command will revert/remove the local commits/change and then you can push

like image 114
Jeril Kuruvila Avatar answered Oct 11 '22 13:10

Jeril Kuruvila


Actually, when you use git reset, you should refer to the commit that you are resetting to; so you would want the db0c078 commit, probably.

An easier version would be git reset --hard HEAD^, to reset to the previous commit before the current head; that way you don't have to be copying around commit IDs.

Beware when you do any git reset --hard, as you can lose any uncommitted changes you have. You might want to check git status to make sure your working copy is clean, or that you do want to blow away any changes that are there.

In addition, instead of HEAD you can use origin/master as reference, as suggested by @bdonlan in the comments: git reset --hard origin/master

like image 44
Brian Campbell Avatar answered Oct 11 '22 13:10

Brian Campbell