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.
Does git reset remove changes made to files?
How to reset back to zero changes
command line - git undo all uncommitted or unsaved changes - Stack Overflow
git: undo all working dir changes including new files - Stack Overflow
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.
I'm new to git and not much of a programmer, but as I continue on my linux journey I'm running into git more and more. I have a repo that I cloned and selected the branch that I want. I then compiled my kernel (for a riscv sbc). Now this where I'm having trouble. I want to reset everything back to when I just downloaded the repo. I've tried git reset --hard but it does not delete files I've created. I made a test and I can't seem to delete it using git commands. I want to make sure that any other files I've created are gone. I've been just deleting the directory and re cloning it, but there has to be a better way. Thanks
This will unstage all files you might have staged with
git add:git resetThis will revert all local uncommitted changes (should be executed in repo root):
git checkout .You can also revert uncommitted changes only to particular file or directory:
git checkout [some_dir|file.txt]Yet another way to revert all uncommitted changes (longer to type, but works from any subdirectory):
git reset --hard HEADThis will remove all local untracked files, so only git tracked files remain:
git clean -fdxWARNING:
-xwill also remove all ignored files, including ones specified by.gitignore! You may want to use-nfor preview of files to be deleted.
To sum it up: executing commands below is basically equivalent to fresh git clone from original source (but it does not re-download anything, so is much faster):
git reset
git checkout .
git clean -fdx
Typical usage for this would be in build scripts, when you must make sure that your tree is absolutely clean - does not have any modifications or locally created object files or build artefacts, and you want to make it work very fast and to not re-clone whole repository every single time.
If you wish to "undo" all uncommitted changes simply run:
git stash
git stash drop
If you have any untracked files (check by running git status), these may be removed by running:
Warning: This will remove all non-commited data, even what is in .gitignore
git clean -fdx
git stash creates a new stash which will become stash@{0}. If you wish to check first you can run git stash list to see a list of your stashes. It will look something like:
stash@{0}: WIP on rails-4: 66c8407 remove forem residuals
stash@{1}: WIP on master: 2b8f269 Map qualifications
stash@{2}: WIP on master: 27a7e54 Use non-dynamic finders
stash@{3}: WIP on blogit: c9bd270 some changes
Each stash is named after the previous commit messsage.
git reset --hard # removes staged and working directory changes
## !! be very careful with these !!
## you may end up deleting what you don't want to
## read comments and manual.
git clean -f -d # remove untracked
git clean -f -x -d # CAUTION: as above but removes ignored files like config.
git clean -fxd :/ # CAUTION: as above, but cleans untracked and ignored files through the entire repo (without :/, the operation affects only the current directory)
To see what will be deleted before-hand, without actually deleting it, use the -n flag (this is basically a test-run). When you are ready to actually delete, then remove the -n flag:
git clean -nfd
Safest method, which I use frequently:
git clean -fd
Syntax explanation as per /docs/git-clean page:
-f(alias:--force). If the Git configuration variable clean.requireForce is not set to false, git clean will refuse to delete files or directories unless given -f, -n or -i. Git will refuse to delete directories with .git sub directory or file unless a second -f is given.-d. Remove untracked directories in addition to untracked files. If an untracked directory is managed by a different Git repository, it is not removed by default. Use -f option twice if you really want to remove such a directory.
As mentioned in the comments, it might be preferable to do a git clean -nd which does a dry run and tells you what would be deleted before actually deleting it.
Link to git clean doc page:
https://git-scm.com/docs/git-clean