I ran git status
which told me everything was up to date and there were no local changes.
Then I made several consecutive changes and realized I wanted to throw everything away and get back to my original state. Will this command do it for me?
git reset --hard HEAD
git reset --hard This command reverts the repo to the state of the HEAD revision, which is the last committed version. Git discards all the changes you made since that point. Use the checkout command with two dashes, then the path to the file for which you want to revert to its previous state.
To revert changes made to your working copy, do this:
git checkout .
Or equivalently, for git version >= 2.23:
git restore .
To revert changes made to the index (i.e., that you have added), do this. Warning this will reset all of your unpushed commits to master!:
git reset
To revert a change that you have committed:
git revert <commit 1> <commit 2>
To remove untracked files (e.g., new files, generated files):
git clean -f
Or untracked directories (e.g., new or automatically generated directories):
git clean -fd
Note: You may also want to run
git clean -fd
as
git reset --hard
will not remove untracked files, where as git-clean will remove any files from the tracked root directory that are not under git tracking. WARNING - BE CAREFUL WITH THIS! It is helpful to run a dry-run with git-clean first, to see what it will delete.
This is also especially useful when you get the error message
~"performing this command will cause an un-tracked file to be overwritten"
Which can occur when doing several things, one being updating a working copy when you and your friend have both added a new file of the same name, but he's committed it into source control first, and you don't care about deleting your untracked copy.
In this situation, doing a dry run will also help show you a list of files that would be overwritten.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With