Reputation: 162915
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
Upvotes: 1527
Views: 2250311
Reputation: 729
Use "git log" to see the commits.
git reset HEAD~
: resets the current HEAD to the commit just before the current HEAD commit.
git reset HEAD~1
: same as above.
git reset HEAD~2
: resets the current HEAD to two commits just before the current HEAD commit.
Upvotes: 0
Reputation: 63
To delete folder from commit
git rm -r --cache <folder name>
To delete file from commit
git rm --cache <file name>
Upvotes: -2
Reputation: 1
If anyone else is looking for the opposite i.e., reverting back to the commit you initially thought you should delete, use git reflog
credit: Murtuzaali Surti's article on git reflog
to find the missing commits.
I luckily didn't manage to delete the commit I needed back, even after attempting multiple frantic git reset --soft/hard
commands and honestly I'm becoming more and more confident that git has my back no matter what, at least if a commit has been made.
You will still need to git reset --hard <SHA-found-with-reflog>
But see the link to the article for guidance.
Upvotes: -3
Reputation: 6306
git reset --hard origin/main
It works for other branch:
git reset --hard origin/master
git reset --hard origin/staging
to reset it to whatever the origin was at.
This was posted by @bdonlan in the comments. I added this answer for people who don't read comments.
Upvotes: 322
Reputation: 4790
I believe that one of those will fit your need
1 - Undo commit and keep all files staged:
git reset --soft HEAD~
2 - Undo commit and unstage all files:
git reset HEAD~
3 - Undo the commit and completely remove all changes:
git reset --hard HEAD~
here is were I found the answer
Upvotes: 396
Reputation: 19940
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
Upvotes: 1917
Reputation: 61
I just had the same problem and ended up doing:
git rebase -i HEAD~N
(N is the number of commits git will show you)
That prompts your text editor and then you can remove the commit you want by deleting the line associated with it.
Upvotes: 3
Reputation: 332736
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
Upvotes: 858
Reputation: 3042
Simply type in the console :
$ git reset HEAD~
This command discards all local commits ahead of the remote HEAD
Upvotes: 65
Reputation: 7368
There are two branches to this question (Rolling back a commit does not mean I want to lose all my local changes):
1. To revert the latest commit and discard changes in the committed file do:
git reset --hard HEAD~1
2. To revert the latest commit but retain the local changes (on disk) do:
git reset --soft HEAD~1
This (the later command) will take you to the state you would have been if you did git add
.
If you want to unstage the files after that, do
git reset
Now you can make more changes before adding and then committing again.
Upvotes: 128
Reputation: 671
Remove the last commit before push
git reset --soft HEAD~1
1
means the last commit, if you want to remove two last use 2
, and so forth*
Upvotes: 67
Reputation: 2031
This is what I do:
First checkout your branch (for my case master
branch):
git checkout master
Then reset to remote HEAD^ (it'll remove all your local changes), force clean and pull:
git reset HEAD^ --hard && git clean -df && git pull
Upvotes: 12
Reputation: 41
One way would be to delete the local branch and checkout that branch from the server if your local branch is ahead of remote by multiple commits and you need to uncommit all of them.
Upvotes: 4
Reputation: 241
I have experienced the same situation I did the below as this much easier.
By passing commit-Id
you can reach to the particular commit you want to go:
git reset --hard {commit-id}
As you want to remove your last commit so you need to pass the commit-Id
where you need to move your pointer:
git reset --hard db0c078d5286b837532ff5e276dcf91885df2296
Upvotes: 24