How can I stash only one of multiple changed files on my branch?
相关问题
- Why does recursive submodule update from github fa
- Extended message for commit via Visual Studio Code
- Emacs shell: save commit message
- Can I organize Git submodules in a flat hierarchy?
- Upload file > 25 MB on Github
相关文章
- 请教Git如何克隆本地库?
- GitHub:Enterprise post-receive hook
- Git Clone Fails: Server Certificate Verification F
- SSIS solution on GIT?
- Is there a version control system abstraction for
- ssh: Could not resolve hostname git: Name or servi
- Cannot commit changes with gitextensions
- git: retry if http request failed
When
git stash -p
(orgit add -p
withstash --keep-index
) would be too cumbersome, I found it easier to usediff
,checkout
andapply
:To "stash" a particular file/dir only:
Then afterwards
Let's say you have 3 files
and you want to stash only b.rb and c.rb but not a.rb
you can do something like this
And you are done! HTH.
This can be done easily in 3 steps using SourceTree.
This can all be done in a matter of seconds in SourceTree, where you can just click on the files (or even individual lines) you want to add. Once added, just commit them to a temporary commit. Next, click the checkbox to add all changes, then click stash to stash everything. With the stashed changes out of the way, glance over at your commit list and note the hash for the commit before your temporary commit, then run 'git reset hash_b4_temp_commit', which is basically like "popping" the commit by resetting your branch to the commit right before it. Now, you're left with just the stuff you didn't want stashed.
The problem with VonC's `intermediate' solution of copying files to outside the Git repo is that you lose path information, which makes copying a bunch of files back later on somewhat of a hassle.
A find it easier to use tar (similar tools will probably do) instead of copy:
One complicated way would be to first commit everything:
Reset back to the original commit but checkout the_one_file from the new commit:
Now you can stash the_one_file:
Cleanup by saving the committed content in your file system while resetting back to the original commit:
Yeah, somewhat awkward...
Since git is fundamentally about managing a all repository content and index (and not one or several files),
git stash
deals, not surprisingly,with the all working directory.Actually, since Git 2.13 (Q2 2017), you can stash individual files, with
git stash push
:See "Stash changes to specific files" for more.
The test case is self-explanatory:
The original answer (below, June 2010) was about manually selecting what you want to stash.
Casebash comments:
bukzor's answer (upvoted, November 2011) suggests a more practical solution, based on
git add
+git stash --keep-index
.Go see and upvote his answer, which should be the official one (instead of mine).
About that option, chhh points out an alternative workflow in the comments:
(Original answer June 2010: manual stash)
Yet,
git stash save --patch
could allows you to achieve the partial stashing you are after: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).
might be a better fit.
If
--patch
doesn't work, a manual process might:For one or several files, an intermediate solution would be to:
(Actually, eleotlecram proposes an interesting alternative)
git stash
git stash
# this time, only the files you want are stashedgit stash pop stash@{1}
# re-apply all your files modificationsgit checkout -- afile
# reset the file to the HEAD content, before any local modificationsAt the end of that rather cumbersome process, you will have only one or several files stashed.