How to revert git uncommitted changes including files and folders

Undo git changes before add and commit

First read two of my new posts in this regard:

You can list the files that will be reverted without actually making any action, just to check what will happen, with:

git checkout --

Then if you want to undo / revert the changes only in current working directory, use

git checkout -- .

Maybe you wan to know how

git reset --hard

works? It will undo both staged and unstaged changes, whereas ‘git checkout — .’ will undo only unstaged changes.


git reset --hard

will revert changes to modified files. and the following command will remove all untracked files and directories. (`-f` is `force`, `-d` is `remove directories`)

git clean -fd

It is good idea to run

git clean -nd

to preview the changes before running git clean to ensure you don’t have untracked files or directories that you care about that will be removed. -n is dry run which shows output without doing anything yet.

Just think about a scenario:

You have committed a git, then you want to undo it. In addition, you have some modifications that you don’t want them. So there is two tasks.

for undoing the last commit, you can use this tutorial:

but don’t forget you have also some modifications that you don’t need them so you can use

git clean -nd
git clean -fd

read more here

Posting daily about Python, Nuclear Physicist PhD