title: Git command to checkout any branch and overwrite local changes
body: <p>Is there a Git command (or a short sequence of commands) that will safely and surely do the following?</p>
  
  <ul>
  <li>Get rid of any local changes.</li>
  <li>Fetch the given branch from origin if necessary</li>
  <li>Checkout the given branch?</li>
  </ul>
  
  <p>Currently I'm stuck with:</p>
  
  <pre><code>git fetch -p
  git stash
  git stash drop
  git checkout $branch
  git pull
  </code></pre>
  
  <p>but it's bothering me because I'm asked for password two times (by <code>fetch</code> and <code>pull</code>). <strong>Generally I would be happy with any solution as long as the password is needed only once.</strong></p>
  
  <p>A couple of notes:</p>
  
  <ul>
  <li>It's a part of homebrewed deployment script for an application (the code is hosted on GitHub).</li>
  <li>There should be no difference if the branch was already fetched from origin or not (i.e. the first deployment of a new branch shouldn't ideally require any additional steps).</li>
  <li>The script is located on a remote machine that can be accessed by several people, hence no credentials are stored and user/password must be entered (but only once if possible).</li>
  <li>I don't care about any local changes; I always want a pristine copy of the given branch (the further part of deployment script produces local changes).</li>
  <li>I can't clone or export a fresh repository each time; it takes too much time.</li>
  </ul>