Until you "push" local changes, they are only available to you, and there are a few different ways to revert changes in a Git repository.
Short answer: To undo local changes completely, without saving them, use: git clean -fd.
In this article, I'll talk about how to undo local changes before committing. And I'll show you two solutions for that: The first allows you to save the changes for later, and the second, completely undoes all the changes.
I assume you already have some understanding of git, on how to add files (git add), to commit (git commit), etc.
Verifying the changes
To view which files have been changed, as well as whether the files have already been added or not, use:
To see the specific changes, line by line, use:
# For all files git diff # For specific files git diff <file>
Undo local changes without losing them
If you need to switch branches or do another commit without including changes you've already done to the code, you can use the git stash command to save them for later.
The git stash command saves all current changes to files added locally and throws those changes in a stack, leaving the repository in the same state as the last commit.
For example, let's say you made changes to the index.html file, but soon after, you noticed that you actually needed to solve some problems in another branch.
You don't want to commit the index.html yet, but it would be nice to save those changes for later. This is when we use git stash:
git add index.html git stash
Changes from index.html will be saved and queued in a stash, leaving the repository in the same way it was in the last commit.
To view the changes in the stash, simply use:
git stash list
To return the stash changes back to the work tree, use:
git stash pop
There may be several modifications to stash, and they remain in this local "hideout" until you use the git stash pop command.
How to undo local changes without saving
If you want to permanently undo local changes before commit, take a look at the situations I have below, and see which one you apply:
To undo a git add:
# Undo git add . (all files) git reset # Undo git add <file> git reset <file>
Warning: this command makes you lose the changes you made to the local files.
So, to undo all local changes, returning to the state of the last local commit:
# Undo changes in a specific file git checkout -- <file> # Undo changes in all local files git reset --hard
To remove unadded (untracked) files, which have not been added through git add: (Added files will not be removed)
# For files git clean -f # For files and directories git clean -fd