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.
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.