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
Answer from 1800 INFORMATION on Stack OverflowTo 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.
Reset all local changes - Sublime Merge - Sublime Forum
How to discard all changes so I can switch branch
Discard all changes made to a file in a branch
Is there a way to remove all local commits while keeping the changes?
I was trying to clean up git and use it in a better way, but the master is now so far from the branch I am working on, I will have to do it another way. The issue I am having is that I have made a couple of small changes just to get the site working locally to find that it is too far gone.
The issue now is git won't let me switch to a different branch without committing and I desparately do not under any circumstances want to do that. How can I just get rid of the changes, I don't need to stash them, I just need to get back to my work. In the past I have just had to delete the folder from my computer and clone it again from github, but there must be a way to just ignore the changes and checkout another branch. Any help would be appreciated I'm panicking currently and worried I'm about to lose 5 months of work because of git when I'm trying to learn how to use it.
I've tried:
git reset --hard HEAD
but that doesn't work
UPDATE: Thank you to all who responded to my post and apologies for lack of response, I have been on Annual Leave.
The upshot of all of this is that I need to learn git a lot better, so am just doing a course on linkedIn Learning so I don't get into this mess again. And for the record it just covered what I think I should have done:
"git log" - to find the last commit and copy SHA
"checkout -- SHA" - this lets git take you back to same status as last commit and reverts all unstaged changes.
There may well be a better or quicker way to do this, but this does it perfectly. And on a side note the course introduced me to the concept of atomic commits as a good practice.