<p>How do I delete untracked local files from the current working tree?</p>

[{'answer_id': 61213, 'author': 'Espo', 'author_id': 2257, 'author_profile': 'https://Stackoverflow.com/users/2257', 'pm_score': 7, 'selected': False, 'text': '<p><a href="http://www.kernel.org/pub/software/scm/git/docs/git-clean.html" rel="noreferrer">git-clean</a> is what you are looking for. It is used to remove untracked files from the working tree.</p>\n'}, {'answer_id': 61214, 'author': 'Fire Lancer', 'author_id': 6266, 'author_profile': 'https://Stackoverflow.com/users/6266', 'pm_score': 9, 'selected': False, 'text': '<p><a href="http://git-scm.com/docs/git-clean" rel="noreferrer"><code>git-clean</code> - Remove untracked files from the working tree</a></p>\n'}, {'answer_id': 64966, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 14, 'selected': True, 'text': '<blockquote>\n<h2><a href="https://git-scm.com/docs/git-clean" rel="noreferrer">git-clean</a> - Remove untracked files from the working tree</h2>\n<h3>Synopsis</h3>\n<pre><code>git clean [-d] [-f] [-i] [-n] [-q] [-e &lt;pattern&gt;] [-x | -X] [--] &lt;path&gt;…\u200b\n</code></pre>\n<h3>Description</h3>\n<p>Cleans the working tree by recursively removing files that are not under version control, <strong>starting from the current directory</strong>.</p>\n<p>Normally, only files unknown to Git are removed, but if the <code>-x</code> option is specified, ignored files are also removed. This can, for example, be useful to remove all build products.</p>\n<p>If any optional <code>&lt;path&gt;...</code> arguments are given, only those paths are affected.</p>\n</blockquote>\n<hr />\n<p>Step 1 is to show what will be deleted by using the <code>-n</code> option:</p>\n<pre><code># Print out the list of files and directories which will be removed (dry run)\ngit clean -n -d\n</code></pre>\n<p>Clean Step - <strong>beware: this will delete files</strong>:</p>\n<pre><code># Delete the files from the repository\ngit clean -f\n</code></pre>\n<ul>\n<li>To remove directories, run <code>git clean -f -d</code> or <code>git clean -fd</code></li>\n<li>To remove ignored files, run <code>git clean -f -X</code> or <code>git clean -fX</code></li>\n<li>To remove ignored and non-ignored files, run <code>git clean -f -x</code> or <code>git clean -fx</code></li>\n</ul>\n<p><strong>Note</strong> the case difference on the <code>X</code> for the two latter commands.</p>\n<p>If <code>clean.requireForce</code> is set to &quot;true&quot; (the default) in your configuration, one needs to specify <code>-f</code> otherwise nothing will actually happen.</p>\n<p>Again see the <a href="http://git-scm.com/docs/git-clean" rel="noreferrer"><code>git-clean</code></a> docs for more information.</p>\n<hr />\n<blockquote>\n<h3>Options</h3>\n<p><strong><code>-f</code>, <code>--force</code></strong></p>\n<p>If the Git configuration variable clean.requireForce is not set to\nfalse, git clean will refuse to run unless given <code>-f</code>, <code>-n</code> or <code>-i</code>.</p>\n<p><strong><code>-x</code></strong></p>\n<p>Don’t use the standard ignore rules read from .gitignore (per\ndirectory) and <code>$GIT_DIR/info/exclude</code>, but do still use the ignore\nrules given with <code>-e</code> options. This allows removing all untracked files,\nincluding build products. This can be used (possibly in conjunction\nwith git reset) to create a pristine working directory to test a clean\nbuild.</p>\n<p><strong><code>-X</code></strong></p>\n<p>Remove only files ignored by Git. This may be useful to rebuild\neverything from scratch, but keep manually created files.</p>\n<p><strong><code>-n</code>, <code>--dry-run</code></strong></p>\n<p>Don’t actually remove anything, just show what would be done.</p>\n<p><strong><code>-d</code></strong></p>\n<p>Remove untracked directories in addition to untracked files. If an\nuntracked directory is managed by a different Git repository, it is\nnot removed by default. Use <code>-f</code> option twice if you really want to\nremove such a directory.</p>\n</blockquote>\n'}, {'answer_id': 912737, 'author': 'robert.berger', 'author_id': 108743, 'author_profile': 'https://Stackoverflow.com/users/108743', 'pm_score': 10, 'selected': False, 'text': '<p>Use <code>git clean -f -d</code> to make sure that <strong>directories</strong> are also removed.</p>\n\n<ol>\n<li><p>Don’t actually remove anything, just show what would be done.</p>\n\n<pre><code>git clean -n\n</code></pre>\n\n<p>or </p>\n\n<pre><code>git clean --dry-run\n</code></pre></li>\n<li><p>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 the <code>-f</code> option twice if you really want to remove such a directory.</p>\n\n<pre><code>git clean -fd\n</code></pre></li>\n</ol>\n\n<p>You can then check if your files are really gone with <code>git status</code>.</p>\n'}, {'answer_id': 14521765, 'author': 'Michał Szajbe', 'author_id': 369894, 'author_profile': 'https://Stackoverflow.com/users/369894', 'pm_score': 8, 'selected': False, 'text': '<p>If untracked directory is a git repository of its own (e.g. submodule), you need to use <code>-f</code> twice:</p>\n\n<p><code>git clean -d -f -f</code></p>\n'}, {'answer_id': 18974433, 'author': 'Vijay C', 'author_id': 255721, 'author_profile': 'https://Stackoverflow.com/users/255721', 'pm_score': 7, 'selected': False, 'text': '<p>If needed to remove untracked files from particular subdirectory,</p>\n\n<pre><code>git clean -f {dir_path}\n</code></pre>\n\n<p>And combined way to delete untracked dir/files and ignored files. </p>\n\n<pre><code>git clean -fxd {dir_path}\n</code></pre>\n\n<p>after this you will have modified files only in <code>git status</code>.</p>\n'}, {'answer_id': 20195320, 'author': 'Oscar Fraxedas', 'author_id': 1074245, 'author_profile': 'https://Stackoverflow.com/users/1074245', 'pm_score': 7, 'selected': False, 'text': '<p>This is what I always use:</p>\n\n<pre><code>git clean -fdx\n</code></pre>\n\n<p>For a very large project you might want to run it a couple of times.</p>\n'}, {'answer_id': 20846779, 'author': 'SystematicFrank', 'author_id': 253098, 'author_profile': 'https://Stackoverflow.com/users/253098', 'pm_score': 9, 'selected': False, 'text': '<p>I am surprised nobody mentioned this before:</p>\n\n<pre><code>git clean -i\n</code></pre>\n\n<p>That stands for <em>interactive</em> and you will get a quick overview of what is going to be deleted offering you the possibility to include/exclude the affected files. Overall, still faster than running the mandatory <code>--dry-run</code> before the real cleaning.</p>\n\n<p>You will have to toss in a <code>-d</code> if you also want to take care of empty folders. At the end, it makes  for a nice alias:</p>\n\n<pre><code>git iclean\n</code></pre>\n\n<p>That being said, the extra hand holding of interactive commands can be tiring for experienced users.  These days I just use the already mentioned <code>git clean -fd</code> </p>\n'}, {'answer_id': 21057032, 'author': 'hiroshi', 'author_id': 338986, 'author_profile': 'https://Stackoverflow.com/users/338986', 'pm_score': 7, 'selected': False, 'text': '<p>I like <code>git stash push -u</code> because you can undo them all with <code>git stash pop</code>.</p>\n\n<p>EDIT: Also I found a way to show untracked file in a stash (e.g. <code>git show stash@{0}^3</code>) <a href="https://stackoverflow.com/a/12681856/338986">https://stackoverflow.com/a/12681856/338986</a></p>\n\n<p>EDIT2: <code>git stash save</code> is deprecated in favor of <code>push</code>. Thanks @script-wolf.</p>\n'}, {'answer_id': 28082580, 'author': 'Pooja', 'author_id': 3446107, 'author_profile': 'https://Stackoverflow.com/users/3446107', 'pm_score': 6, 'selected': False, 'text': '<p><code>git clean -fd</code> removes directory</p>\n\n<p><code>git clean -fX</code> removes ignored files</p>\n\n<p><code>git clean -fx</code> removes ignored and un-ignored files</p>\n\n<p>can be used all above options in combination as </p>\n\n<p><code>git clean -fdXx</code> </p>\n\n<p>check git manual for more help</p>\n'}, {'answer_id': 29667299, 'author': 'Chhabilal', 'author_id': 2554162, 'author_profile': 'https://Stackoverflow.com/users/2554162', 'pm_score': 5, 'selected': False, 'text': '<p>A better way is to use: git clean</p>\n\n<pre><code>git clean -d -x -f\n</code></pre>\n\n<p>This removes untracked files, including directories <code>(-d)</code> and files ignored by <code>git (-x)</code>.</p>\n\n<p>Also, replace the <code>-f</code> argument with <code>-n</code> to perform a <code>dry-run</code> or <code>-i</code> for interactive mode and it will tell you what will be removed.</p>\n'}, {'answer_id': 34049725, 'author': 'Nikita Leonov', 'author_id': 723050, 'author_profile': 'https://Stackoverflow.com/users/723050', 'pm_score': 5, 'selected': False, 'text': "<p><code>git clean -f -d -x $(git rev-parse --show-cdup)</code> applies clean to the root directory, no matter where you call it within a repository directory tree. I use it all the time as it does not force you to leave the folder where you working now and allows to clean &amp; commit right from the place where you are.</p>\n\n<p>Be sure that flags <code>-f</code>, <code>-d</code>, <code>-x</code> match your needs:</p>\n\n<pre><code>-d\n       Remove untracked directories in addition to untracked files. If an\n       untracked directory is managed by a different Git repository, it is\n       not removed by default. Use -f option twice if you really want to\n       remove such a directory.\n\n-f, --force\n       If the Git configuration variable clean.requireForce is not set to\n       false, git clean will refuse to delete files or directories unless\n       given -f, -n or -i. Git will refuse to delete directories with .git\n       sub directory or file unless a second -f is given. This affects\n       also git submodules where the storage area of the removed submodule\n       under .git/modules/ is not removed until -f is given twice.\n\n-x\n       Don't use the standard ignore rules read from .gitignore (per\n       directory) and $GIT_DIR/info/exclude, but do still use the ignore\n       rules given with -e options. This allows removing all untracked\n       files, including build products. This can be used (possibly in\n       conjunction with git reset) to create a pristine working directory\n       to test a clean build.\n</code></pre>\n\n<p>There are other flags as well available, just check <code>git clean --help</code>.</p>\n"}, {'answer_id': 35427633, 'author': 'Omar Mowafi', 'author_id': 1350159, 'author_profile': 'https://Stackoverflow.com/users/1350159', 'pm_score': 4, 'selected': False, 'text': '<p>To know what will be deleted before actually deleting:</p>\n\n<p><code>git clean -d -n</code></p>\n\n<p>It will output something like:</p>\n\n<p><strong>Would remove sample.txt</strong></p>\n\n<p>To delete everything listed in the output of the previous command:</p>\n\n<p><code>git clean -d -f</code></p>\n\n<p>It will output something like:</p>\n\n<p><strong>Removing sample.txt</strong></p>\n'}, {'answer_id': 35539401, 'author': 'thybzi', 'author_id': 3027390, 'author_profile': 'https://Stackoverflow.com/users/3027390', 'pm_score': 5, 'selected': False, 'text': '<p>A lifehack for such situation I just invented and tried (that works perfectly):</p>\n\n<pre><code>git add .\ngit reset --hard HEAD\n</code></pre>\n\n<p><strong>Beware!</strong> Be sure to <strong>commit any needed changes</strong> (even in non-untracked files) <strong>before performing this</strong>.</p>\n'}, {'answer_id': 35737150, 'author': 'JD Brennan', 'author_id': 304712, 'author_profile': 'https://Stackoverflow.com/users/304712', 'pm_score': 4, 'selected': False, 'text': '<p>If you just want to delete the files listed as untracked by \'git status\'</p>\n\n<pre><code>git stash save -u\ngit stash drop "stash@{0}"\n</code></pre>\n\n<p>I prefer this to \'git clean\' because \'git clean\' will delete files\nignored by git, so your next build will have to rebuild everything\nand you may lose your IDE settings too.</p>\n'}, {'answer_id': 37614185, 'author': 'Thanga', 'author_id': 5678086, 'author_profile': 'https://Stackoverflow.com/users/5678086', 'pm_score': 9, 'selected': False, 'text': '<h2>Simple Way to remove untracked files</h2>\n\n<p>To remove all untracked files, The simple\nway is to <strong>add all of them first</strong> and <strong>reset the repo</strong> as below</p>\n\n<pre><code>git add --all\ngit reset --hard HEAD\n</code></pre>\n\n<hr>\n'}, {'answer_id': 38978877, 'author': 'rahul286', 'author_id': 156336, 'author_profile': 'https://Stackoverflow.com/users/156336', 'pm_score': 5, 'selected': False, 'text': '<p>For me only following worked:</p>\n\n<pre><code>git clean -ffdx\n</code></pre>\n\n<p>In all other cases, I was getting message <em>"Skipping Directory"</em> for some subdirectories. </p>\n'}, {'answer_id': 39968630, 'author': 'kujiy', 'author_id': 5815086, 'author_profile': 'https://Stackoverflow.com/users/5815086', 'pm_score': 4, 'selected': False, 'text': "<p>Normal <code>git clean</code> command doesn't remove untracked files with my <code>git version 2.9.0.windows.1</code>.</p>\n\n<pre><code>$ git clean -fdx     # doesn't remove untracked files\n$ git clean -fdx *   # Append star then it works!\n</code></pre>\n"}, {'answer_id': 40235858, 'author': 'Gaurav', 'author_id': 3809978, 'author_profile': 'https://Stackoverflow.com/users/3809978', 'pm_score': 4, 'selected': False, 'text': '<p>To remove the untracked files you should first use command to view the files that will be affected by cleaning </p>\n\n<pre><code>git clean -fdn\n</code></pre>\n\n<p>This will show you the list of files that will be deleted. Now to actually delete those files use this command:</p>\n\n<pre><code>git clean -fd\n</code></pre>\n'}, {'answer_id': 41187216, 'author': 'Gnanasekar S', 'author_id': 6859356, 'author_profile': 'https://Stackoverflow.com/users/6859356', 'pm_score': 2, 'selected': False, 'text': '<p><em>Note: First navigate to the directory and checkout the branch you want to clean.</em></p>\n\n<p><code>-i</code> interactive mode and it will tell you what will be removed and you can choose an action from the list.</p>\n\n<ol>\n<li><p>To clean <strong>files</strong> only [Folders will not be listed and will not be cleaned]: \n<code>$ git clean -i</code></p></li>\n<li><p>To clean <strong>files and folders</strong>:\n<code>$ git clean -d -i</code></p></li>\n</ol>\n\n<p><code>-d</code> including directories.</p>\n\n<hr>\n\n<p>If you choose <code>c</code> from the list. The files/folders will be deleted that are not tracked and will also remove files/folders that you mess-up.*</p>\n\n<p>For instance: If you restructure the folder in your remote and pull the changes to your local computer. files/folders that are created by others initially will be in past folder and in the new one that you restructure.</p>\n'}, {'answer_id': 42185640, 'author': 'Shital Shah', 'author_id': 207661, 'author_profile': 'https://Stackoverflow.com/users/207661', 'pm_score': 7, 'selected': False, 'text': "<p><strong>Remove all extra folders and files in this repo + submodules</strong></p>\n\n<p>This gets you in same state as fresh clone.</p>\n\n<pre><code>git clean -ffdx\n</code></pre>\n\n<p><strong>Remove all extra folders and files in this repo but not its submodules</strong></p>\n\n<pre><code>git clean -fdx\n</code></pre>\n\n<p><strong>Remove extra folders but not files (ex. build or logs folder)</strong></p>\n\n<pre><code>git clean -fd\n</code></pre>\n\n<p><strong>Remove extra folders + ignored files (but not newly added files)</strong></p>\n\n<p>If file wasn't ignored and not yet checked-in then  it stays. Note the capital X.</p>\n\n<pre><code>git clean -fdX\n</code></pre>\n\n<p><strong>New interactive mode</strong></p>\n\n<pre><code>git clean\n</code></pre>\n"}, {'answer_id': 42269293, 'author': 'Vaisakh VM', 'author_id': 1905008, 'author_profile': 'https://Stackoverflow.com/users/1905008', 'pm_score': 4, 'selected': False, 'text': '<p><code>git clean -f to remove untracked files from working directory.</code></p>\n\n<p>I have covered some basics here in my blog, <a href="https://vaisakh.github.io/2017/02/14/git-intro-basic-commands.html" rel="noreferrer">git-intro-basic-commands</a></p>\n'}, {'answer_id': 42564993, 'author': 'bit_cracker007', 'author_id': 1098479, 'author_profile': 'https://Stackoverflow.com/users/1098479', 'pm_score': 5, 'selected': False, 'text': '<p><strong><em>User interactive approach:</em></strong>    </p>\n\n<pre><code>git clean -i -fd\n\nRemove .classpath [y/N]? N\nRemove .gitignore [y/N]? N\nRemove .project [y/N]? N\nRemove .settings/ [y/N]? N\nRemove src/com/arsdumpgenerator/inspector/ [y/N]? y\nRemove src/com/arsdumpgenerator/manifest/ [y/N]? y\nRemove src/com/arsdumpgenerator/s3/ [y/N]? y\nRemove tst/com/arsdumpgenerator/manifest/ [y/N]? y\nRemove tst/com/arsdumpgenerator/s3/ [y/N]? y\n</code></pre>\n\n<p>-i for interactive<br>\n-f for force<br>\n-d for directory<br>\n-x for ignored files(add if required)<br><br>\n<strong>Note:</strong> <em>Add <strong>-n</strong> or <strong>--dry-run</strong> to just check what it will do.</em></p>\n'}, {'answer_id': 45220636, 'author': 'Alireza', 'author_id': 5423108, 'author_profile': 'https://Stackoverflow.com/users/5423108', 'pm_score': 6, 'selected': False, 'text': '<p><strong>OK,</strong> deleting <strong>unwanted untracked files and folders</strong> are easy using <code>git</code> in command line, just do it like this:</p>\n\n<pre><code>git clean -fd\n</code></pre>\n\n<p><strong>Double check</strong> before doing it as it will delete the files and folders without making any history...</p>\n\n<p>Also in this case, <code>-f</code> stands for force and <code>-d</code> stands for directory...</p>\n\n<p>So, if you want to delete files only, you can use <code>-f</code> only:</p>\n\n<pre><code>git clean -f\n</code></pre>\n\n<p>If you want to <strong>delete</strong>(directories) and files, you can delete only untracked directories and files like this:</p>\n\n<pre><code>git clean -fd\n</code></pre>\n\n<p>Also, you can use <code>-x</code> flag for including the files which are ignored by git. This would be helpful if you want to delete everything.</p>\n\n<p>And adding <code>-i</code> flag, makes git asking you for permission for deleting files one by one on the go.</p>\n\n<p>If you not sure and want to check things first, add <code>-n</code> flag.</p>\n\n<p>Use <code>-q</code> if you don\'t want to see any report after successful deletion.</p>\n\n<p>I also create the image below to make it more memorable, especially I have seen many people confuse <code>-f</code> for cleaning folder sometimes or mix it up somehow!</p>\n\n<p><br>\n<a href="https://i.stack.imgur.com/3W563.png" rel="noreferrer"><img src="https://i.stack.imgur.com/3W563.png" alt="deleting unwanted untracked files and folder"></a> </p>\n'}, {'answer_id': 45558196, 'author': 'Vivek', 'author_id': 4199462, 'author_profile': 'https://Stackoverflow.com/users/4199462', 'pm_score': 0, 'selected': False, 'text': '<p>use <code>git reset HEAD &lt;file&gt;</code> to unstage a file</p>\n'}, {'answer_id': 45994347, 'author': 'Mohideen bin Mohammed', 'author_id': 4453737, 'author_profile': 'https://Stackoverflow.com/users/4453737', 'pm_score': 4, 'selected': False, 'text': '<p>uggested Command for <strong>Removing Untracked Files from git docs</strong> is <strong>git clean</strong> </p>\n\n<p><strong>git clean</strong> - Remove untracked files from the working tree</p>\n\n<p><strong>Suggested Method:</strong>  Interative Mode by using <code>git clean -i</code>\nso we can have control over it. let see remaining available options.</p>\n\n<p><strong>Available Options:</strong></p>\n\n<pre><code>git clean \n    -d -f -i -n -q -e -x -X (can use either)\n</code></pre>\n\n<p><strong>Explanation:</strong></p>\n\n<p><strong>1.</strong> <strong>-d</strong></p>\n\n<p>Remove untracked directories in addition to untracked files. If an untracked directory is managed by a different Git repository,\n   it is not removed by default. Use -f option twice if you really want to remove such a directory.</p>\n\n<p><strong>2. -f, --force</strong></p>\n\n<p>If the Git configuration variable clean.requireForce is not set to false, git clean will refuse to run unless given -f, -n or\n   -i.</p>\n\n<p><strong>3. -i, --interactive</strong></p>\n\n<p>Show what would be done and clean files interactively. See “Interactive mode” for details.</p>\n\n<p><strong>4. -n, --dry-run</strong></p>\n\n<p>Don’t actually remove anything, just show what would be done.</p>\n\n<p><strong>5. -q, --quiet</strong></p>\n\n<p>Be quiet, only report errors, but not the files that are successfully removed.</p>\n\n<p><strong>6. -e , --exclude=</strong></p>\n\n<p>In addition to those found in .gitignore (per directory) and $GIT_DIR/info/exclude, also consider these patterns to be in the\n   set of the ignore rules in effect.</p>\n\n<p><strong>7. -x</strong></p>\n\n<p>Don’t use the standard ignore rules read from .gitignore (per directory) and $GIT_DIR/info/exclude, but do still use the ignore\n   rules given with -e options. This allows removing all untracked files, including build products. This can be used (possibly in\n   conjunction with git reset) to create a pristine working directory to test a clean build.</p>\n\n<p><strong>8. -X</strong></p>\n\n<p>Remove only files ignored by Git. This may be useful to rebuild everything from scratch, but keep manually created files.</p>\n'}, {'answer_id': 46409906, 'author': 'Sergey', 'author_id': 739731, 'author_profile': 'https://Stackoverflow.com/users/739731', 'pm_score': 3, 'selected': False, 'text': '<blockquote>\n  <h2><strong>Clean out git repository and all submodules recursively</strong></h2>\n  \n  <p>The following command will clean out\n  the current git repository and all its submodules recursively:</p>\n\n<pre><code>(git clean -d -x -f &amp;&amp; git submodule foreach --recursive git clean -d -x -f)\n</code></pre>\n</blockquote>\n'}, {'answer_id': 46868431, 'author': 'DevWL', 'author_id': 2179965, 'author_profile': 'https://Stackoverflow.com/users/2179965', 'pm_score': 7, 'selected': False, 'text': '<h1>Be careful while running `git clean` command.</h1>\n<h2><strong>Always use</strong> <code>-n</code> first</h2>\n<p>Always use <code>-n</code> before running the clean command as it will show you what files would get removed.</p>\n<p><code>-d</code> Normally, when no  is specified, git clean will not recurse into untracked directories to avoid removing too much. Specify -d to have it recurse into such directories as well. If any paths are specified, -d is irrelevant; all untracked files matching the specified paths (with exceptions for nested git directories mentioned under --force) will be removed.</p>\n<p><code>-f</code> | <code>--force</code>\nIf the Git configuration variable clean.requireForce is not set to false, git clean will refuse to delete files or directories unless given -f or -i. Git will refuse to modify untracked nested git repositories (directories with a .git subdirectory) unless a second -f is given.</p>\n<pre><code>git clean -n -d \ngit clean -n -d -f\n</code></pre>\n<p>Now run without <code>-n</code> if output was what you intend to remove.</p>\n<pre><code>git clean -d -f\n</code></pre>\n<p>By default, <code>git clean</code> will only remove untracked files that are not ignored. Any file that matches a pattern in your .gitignore or other ignore files will not be removed. If you want to remove those files too, you can add a <code>-x</code> to the clean command.</p>\n<pre><code>git clean -f -d -x\n</code></pre>\n<p>There is also interactive mode available <code>-i</code> with the clean command</p>\n<pre><code>git clean -x -i\n</code></pre>\n<h1>Alternatively</h1>\nIf you are not 100% sure that deleting your uncommitted work is safe, you could use stashing instead\n<pre><code>git stash --all\n</code></pre>\n<p>Before you use <code>stash --all</code> note:\nIf the <code>--all</code> option is used, then the <strong>ignored files</strong> are stashed and cleaned in addition to the <strong>untracked files</strong>.</p>\n<pre><code>git stash push --keep-index\n</code></pre>\n<p>If the --keep-index option is used, all changes already added to the index are left intact. Your staged changes remain in your workspace, but at the same time, they are also saved into your stash.</p>\n<p>Calling <code>git stash</code> without any arguments is equivalent to <code>git stash push</code>.</p>\n<pre><code>git stash push -m &quot;name your stash&quot; // before git stash save (deprecated)\n</code></pre>\n<p>Stashing based on the used flags can clear your directory from unstaged / staged files by writing them to stash storage. I give’s flexibility to retrieve the files at any point in time using <strong>stash</strong> with <strong>apply</strong> or <strong>pop</strong>. Then if you are fine with removing your stashed files you could run:</p>\n<pre><code>git stash drop // or clean\n</code></pre>\n<p>To see full instruction on how to work with stash see this <a href="https://stackoverflow.com/questions/11269256/how-to-name-and-retrieve-a-stash-by-name-in-git">How to name and retrieve a stash by name in git?</a></p>\n<p><a href="https://i.stack.imgur.com/G356U.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/G356U.jpg" alt="enter image description here" /></a></p>\n'}, {'answer_id': 47070614, 'author': 'Jämes', 'author_id': 2780334, 'author_profile': 'https://Stackoverflow.com/users/2780334', 'pm_score': 3, 'selected': False, 'text': "<p><strong>oh-my-zsh</strong> with <strong>zsh</strong> provides those great aliases via the git plugin. They can be used in bash as well.</p>\n\n<p><code>gclean='git clean -fd'</code><br>\n<code>gpristine='git reset --hard &amp;&amp; git clean -dfx'</code></p>\n\n<ul>\n<li><code>gclean</code> <em>removes untracked directories in addition to untracked files</em>.</li>\n<li><code>gpristine</code> hard reset the local changes, remove untracked directories, \nuntracked files and <em>don't use the standard ignore rules read from .gitignore (per directory) and $GIT_DIR/info/exclude, but do still use the ignore rules given with -e options. This allows removing all untracked files, including build products. This can be used (possibly in conjunction with git reset) to create a pristine working directory to test a clean build</em>.</li>\n</ul>\n"}, {'answer_id': 47627685, 'author': 'Elangovan', 'author_id': 2614459, 'author_profile': 'https://Stackoverflow.com/users/2614459', 'pm_score': 3, 'selected': False, 'text': '<p>We can easily removed local untracked files from the current git working tree by using below git comments.</p>\n\n<pre><code>git reset [--soft | --mixed [-N] | --hard | --merge | --keep] [-q] [&lt;commit&gt;]\n</code></pre>\n\n<p><strong>Example:</strong></p>\n\n<pre><code>git reset --hard HEAD\n</code></pre>\n\n<p><strong>Links :</strong></p>\n\n<ol>\n<li><a href="https://git-scm.com/docs/git-reset" rel="noreferrer">https://git-scm.com/docs/git-reset</a></li>\n<li><a href="https://stackoverflow.com/questions/9529078/how-do-i-use-git-reset-hard-head-to-revert-to-a-previous-commit">How do I use &#39;git reset --hard HEAD&#39; to revert to a previous commit?</a></li>\n<li><a href="https://stackoverflow.com/questions/1628088/reset-local-repository-branch-to-be-just-like-remote-repository-head">Reset local repository branch to be just like remote repository HEAD</a></li>\n<li><a href="https://jwiegley.github.io/git-from-the-bottom-up/3-Reset/4-doing-a-hard-reset.html" rel="noreferrer">https://jwiegley.github.io/git-from-the-bottom-up/3-Reset/4-doing-a-hard-reset.html</a> </li>\n</ol>\n'}, {'answer_id': 50404741, 'author': 'Sudhir Vishwakarma', 'author_id': 493356, 'author_profile': 'https://Stackoverflow.com/users/493356', 'pm_score': 3, 'selected': False, 'text': '<pre><code>git clean -f\n</code></pre>\n\n<p>will remove the untracked files from the current git </p>\n\n<pre><code>git clean -fd\n</code></pre>\n\n<p>when you want to remove directories and files, this will delete only untracked directories and files</p>\n'}, {'answer_id': 51873385, 'author': 'Gediminas', 'author_id': 1412149, 'author_profile': 'https://Stackoverflow.com/users/1412149', 'pm_score': 2, 'selected': False, 'text': "<blockquote>\n  <p>I haved failed using most popular answers here - git doesn't delete\n  untracked files from the repository anyway. No idea why. This is my\n  super simplified answer without SPECIAL GIT COMMANDS!</p>\n</blockquote>\n\n<p>Mission: delete untracked files from git repository:</p>\n\n<ol>\n<li>Move files and folders elsewhere from your local project folder for a while</li>\n<li>Delete all lines in .gitignore about these files and folders for the commit</li>\n<li>Git add . </li>\n<li>Git commit -m “Cleaning repository from untracked files”</li>\n<li>Git push</li>\n</ol>\n\n<p>All files and folders has been deleted from the repository. </p>\n\n<p>Lets restore them on localhost if you need them:</p>\n\n<ol>\n<li>Move back all files and folders you have moved temporary to the local project folder again</li>\n<li>Move back all lines about these files and folders to .gitignore</li>\n<li>Git add .</li>\n<li>Git commit -m “Checking or files not appearing again in git repository”</li>\n<li>Git push</li>\n</ol>\n\n<p>You are done!</p>\n"}, {'answer_id': 53244015, 'author': 'Zia', 'author_id': 2931121, 'author_profile': 'https://Stackoverflow.com/users/2931121', 'pm_score': 2, 'selected': False, 'text': '<p>I like to use <code>git stash</code> command, later you can get stashed files and changes.\n<code>git clean</code> is also a good option but totally depends on your requirement.\nhere is the explanation of git stash and git clean,<a href="https://git-scm.com/book/en/v2/Git-Tools-Stashing-and-Cleaning" rel="nofollow noreferrer">7.3 Git Tools - Stashing and Cleaning</a></p>\n'}, {'answer_id': 55831043, 'author': 'victorm1710', 'author_id': 680032, 'author_profile': 'https://Stackoverflow.com/users/680032', 'pm_score': 1, 'selected': False, 'text': '<p>If nothing else works, to simply remove all the changes listed by the "git status" command one can use the following combo:</p>\n\n<p><code>git add -A &amp;&amp; git commit -m temp &amp;&amp; git reset --hard HEAD^</code></p>\n\n<p>This will first stage all of your changes then create a temporary commit and then discard it.</p>\n'}, {'answer_id': 56520858, 'author': 'Thirdy', 'author_id': 5118429, 'author_profile': 'https://Stackoverflow.com/users/5118429', 'pm_score': -1, 'selected': False, 'text': '<p>I use this:</p>\n\n<ol>\n<li><code>git status</code></li>\n<li>copy the path of the file</li>\n<li><code>rm &lt;path of file&gt;</code></li>\n</ol>\n\n<p>My project has a lot of generated files created by a giant ANT build script. Using <code>git clean</code> would create chaos.</p>\n'}, {'answer_id': 59435746, 'author': 'ideasman42', 'author_id': 432509, 'author_profile': 'https://Stackoverflow.com/users/432509', 'pm_score': 0, 'selected': False, 'text': '<p>This can be done using a shell script, I use this scrtipt that lists what will be removed, then lets me confirm the operation.</p>\n\n<p>This is useful since I sometimes have patches or other files I\'d like to check on before wiping everything away.</p>\n\n<pre class="lang-sh prettyprint-override"><code>#!/bin/bash\nreadarray -t -d \'\' FILES &lt; &lt;(git ls-files -z --other --directory)\nif [ "$FILES" = "" ]; then\n    echo  "Nothing to clean!"\n    exit 0\nfi\necho -e "Dirty files:\\n"\nprintf \'  %s\\n\' "${FILES[@]}"\nDO_REMOVE=0\nwhile true; do\n    echo ""\n    read -p "Remove ${#FILES[@]} files? [y/n]: " choice\n    case "$choice" in\n        y|Y )\n            DO_REMOVE=1\n            break ;;\n        n|N )\n            echo "Exiting!"\n            break ;;\n        * ) echo "Invalid input, expected [Y/y/N/n]"\n            continue ;;\n    esac\ndone\n\nif [ "$DO_REMOVE" -eq 1 ];then\n    echo "Removing!"\n    for f in "${FILES[@]}"; do\n        rm -rfv -- "$f"\n    done\nfi\n</code></pre>\n'}, {'answer_id': 61917678, 'author': 'Rajeev Shetty', 'author_id': 3932147, 'author_profile': 'https://Stackoverflow.com/users/3932147', 'pm_score': 5, 'selected': False, 'text': '<p>To remove Untracked files :</p>\n\n<pre><code>git add .\ngit reset --hard HEAD\n</code></pre>\n'}, {'answer_id': 63590924, 'author': 'Samir Kape', 'author_id': 8312897, 'author_profile': 'https://Stackoverflow.com/users/8312897', 'pm_score': 1, 'selected': False, 'text': '<p>usage: git clean [-d] [-f] [-i] [-n] [-q] [-e ] [-x | -X] [--] ...</p>\n<pre><code>-q, --quiet           do not print names of files removed\n-n, --dry-run         dry run\n-f, --force           force\n-i, --interactive     interactive cleaning\n-d                    remove whole directories\n-e, --exclude &lt;pattern&gt;\n                      add &lt;pattern&gt; to ignore rules\n-x                    remove ignored files, too\n-X                    remove only ignored files\n</code></pre>\n'}, {'answer_id': 64769771, 'author': 'KARTHIKEYAN.A', 'author_id': 4652706, 'author_profile': 'https://Stackoverflow.com/users/4652706', 'pm_score': 3, 'selected': False, 'text': '<p>To remove complete changes <strong>git clean -f -d</strong></p>\n<pre><code>$ git clean -f -d\nRemoving client/app/helpers/base64.js\nRemoving files/\nRemoving package.json.bak\n\nwhere \n-f is force \n-d is a directory \n</code></pre>\n'}, {'answer_id': 67098568, 'author': 'jenny', 'author_id': 6409591, 'author_profile': 'https://Stackoverflow.com/users/6409591', 'pm_score': 4, 'selected': False, 'text': '<p><code>git add --all</code>, <code>git stash</code> and <code>git stash drop</code>, try these three commands in this order inorder to remove all untracked files. By adding all those untracked files to git and stashing them will move all those untracked files to stash list and dropping out top one i.e., stash@{0} will remove the stashed changes from stash list.</p>\n'}, {'answer_id': 68547440, 'author': 'Ahmet Emrebas', 'author_id': 12603032, 'author_profile': 'https://Stackoverflow.com/users/12603032', 'pm_score': 3, 'selected': False, 'text': '<p>I think the safe and easy way is this!</p>\n<pre><code>git add .\ngit stash \n</code></pre>\n<p>For  more information\n<a href="https://www.atlassian.com/git/tutorials/saving-changes/git-stash#stashing-your-work" rel="noreferrer">https://www.atlassian.com/git/tutorials/saving-changes/git-stash#stashing-your-work</a></p>\n'}]
['https://Stackoverflow.com/questions/61212', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4883/']