do not use --hard use --soft instead.
Thus if you want to remove your latest commit you'd do:
git reset --soft HEAD^
do not use --hard use --soft instead.
Thus if you want to remove your latest commit you'd do:
git reset --soft HEAD^
While Alex is very correct, I might be tempted to try a different sequence:
If I wanted the commit on a yet-to-be-born branch:
git branch newbranch
git reset --hard HEAD^
If I wanted the commit on an existing branch:
git checkout otherbranch
git cherry-pick firstbranch
git checkout firstbranch
git reset --hard HEAD^
The full example of Alex's answer
git reset --soft HEAD^
git checkout otherbranch
git commit -am "Message"
Note the last example will fail poorly if the attempt to "float" the change to the other branch fails due to conflicts. You will then need to stash/checkout/apply to get into conflict resolution.
I accidentally typed git reset in the terminal instead of just reset. I can still see my changes though.
I read that it just moves the head but i just want to hear from someone who is sure lol.
How do I undo the last commit without losing changes?
how do I revert to a previous commit without changes for this scenario ?
Is there a way to remove all local commits while keeping the changes?
version control - How do I undo the most recent local commits in Git? - Stack Overflow
Hi guys,
I am stuck in another tricky situation.
When I discovered I have done certain commits that are wrong, then I did :
git reset --hard commitNo1
I went on to do the changes that I need to only to discover that the code is already mixed with some code-generation stuff. No choice, I then wanted to revert to a earlier version :
I did
git revert --no-commit commitEarlierNo..HEAD
following this suggestion :
https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit
and now I got this below message:
error: Your local changes to the following files would be overwritten by merge: Please commit your changes or stash them before you merge Aborting fatal: revert failed
Please help me now what can i do by aborting all the changes and I just want to revert to that even earlier version, and I am ok to discard changes I have made so far after I did that reset Hard to that commitNo1
I would like to organize my PR after my feature is done by removing all of my 'work in progress'-commits and creating new commits that group files to improve the readability of my PR.
I have tried using git reset, but that only unstages changes that have been added using git add . & all files that are already committed seem to be out of reach.
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.
Git commit only saves it to the stage, which is locally on your computer. Use Push to update it to a remote server (Like github).
Use git revert <ID> to revert back to a previous commit. each commit has an identifying code.
See here for more details on revert
The above answer is not quite correct - git revert <ID> does not set your repository to that commit -- git revert <ID> creates a new commit that undoes the changes introduced by commit <ID>. It's more or less a way to 'undo' a commit and save that undo in your history as a new commit.
If you want to set your branch to the state of a particular commit (as implied by the OP), you can use git reset <commit>, or git reset --hard <commit> The first option only updates the INDEX, leaving files in your working directory unchanged as if you had made the edits but not yet committed them. With the --hard option, it replaces the contents of your working directory with what was on <commit>.
A note of warning that git reset will alter history -- if I made several commits and then reset to the first commit, the subsequent commits will no longer be in the commit history. This can cause some serious headaches if any of those lost commits have been pushed to a public repository. Make sure you only use it to get rid of commits that haven't been pushed to another repository!
This is what you want to do:
- Add all the files, individually or in a folder, that you want to remove from the repo but keep locally to
.gitignore. - Execute
git rm --cached put/here/your/file.extfor each file orgit rm --cached folder/\\*if they are in a folder (it is/\\*because you need to escape the*) - Commit your changes.
- Push to remote.
After having done this, you will effectively "ignore tracked files without deleting them", removing them from the repo and keeping all of them untouched in your system. Then when your team pulls the changes, they will pull the .gitignore file and their individual configuration files will not be overwritten by the ones in the repo because you have ignored them and deleted them from it. Also, because now the new .gitignore is ignoring these files, everybody will have their own version of them not showing in the "uncommitted changes".
[EDIT]:
IMPORTANT NOTE!
As @ardila mentions, please note that executing this procedure will cause the deletion of the files for all those who pull these changes. To avoid this issue just make a backup of all those files you want to keep first, then perform the pull which will delete the files, and finally just restore the backed up files. Now from this point onward these files will not be erased or updated with the upstream changes on every pull (since they have been ignored already)
There seems to be an easy solution found here. Adding the file / directory to .gitignore only is not enough, but git will allow you to manually "ignore" changes to a file / directory:
git update-index --assume-unchanged <file>
And if you want to start tracking changes again, you can undo the previous command using:
git update-index --no-assume-unchanged <file>
To view files for which change tracking is disabled, you can use (linux/unix):
git ls-files -v | grep ^[h]
or (windows):
git ls-files -v | find "h "
Background: I needed this to add a configuration file with user data to the repo. The user should pull the file and edit it for his/her system, but the file should subsequently be ignored by git and never commited.