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
Answer from Jeril Kuruvila on Stack OverflowIF 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
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
How to un-commit last un-pushed git commit without losing the changes - Stack Overflow
I have 3 commits that is not pushed. I need to change the 1st commit. What is the best way to do it?
How to undo pushed commits
git reset - How can I undo pushed commits using Git? - Stack Overflow
Videos
There are a lot of ways to do so, for example:
in case you have not pushed the commit publicly yet:
git reset HEAD~1 --soft
That's it, your commit changes will be in your working directory, whereas the LAST commit will be removed from your current branch. See git reset man
In case you did push publicly (on a branch called 'master'):
git checkout -b MyCommit //save your commit in a separate branch just in case (so you don't have to dig it from reflog in case you screw up :) )
revert commit normally and push
git checkout master
git revert a8172f36 #hash of the commit you want to destroy
# this introduces a new commit (say, it's hash is 86b48ba) which removes changes, introduced in the commit in question (but those changes are still visible in the history)
git push origin master
now if you want to have those changes as you local changes in your working copy ("so that your local copy keeps the changes made in that commit") - just revert the revert commit with --no-commit option:
git revert --no-commit 86b48ba (hash of the revert commit).
I've crafted a small example: https://github.com/Isantipov/git-revert/commits/master
The easiest way to undo the last Git commit is to execute the git reset command with one of the below options
- soft
- hard
- mixed
Let's assume you have added two commits and you want to undo the last commit
$ git log --oneline
45e6e13 (HEAD -> master) Second commit
eb14168 Initial commit
–soft option undo the last commit and preserve changes done to your files
$ git reset --soft HEAD~1
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: file.html
$ git log --oneline
eb14168 (HEAD -> master) Initial commit
–hard option undo the last commit and discard all changes in the working directory and index
$ git reset --hard HEAD~1
$ git status
nothing to commit, working tree clean
$ git log --oneline
eb14168 (HEAD -> master) Initial commit
--mixed option undo the last commit and keep changes in the working directory but NOT in the index
$ git reset --mixed HEAD~1
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: file.html
no changes added to commit (use "git add" and/or "git commit -a")
$ git log --oneline
eb14168 (HEAD -> master) Initial commit
I accidentally included a key file in the "doc update" commit so I need to remove it before pushing to the server. However, there are 2 more commits after that one. I know I could cancel the commits and make them all as working tree changes. I just wonder if there is a better way to preserve the last 2 commits.
P.S. this is a personal project so I am the only one who use this. That's why I have 3 commits queued up here. I know its a bad move. If I push my last commit before starting a new one, I could have avoid this situation.
-
One of our artists managed to push 1 month of progress without merging
-
As you can see I tried to undo the commits one by one in reverse order
-
I couldn't undo the merge commit of Arvincle because it's empty
-
When I try to revert the commit before that I get one file I need to merge first, but the rest of the changes are not undone
Is there a way to reset the HEAD of main to the last valid commit?
You can revert individual commits with:
git revert <commit_hash>
This will create a new commit which reverts the changes of the commit you specified. Note that it only reverts that specific commit, and not commits that come after that. If you want to revert a range of commits, you can do it like this:
git revert <oldest_commit_hash>..<latest_commit_hash>
It reverts all the commits after <oldest_commit_hash> up to and including <latest_commit_hash>. Some versions of git also revert the <oldest_commit_hash> itself, so double check if that commit gets reverted or not. You can always drop the latest revert commit (which reverts the oldest commit) with g reset --hard HEAD~.
To find out the hash of the commit(s) you can use git log.
Look at the git-revert man page for more information about the git revert command. Also, look at this answer for more information about reverting commits.
Extra note for re-applying reverted commits:
Usually you revert commits because you discovered the commits that you pushed turn out to have an issue. Then you first want to restore the repo to a stable state, before you continue to fix the issue. So then you would first do the git revert, as described before. Then push those revert commits, so the remote is stable. After that you would want to re-apply the reverted commits locally, so your local repo's files are back to the state before the revert. Then you can fix the issue.
To re-apply the reverted commits, you have to revert the revert commits. So what you would do, is to again execute the git revert command, but then with the range of commits of the revert commits. So your new revert commits will revert the previous revert commits. Then your files are back to the state before the first revert. Then you can fix the issue, create a new commit with the fix, and then push all the new commits.
A solution that keeps no traces of the "undo".
NOTE: Don't do this if someone already pulled your change (I would use this only on my personal repo.)
Run:
git reset <previous label or sha1>
Note: previous means the commit before the erroneous commit
This will re-checkout all the updates locally (so git status will list all updated files, meaning, the files you changed/added/.. and were committed).
Then you "do your work" and re-commit your changes (Note: This step is optional).
git commit -am "blabla"
At this moment your local tree differs from the remote
git push -f <remote-name> <branch-name>
will force the remote branch to take this push and remove the previous one (specifying remote-name and branch-name is not mandatory but is recommended to avoid updating all branches with update flag).
!! Watch-out some tags may still be pointing removed commit !! how-to-delete-a-remote-tag
Think we have code.txt file.
We make some changes on it and commit.
We can undo this commit in three ways, but first you should know what is the staged file...
An staged file is a file that ready to commit and if you run git status this file will be shown with green color and if this is not staged for commit will be shown with red color:

It means if you commit your change, your changes on this file is not saved.
You can add this file in your stage with git add code.txt and then commit your change:

Undo last commit:
Now if we want to just undo commit without any other changes, we can use
git reset --soft HEAD^
If we want to undo commit and its changes (THIS IS DANGEROUS, because your change will lost), we can use
git reset --hard HEAD^
And if we want to undo commit and remove changes from stage, we can use
git reset --mixed HEAD^or in a short formgit reset HEAD^
Usually, you want to undo a commit because you made a mistake and you want to fix it - essentially what the OP did when he asked the question. Really, you actually want to redo a commit.
Most of the answers here focus on the command line. While the command line is the best way to use Git when you're comfortable with it, its probably a bit alien to those coming from other version control systems to Git.
Here's how to do it using a GUI. If you have Git installed, you already have everything you need to follow these instructions.
NOTE: I will assume here that you realised the commit was wrong before you pushed it. If you don't know what pushing means, then you probably haven't pushed. Carry on with the instructions. If you have pushed the faulty commit, the least risky way is just to follow up the faulty commit with a new commit that fixes things, the way you would do it in a version control system that does not allow you to rewrite history.
That said, here's how to fix your most recent fault commit using a GUI:
- Navigate to your repository on the command line and start the GUI with
git gui - Choose "Amend last commit". You will see your last commit message, the files you staged and the files you didn't.
- Now change things to how you want them to look and click Commit.