title: Various ways to remove local Git changes
body: <p>I just cloned a git repository and checked out a branch. I worked on it, and then decided to remove all my local changes, as I wanted the original copy.</p>
  
  <p>In short, I had to do the following two commands to remove my local changes</p>
  
  <pre><code>git checkout .
  
  git clean -f
  </code></pre>
  
  <p><strong>My question is,</strong></p>
  
  <p>(1) Is this the correct approach in getting rid of local changes, or else please let me know the correct approach.</p>
  
  <p>(2) when do we use <code>git reset --hard</code> as i am able to reset even without this command</p>
  
  <p>Thanks</p>
  
  <p><strong>*Solution : Major Edit(s): 03/26 : *</strong> 
  Replaced many of vague terms with git specific terminology
   [tracked/untracked/staged/unstaged]</p>
  
  <p>There could be only three categories of files when we make local changes:</p>
  
  <blockquote>
    <p>Type 1. Staged Tracked files</p>
    
    <p>Type 2. Unstaged Tracked files</p>
    
    <p>Type 3. Unstaged UnTracked files a.k.a UnTracked files</p>
  </blockquote>
  
  <ul>
  <li>Staged - Those that are moved to staging area/ Added to index</li>
  <li>Tracked - modified files</li>
  <li>UnTracked - new files. Always unstaged. If staged, that means they are tracked.</li>
  </ul>
  
  <p><strong>What each commands do:</strong></p>
  
  <ol>
  <li><p><code>git checkout .</code> - Removes Unstaged Tracked files ONLY [Type 2]</p></li>
  <li><p><code>git clean -f</code> - Removes Unstaged UnTracked files ONLY [Type 3]</p></li>
  <li><p><code>git reset --hard</code> - Removes Staged Tracked and UnStaged Tracked files ONLY[Type 1, Type 2]</p></li>
  <li><p><code>git stash -u</code> - Removes all changes [Type 1, Type 2, Type 3]</p></li>
  </ol>
  
  <p><strong>Conclusion:</strong></p>
  
  <p>It's clear that we can use either </p>
  
  <pre><code>(1) combination of `git clean -f` and `git reset --hard` 
  </code></pre>
  
  <p>OR</p>
  
  <pre><code>(2) `git stash -u`
  </code></pre>
  
  <p>to achieve the desired result.</p>
  
  <p>Note: Stashing, as the word means 'Store (something) safely and secretly in a specified place.' This can always be retrieved using <code>git stash pop</code>.
  So choosing between the above two options is developer's call.</p>
  
  <p>Thank you Christoph and Frederik Schøning.</p>
  
  <p><strong>Edit: 03/27</strong></p>
  
  <p>I thought it's worth putting the '<em>beware</em>' note to <code>git clean -f</code></p>
  
  <p><code>git clean -f</code></p>
  
  <p>There is no going back. Use <code>-n</code> or <code>--dry-run</code> to preview the damage you'll do.</p>
  
  <p>If you want to also remove directories, run <code>git clean -f -d</code></p>
  
  <p>If you just want to remove ignored files, run <code>git clean -f -X</code></p>
  
  <p>If you want to remove ignored as well as non-ignored files, run <code>git clean -f -x</code></p>
  
  <p>reference : more on <code>git clean</code> : <a href="https://stackoverflow.com/questions/61212/removing-untracked-files-from-your-git-working-copy?rq=1">How to remove local (untracked) files from the current Git working tree?</a></p>
  
  <p><strong>Edit: 05/20/15</strong></p>
  
  <p><em>Discarding all local commits on this branch</em> [Removing local commits]</p>
  
  <p>In order to discard all local commits on this branch, to make the local branch identical 
  to the "upstream" of this branch, simply run <code>git reset --hard @{u}</code></p>
  
  <p>Reference: <a href="http://sethrobertson.github.io/GitFixUm/fixup.html" rel="noreferrer">http://sethrobertson.github.io/GitFixUm/fixup.html</a></p>
  
  <p>or do <code>git reset --hard origin/master</code> [if local branch is <code>master</code>]</p>
  
  <p><strong>Note: 06/12/2015</strong>
  This is <strong>not</strong> a duplicate of the other SO question that's marked as duplicate. This question address how to remove local GIT changes [remove a file added, remove changes added to existing file etc and the various approaches; Where in the other SO thread only address how to remove local commit. If you added a file, and you want to remove that alone, then the other SO thread doesn't discuss about it. Hence this is not a duplicate of the other one]</p>
  
  <p><strong>Edit: 06/23/15</strong></p>
  
  <p>How to revert a commit already pushed to a remote repository?</p>
  
  <pre><code>$ git revert ab12cd15
  </code></pre>
  
  <p><strong>Edit: 09/01/2015</strong></p>
  
  <p>Delete a previous commit from local branch and remote branch</p>
  
  <p>Case: You just commited a change to your local branch and immediately pushed to the remote branch,
  Suddenly realized , Oh no! I dont need this change. Now do what?</p>
  
  <p><code>git reset --hard HEAD~1</code>     [for deleting that commit from local branch]</p>
  
  <p><code>git push origin HEAD --force</code> [both the commands must be executed. For deleting from remote branch]</p>
  
  <p>Whats the branch ? Its the currently checked out branch.</p>
  
  <p><strong>Edit 09/08/2015</strong> - Remove local git <strong>merge</strong>:</p>
  
  <p>I am on <code>master</code> branch and merged <code>master</code> branch with a newly working branch <code>phase2</code></p>
  
  <pre><code>$ git status
  # On branch master
  
  $ git merge phase2
  
  $ git status
  # On branch master
  # Your branch is ahead of 'origin/master' by 8 commits.
  </code></pre>
  
  <p>Q: How to get rid of this merge?
  Tried <code>git reset --hard</code>   and <code>git clean -d -f</code>
  Both didn't work.</p>
  
  <p>The only thing that <strong>worked</strong> are any of the below ones:</p>
  
  <pre><code>$ git reset --hard origin/master
  </code></pre>
  
  <p>or</p>
  
  <pre><code>$ git reset --hard HEAD~8
  </code></pre>
  
  <p>or</p>
  
  <p><code>$ git reset --hard 9a88396f51e2a068bb7</code>  [sha commit code - this is the one that was present before all your merge commits happened]</p>