How to discard all commited changes?

Asked

Viewed 25,119 times

5

How to discard all commited changes?

Example: I have a project that has a few dozen commits, assuming that at a given moment I decide to discard everything which has not yet been confirmed via commit, how can I do this?

Note: I roughly want to undo everything until the previous commit. What would be the best way to do this?

4 answers

8

I have successfully used only these two commands below when I make several changes to a branch and wish to get rid of them all without having committed anything yet.

The first command is to reverse all changes in files that were versioned:

git checkout -- .

The according to is to delete all files and directories created:

git clean -f -d

At the last command, you can still add the -x to also delete files that were created but are being ignored by git (therefore, they do not appear in git status).

If you have added files in index (using git add), you need to apply the following command before the above commands, to remove them from index:

git reset HEAD .

6

Lists all commits you’ve ever made:

git log --stat

You’ll list your last commits, then you choose an ID, where you want to go back:

* 518ce00 
* ec3be16
* df9b821
* 8db3a02
* 698f520
* 19ccc39

Then just reset to the point you chose:

git reset --hard 19ccc39

But the ideal would be for you to work with branches, so avoid this.

5

If you didn’t git add / git commit, can simply:

git checkout .

or delete and clone the project again

  • You can also use: git checkout filename

  • This would only work for files that have changed but haven’t been committed yet

  • 1

    It’s what’s being asked, not commited files

  • Whoops, that’s right, whatever’s not committed. git checkout . reverts: changed, added and deleted files that have not been contemplated by a commit?

  • That’s right, basically undoes the design changes

0

If you want to dispose of everything permanently:

git checkout .

If you want it all back, but you might regret it:

git stash

Browser other questions tagged

You are not signed in. Login or sign up in order to post.