I accidentally committed the wrong files to Git but haven't pushed the commit to the server yet.
How do I undo those commits from the local repository?
I accidentally committed the wrong files to Git but haven't pushed the commit to the server yet.
How do I undo those commits from the local repository?
To reset to the previous revision, permanently deleting all uncommitted changes:
git reset --hard HEAD~1
--soft to keep your changes as uncommitted changes, --hard to nuke the commit completely and revert back by one. Remember to do such operations only on changes, that are not pushed yet.
Commented
Mar 9, 2015 at 9:11
git reset --hard.
Commented
Sep 13, 2016 at 21:17
git stores its contents in its object database. The stored contents are only removed when garbage collection is executed. It is therefore possible to recover the last staged version of a file that was not currently staged when git reset --hard was executed (see the posts linked above for more information).
Commented
Sep 13, 2016 at 21:22
Use SourceTree (graphical tool for Git) to see your commits and tree. You can manually reset it directly by right clicking it.
A simple step-by-step guide is as follows:
Destroy a commit and throw away any uncommitted changes
git reset --hard HEAD~1
Undo the commit, but keep your changes
git reset HEAD~1
Keep your files, and stage all changes back automatically
git reset --soft HEAD~1
Resurrect a commit you destroyed
git reflog # To find the sh
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 form git 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:
git guiIf you want to revert the last commit but still want to keep the changes locally that were made in the commit, use this command:
git reset HEAD~1 --mixed
You can use:
git reset HEAD@{1}
This command will delete your wrong commit without a Git log.
Undo the Last Commit
There are tons of situations where you really want to undo that last commit into your code. E.g. because you'd like to restructure it extensively - or even discard it altogether!
In these cases, the "reset" command is your best friend:
$ git reset --soft HEAD~1
The above command (reset) will rewind your current HEAD branch to the specified revision. In our example above, we'd like to return to the one before the current revision - effectively making our last commit undone.
Note the --soft flag: this makes sure that the changes in undone revisions are preserved. After running the command, you'll find the changes as uncommitted local modifications in your working copy.
If you don't want to keep these changes, simply use the --hard flag. Be sure to only do this when you're sure you don't need these changes any more.
$ git reset --hard HEAD~1
Just undo the last commit:
git reset --soft HEAD~
Or undo the time before last time commit:
git reset --soft HEAD~2
Or undo any previous commit:
git reset --soft <commitID>
(you can get the commitID using git reflog)
When you undo a previous commit, remember to clean the workplace with
git clean
More details can be found in the docs: git-reset
Before answering let's add some background, explaining what is this HEAD.
First of all what is HEAD?HEAD is simply a reference to the current commit (latest) on the current branch.
There can only be a single HEAD at any given time. (excluding git worktree)
The content of HEAD is stored inside .git/HEAD and it contains the 40 bytes SHA-1 of the current commit.
detached HEADIf you are not on the latest commit - meaning that HEAD is pointing to a prior commit in history its called detached HEAD.
On the command line, it will look like this- SHA-1 instead of the branch name since the HEAD is not pointing to the tip of the current branch

git checkoutgit checkout <commit_id>
git checkout -b <new branch> <commit_id>
git checkout HEAD~X // x is the number of commits t go back
This will checkout new branch pointing to the desired commit.
This command will checkout to a given commit.
At this point, you can create a branch and start to work from this point on.
# Checkout a given commit.
# Doing so will result in a `detached HEAD` which mean that the `HEAD`
# is not pointing to the latest so you will need to checkout branch
# in order to be able to update the code.
git checkout <commit-id>
# create a new branch forked to the given commit
git checkout -b <branch name>
git reflogYou can always use the reflog as well.
git reflog will display any change which updated the HEAD and checking out the desired reflog entry will set the HEAD back to this commit.
Every time the HEAD is modified there will be a new entry in the reflog
git reflog
git checkout HEAD@{...}
This will get you back to your desired commit
git reset --hard <commit_id>"Move" your HEAD back to the desired commit.
# This will destroy any local modifications.
# Don't do it if you have uncommitted work you want to keep.
git reset --hard 0d1d7fc32
# Alternatively, if there's work to keep:
git stash
git reset --hard 0d1d7fc32
git stash pop
# This saves the modifications, then reapplies that patch after resetting.
# You could get merge conflicts if you've modified things which were
# changed since the commit you reset to.
git rebase --no-autostash as well.git revert <sha-1>"Undo" the given commit or commit range.
The reset command will "undo" any changes made in the given commit.
A new commit with the undo patch will be committed while the original commit will remain in the history as well.
# add new commit with the undo of the original one.
# the <sha-1> can be any commit(s) or commit range
git revert <sha-1>
This schema illustrates which command does what.
As you can see there reset && checkout modify the HEAD.
In my case I committed and pushed to the wrong branch, so what I wanted was to have all my changes back so I can commit them to a new correct branch, so I did this:
On the same branch that you committed and pushed, if you type "git status" you won't see anything new because you committed and pushed, now type:
git reset --soft HEAD~1
This will get all your changes(files) back in the stage area, now to get them back in the working directory(unstage) you just type:
git reset FILE
Where "File" is the file that you want to commit again. Now, this FILE should be in the working directory(unstaged) with all the changes that you did. Now you can change to whatever branch that you want and commit the changes in that branch. Of course, the initial branch that you committed is still there with all changes, but in my case that was ok, if it is not for you-you can look for ways to revert that commit in that branch.
Undo the last commit:
git reset --soft HEAD^ or git reset --soft HEAD~
This will undo the last commit.
Here --soft means reset into staging.
HEAD~ or HEAD^ means to move to commit before HEAD.
Replace the last commit to new commit:
git commit --amend -m "message"
It will replace the last commit with the new commit.
If you are working with SourceTree, this will help you.
Right click on the commit then select "Reset (current branch)/master to this commit" and last select "Soft" reset.
To undo your local commit you use git reset <commit>. Also that tutorial is very helpful to show you how it works.
Alternatively, you can use git revert <commit>: reverting should be used when you want to add another commit that rolls back the changes (but keeps them in the project history).
Suppose you made a wrong commit locally and pushed it to a remote repository. You can undo the mess with these two commands:
First, we need to correct our local repository by going back to the commit that we desire:
git reset --hard <previous good commit id where you want the local repository to go>
Now we forcefully push this good commit on the remote repository by using this command:
git push --force-with-lease
The 'with-lease' version of the force option it will prevent accidental deletion of new commits you do not know about (i.e. coming from another source since your last pull).
VISUAL STUDIO USERS (2015, etc.)
If you cannot synchronise in Visual Studio as you are not allowed to push to a branch like "development" then as much as I tried, in Visual Studio NEITHER the REVERT NOR the RESET (hard or soft) would work.
Per the answer with TONS OF VOTES:
Use this at the command prompt of root of your project to nuke anything that will attempt to get pushed:
git reset --hard HEAD~1
Backup or zip your files just in case you don't wish to lose any work, etc...
Everybody comments in such a complicated manner.
If you want to remove the last commit from your branch, the simplest way to do it is:
git reset --hard HEAD~1
Now to actually push that change to get rid of your last commit, you have to
git push --force
And that's it. This will remove your last commit.
--hard`, but leave it with the default --soft` so we can remove and add stuff before the ``git push -f`
In speaking of Git-related commands in the previous answers, I would like to share my typical Git cycles with all readers which may helpful. Here is how I work with Git,
Cloning the first time from the remote server
git clone $project
Pulling from remote (when I don't have a pending local commit to push)
git pull
Adding a new local file1 into $to_be_committed_list (just imagine $to_be_committed_list means staged area)
git add $file1
Removing mistakenly added file2 from $to_be_committed_list (assume that file2 is added like step 3, which I didn't want)
git reset $file2
Committing file1 which is in $to_be_committed_list
git commit -m "commit message description"
Syncing local commit with remote repository before pushing
git pull --rebase
Resolving when conflict occurs prerequisite configure mergetool
git mergetool #resolve merging here, also can manually merge
Adding conflict-resolved files, let's say file1:
git add $file1
Continuing my previous rebase command
git rebase --continue
Pushing ready and already synced last local commit
git push origin head:refs/for/$branch # branch = master, dev, etc.
In these cases, the "reset" command is your best friend:
git reset --soft HEAD~1
Reset will rewind your current HEAD branch to the specified revision. In our example above, we'd like to return to the one before the current revision - effectively making our last commit undone.
Note the --soft flag: this makes sure that the changes in undone revisions are preserved. After running the command, you'll find the changes as uncommitted local modifications in your working copy.
If you don't want to keep these changes, simply use the --hard flag. Be sure to only do this when you're sure you don't need these changes anymore.
git reset --hard HEAD~1
In order to get rid of (all the changes in) last commit, last 2 commits and last n commits:
git reset --hard HEAD~1
git reset --hard HEAD~2
...
git reset --hard HEAD~n
And, to get rid of anything after a specific commit:
git reset --hard <commit sha>
e.g.,
git reset --hard 0d12345
Be careful with the hard option: it deletes the local changes in your repo as well and reverts to the previous mentioned commit. You should only run this if you are sure you messed up in your last commit(s) and would like to go back in time.
As a side-note, about 7 letters of the commit hash is enough, but in bigger projects, you may need up to 12 letters for it to be unique. You can also use the entire commit SHA if you prefer.
The above commands work in GitHub for Windows as well.
Remove a wrong commit that is already pushed to Github
git push origin +(previous good commit id):(branch name)
Please specify the last good commit id you would like to reset back in Github.
For example. If latest commit id is wrong then specify the previous commit id in above git command with the branch name.
You can get previous commit id using git log
You need to do the easy and fast
git commit --amend
if it's a private branch or
git commit -m 'Replace .class files with .java files'
if it's a shared or public branch.
I got the commit ID from bitbucket and then did:
git checkout commitID .
Example:
git checkout 7991072 .
And it reverted it back up to that working copy of that commit.
Do as the following steps.
Step 1
Hit git log
From the list of log, find the last commit hash code and then enter:
Step 2
git reset <hash code>
In order to remove some files from a Git commit, use the “git reset” command with the “–soft” option and specify the commit before HEAD.
$ git reset --soft HEAD~1
When running this command, you will be presented with the files from the most recent commit (HEAD) and you will be able to commit them.
Now that your files are in the staging area, you can remove them (or unstage them) using the “git reset” command again.
$ git reset HEAD <file>
Note: this time, you are resetting from HEAD as you simply want to exclude files from your staging area
If you are simply not interested in this file any more, you can use the “git rm” command in order to delete the file from the index (also called the staging area).
$ git rm --cached <file>
When you are done with the modifications, you can simply commit your changes again with the “–amend” option.
$ git commit --amend
To verify that the files were correctly removed from the repository, you can run the “git ls-files” command and check that the file does not appear in the file (if it was a new one of course)
$ git ls-files
<file1>
<file2>
Since Git 2.23, there is a new way to remove files from commit, but you will have to make sure that you are using a Git version greater or equal than 2.23.
$ git --version
Git version 2.24.1
Note: Git 2.23 was released in August 2019 and you may not have this version already available on your computer.
To install newer versions of Git, you can check this tutorial. To remove files from commits, use the “git restore” command, specify the source using the “–source” option and the file to be removed from the repository.
For example, in order to remove the file named “myfile” from the HEAD, you would write the following command
$ git restore --source=HEAD^ --staged -- <file>
As an example, let’s pretend that you edited a file in your most recent commit on your “master” branch.
The file is correctly committed but you want to remove it from your Git repository.
To remove your file from the Git repository, you want first to restore it.
$ git restore --source=HEAD^ --staged -- newfile
$ git status
Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits)
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: newfile
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: newfile
As you can see, your file is back to the staging area.
From there, you have two choices, you can choose to edit your file in order to re-commit it again, or to simply delete it from your Git repository.
In this section, we are going to describe the steps in order to remove the file from your Git repository.
First, you need to unstage your file as you won’t be able to remove it if it is staged.
To unstage a file, use the “git reset” command and specify the HEAD as source.
$ git reset HEAD newfile
When your file is correctly unstaged, use the “git rm” command with the “–cached” option in order to remove this file from the Git index (this won’t delete the file on disk)
$ git rm --cached newfile
rm 'newfile'
Now if you check the repository status, you will be able to see that Git staged a deletion commit.
$ git status
Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits)
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: newfile
Now that your file is staged, simply use the “git commit” with the “–amend” option in order to amend the most recent commit from your repository.
`$ git commit --amend
[master 90f8bb1] Commit from HEAD
Date: Fri Dec 20 03:29:50 2019 -0500
1 file changed, 2 deletions(-)
delete mode 100644 newfile
`As you can see, this won’t create a new commit but it will essentially modify the most recent commit in order to include your changes.
In some cases, you don’t want all the files to be staged again: you only one to modify one very specific file of your repository.
In order to remove a specific file from a Git commit, use the “git reset” command with the “–soft” option, specify the commit before HEAD and the file that you want to remove.
$ git reset HEAD^ -- <file>
When you are done with the modifications, your file will be back in the staging area.
First, you can choose to remove the file from the staging area by using the “git reset” command and specify that you want to reset from the HEAD.
$ git reset HEAD <file>
Note: it does not mean that you will lose the changes on this file, just that the file will be removed from the staging area.
If you want to completely remove the file from the index, you will have to use the “git rm” command with the “–cached” option.
$ git reset HEAD <file>
In order to make sure that your file was correctly removed from the staging area, use the “git ls-files” command to list files that belong to the index.
$ git ls-files
When you are completely done with your modifications, you can amend the commit you removed the files from by using the “git commit” command with the “–amend” option.
$ git commit --amend
git rm file` and git checkout file`?
git reset --soft HEAD~1
Reset will rewind your current HEAD branch to the specified revision.
Note the --soft flag: this makes sure that the changes in undone revisions are preserved. After running the command, you'll find the changes as uncommitted local modifications in your working copy.
If you don't want to keep these changes, simply use the --hard flag. Be sure to only do this when you're sure you don't need these changes anymore.
git reset --hard HEAD~1
Undoing Multiple Commits
git reset --hard 0ad5a7a6
Keep in mind, however, that using the reset command undoes all commits that came after the one you returned to:
OP: How do I undo the most recent local commits in Git? I accidentally committed the wrong files [as part of several commits].
There are several ways to "undo" as series of commits, depending on the outcome you're after. Considering the start case below, reset, rebase and filter-branch can all be used to rewrite your history.
How can C1 and C2 be undone to remove the tmp.log file from each commit?
In the examples below, absolute commit references are used, but it works the same way if you're more used to relative references (i.e. HEAD~2 or HEAD@{n}).
reset$ git reset --soft t56pi
With reset, a branch can be reset to a previous state, and any compounded changes be reverted to the Staging Area, from where any unwanted changes can then be discarded.
Note: As reset clusters all previous changes into the Staging Area, individual commit meta-data is lost. If this is not OK with you, chances are you're probably better off with rebase or filter-branch instead.
rebase$ git rebase --interactive t56pi
Using an interactive rebase each offending commit in the branch can be rewritten, allowing you to modify and discard unwanted changes. In the infographic above, the source tree on the right illustrates the state post rebase.
Step-by-step
t56pi)pick with edit. Save and close.HEAD, remove the unwanted files, and create brand new commits.Note: With rebase much of the commit meta data is kept, in contrast to the reset alternative above. This is most likely a preferred option, if you want to keep much of your history but only remove the unwanted files.
filter-branch$ git filter-branch --tree-filter 'rm -r ./tmp.log' t56pi..HEAD
Above command would filter out the file ./tmp.log from all commits in the desired range t56pi..HEAD (assuming our initial start case from above). See below illustration for clarity.
Similar to rebase, filter-branch can be used to wipe unwanted files from a subsection of a branch. Instead of manually editing each commit through the rebase process, filter-branch can automatically preformed the desired action on each commit.
Note: Just like rebase, filter-branch would preserve the rest of the commit meta-data, by only discarding the desired file. Notice how C1 and C2 have been rewritten, and the log-file discarded from each commit.
Just like anything related to software development, there are multiple ways to achieve the same (or similar) outcome for a give problem. You just need to pick the one most suitable for your particular case.
Do note that all three alternatives above rewrites the history completely. Unless you know exactly what you're doing and have good communication within your team - only rewrite commits that have not yet been published remotely!
Source: All examples above are borrowed from this blog.
You can undo your Git commits in two ways:
First is you can use git revert, if you want to keep your commit history:
git revert HEAD~3
git revert <hashcode of commit>
Second is you can use git reset, which would delete all your commit history and bring your head to commit where you want it.
git reset <hashcode of commit>
git reset HEAD~3
You can also use the --hard keyword if any of it starts behaving otherwise. But, I would only recommend it until it's extremely necessary.
git undo, that's it. Then the reputation git has for handling mistakes made by us mere mortals disappears. Implement by pushing the current state on a git stack before executing anygitcommand. It would affect performance, so it would be best to add a config flag as to whether to enable it.aliasfeature: git-scm.com/book/en/v2/Git-Basics-Git-Aliasesgit reflogis already close to what you describe, but gives the user more control on what's to be (un)done. But please, no, "undo" does not work the same everywhere, and people would expect many different things for the feature to achieve. Undo last commit? Undo last action? If last action was a push, undo how exactly, (reset and push) or (revert and push)?