问题
Question:
I have a couple of edited files as listed below, and also I have some newly created files
Now I want to stash the untracked file (in this case, it's db/migrate/20161212071336_add_paranoid_fields.rb
, but not stash the changed files).
How should I do that?
Why I want to stash a single untracked file
I created this file at first, and then I realised that I don't need it immediately (it needs to be deleted in order for my program to work correctly); but I might need it in future.
What I've searched (and not giving me an answer), and the reason why they don't help
- How do you stash an untracked file?
- this would stash all files; I want on stash only a single file.
- how can I git stash a specific file?
- it suggests to use
stash -p
, but this would only stash the tracked file.
- it suggests to use
回答1:
If you only have this one untracked file you can do:
git add -u
git stash --include-untracked --keep-index
And that will add only untracked files to the stash, and remove them from the working directory.
If you have several untraked files and you want to include just this one to the stash, then you will have to do a commit, then the stash of the single file and then a reset
git add -u
git commit -m "temp commit"
git add my_file_to_stash
git stash
git reset --hard HEAD^
The subtlely here is that when you recover the stash later, that file will be added to the index. That is probably a good thing, as it will remind you that this file is important.
回答2:
Going off the information provided in the other answers, I created a script, git-stash-selection
, which I use aliased to gsts
:
#!/bin/sh
#
# Git stash only a selection of files, with a message.
#
# Usage:
# git-stash-selection [<message>] [<paths>...]
message=$1
shift
stash_paths="$*"
git add --all
git reset $stash_paths
git commit --allow-empty -m "temp - excluded from stash"
git add --all
git stash save $message
git reset --soft HEAD^
git reset
I've provided full details in another answer of the method this uses and why a number of simpler commands don't work.
Edit: This might be able to be improved using this patch method instead of committing. Or maybe using another stash
回答3:
stash does not provide this functionality. As what you want to achieve is to keep this file as part of an additional development, it will be best kept in a branch.
Steps:
- stash modified files (the file that you want to keep will remain untouched as it is not tracked.
- create a new branch and record the file there
- come back to the original branch
The commands:
git stash
git checkout -b paranoid_fields
git add db/migrate/20161212071336_add_paranoid_fields.rb
git commit
git checkout master
git stash pop
When you want to recover the file:
git merge paranoid_fields
will give it back.
If you want to just have a look at the file:
git show paranoid_fields:db/migrate/20161212071336_add_paranoid_fields.rb
回答4:
Similar to @rodrigo's answer, but without the commit and reset.
You can git stash
to store all of your current modified, tracked files in position stash@{0}
.
You can then git add <filename>
on your untracked file that you want to stash, and then git stash
again.
Running git stash list
will show that now the untracked file is stored in stash@{0}
and your modified tracked files are stored in stash@{1}
.
Now you can simply git stash pop stash@{1}
to remove your modified tracked files from the stash and put them back in your working directory, and it will leave your untracked file stashed in stash@{0}
.
You can see your file with git stash show -p stash@{0}
and restore it with either git stash apply stash@{0}
or git stash pop stash@{0}
. As @rodrigo points out, the untracked file will be restored to the staging area again.
回答5:
In git version 2.21.0 (Windows MingW64)
You can do:
git stash push --include-untracked -- db/migrate/20161212071336_add_paranoid_fields.rb
Only the specified file is included in the stash. You can provide multiple files (both untracked and tracked) and what you specify will be stashes and checked out as normal, the other files remain as is.
来源:https://stackoverflow.com/questions/41096269/git-stash-single-untracked-file