Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Since git is fundamentally about managing a all repository <em>content</em> and index (and not one or several files), <code>git stash</code> deals, not surprisingly, <del>with the all working directory</del>.</p> <p>Actually, since Git 2.13 (Q2 2017), you can stash individual files, with:</p> <pre><code>git stash push [--] [&lt;pathspec&gt;...] </code></pre> <p>See "<a href="https://stackoverflow.com/a/42963606/6309">Stash changes to specific files</a>" for more.</p> <hr> <p>The original answer (below, June 2010) was about manually selecting what you want to stash. </p> <p><a href="https://stackoverflow.com/users/165495/casebash">Casebash</a> comments:</p> <blockquote> <p>This (the <code>stash --patch</code> original solution) is nice, but often I've modified a lot of files so using patch is annoying</p> </blockquote> <p><a href="https://stackoverflow.com/users/146821/bukzor">bukzor</a>'s <a href="https://stackoverflow.com/a/8333163/6309">answer</a> (upvoted, November 2011) suggests a more practical solution, based on<br> <strong><code>git add</code> + <code>git stash --keep-index</code></strong>.<br> Go see and upvote his answer, which should be the official one (instead of mine).</p> <p>About that option, <a href="https://stackoverflow.com/users/88814/chhh">chhh</a> points out an alternative workflow in the comments:</p> <blockquote> <p>you should "<code>git reset --soft</code>" after such a stash to get your clear staging back:<br> In order to get to the original state - which is a clear staging area and with only some select un-staged modifications, one could softly reset the index to get (without committing anything like you - bukzor - did).</p> </blockquote> <hr> <p>(Original answer June 2010: manual stash)</p> <p>Yet, <code>git stash save --patch</code> could allows you to achieve the partial stashing you are after:</p> <blockquote> <p>With <code>--patch</code>, you can interactively select hunks from in the diff between HEAD and the working tree to be stashed.<br> The stash entry is constructed such that its index state is the same as the index state of your repository, and its worktree contains only the changes you selected interactively. The selected changes are then rolled back from your worktree.</p> </blockquote> <p>However that will save the full index (which may not be what you want since it might include other files already indexed), and a partial worktree (which could look like the one you want to stash).</p> <pre><code>git stash --patch --no-keep-index </code></pre> <p>might be a better fit.</p> <hr> <p>If <code>--patch</code> doesn't work, a manual process might:</p> <p>For one or several files, an intermediate solution would be to:</p> <ul> <li>copy them outside the Git repo<br> (Actually, <a href="https://stackoverflow.com/users/578435/eleotlecram">eleotlecram</a> proposes an <a href="https://stackoverflow.com/a/7569847/6309">interesting alternative</a>)</li> <li><code>git stash</code></li> <li>copy them back</li> <li><code>git stash</code> # this time, only the files you want are stashed</li> <li><code>git stash pop stash@{1}</code> # re-apply all your files modifications</li> <li><code>git checkout -- afile</code> # reset the file to the HEAD content, before any local modifications</li> </ul> <p>At the end of that rather cumbersome process, you will have only one or several files stashed.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload