Commits / Reset and clean

git reset and git clean -n

When you have a messy working directory, full of changes that you want to get rid of, you can reset to return to a known state.


Add a change to our workshop repository, and see the unstaged changes

$ echo 6 >> file
$ git diff file
diff --git a/file b/file
index 8a1218a..119d9e8 100644
--- a/file
+++ b/file
@@ -3,3 +3,4 @@

Great. So to return to HEAD, we can reset there.

git reset --hard HEAD

Remember about the HEAD reference? This reset moves our working tree back to that reference, discarding all unstaged changes. This deletes files and directories so use with care.

Resetting to origin branches

Skipping forward a little bit here, but you can give any ref to reset to, and git will do the rest. This is equally powerful and terrifying, as you can lose work and commits this way. Later in the workshop we’ll go through how to recover missing commits if you do screw something up.

git checkout master
git reset origin/master --hard

This will undo all of our local commits to the master branch and reset HEAD to point to where ever origin/master is pointing to.


The clean utility just removes untracked files, i.e. ones haven’t been added as a commit.

To see what files would be removed (without actually removing anything)

git clean -n

And to remove the files:

git clean -f

To remove files and directories:

git clean -d -n/-f

This is not reversible, so be careful.

SCARY STUFF! Now I’ve shown you how to lose your work, let’s keep going! Next up is merging branches