Skip to content

Instantly share code, notes, and snippets.

@subfuzion
Created August 5, 2011 18:37
Show Gist options
  • Star 40 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save subfuzion/1128192 to your computer and use it in GitHub Desktop.
Save subfuzion/1128192 to your computer and use it in GitHub Desktop.
Git Tips: Reset, Clean - Remove untracked files and directories from the working tree when switching branches or checking out different commits.
git reset --hard
git clean -f -d
Description:
============
Git Tips: Remove untracked files and directories from the working
tree when switching branches or checking out different commits.
Explanation:
============
When switching branches or checking out another set of commits,
you might want to only have the files and directories that are
a part of that actual version. The commands shown above will
accomplish this.
Be warned that any untracked files will be deleted, along with
changes to tracked files. The two commands together reset the
index and working tree, so ensure that any changes you don't want
to lose were either committed to another branch or otherwise
backed up somehow.
Reference:
==========
git reset --hard
http://www.kernel.org/pub/software/scm/git/docs/git-reset.html
Resets the index and working tree. Any changes to tracked files
in the working tree since <commit> are discarded.
git clean -f -d
http://www.kernel.org/pub/software/scm/git/docs/v1.7.6/git-clean.html
-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.
-f
--force
If the git configuration variable clean.requireForce is not set to false, git clean will refuse to run unless given -f or -n.
@vibs2006
Copy link

Thanks. Really Very Helpful.

@Young-Je
Copy link

Young-Je commented Feb 5, 2018

what is the difference between git checkout . and git clean -df

@DavidChenLiang
Copy link

@Young-Je, git checkout will leave untracked files alone, they will be shown as 'untrack' in your new branch, if you run git clean -df, these untracked files/folder will be DELETED from your file system!! If you switch back to the previous branch, they are gone for good.
Hope my answer helps.

@alexmgillis
Copy link

just be careful or you will lost a lot and possibly no way to get it back. Make a backup before you do something like this.

@aws-rbs
Copy link

aws-rbs commented May 17, 2018

Cheers. Worked as decribed.

@evadeflow
Copy link

evadeflow commented Jun 25, 2018

Thanks for the pointer! My 'lazy web' searches have brought me back here a few times, so I hope you'll forgive my scribbling this Note to Self here for the next time. As @alexmgillis mentioned, it's impossible to get back uncommitted changes if you run these command by mistake, so I recommend the following alias:

[alias]                                                                                                                                                                                       
    clobber = !git stash && git reset --hard && git clean -fd

Stashing the working copy state before running git reset --hard is nearly always a good idea, since it adds any uncommitted changes to the reflog so you can retrieve them later if you regret deleting them. Here's a short demonstration:

$ vim README.md                                                                                                                                                                               
$ # make some edits
$ git clobber
Saved working directory and index state WIP on (no branch): 93bc2e2072 Merge pull request #18928 from tensorflow/release-patch-4-1
HEAD is now at 93bc2e2072 Merge pull request #18928 from tensorflow/release-patch-4-1

$ git status
HEAD detached at v1.8.0
nothing to commit, working tree clean

$ git diff stash@{0}
diff --git a/README.md b/README.md
index 5efde96d7b..99f4a253d9 100644
--- a/README.md
+++ b/README.md
@@ -96,6 +96,3 @@ Learn more about the TensorFlow community at the [community page of tensorflow.o
 ## License
 
 [Apache License 2.0](LICENSE)
-
-Very important changes that I accidentally obliterated. But it's okay! I
-stashed them first, so they can be resurrected...

@nrgapple
Copy link

This is great. I'm always resetting and cleaning

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment