Git stash single untracked file?
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
stash -p
, but this would only stash the tracked file. 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.
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
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:
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
链接地址: http://www.djcxy.com/p/24516.html
下一篇: Git存储单个未跟踪文件?