Git stash single untracked file?

醉酒当歌 提交于 2019-12-01 06:31:45
rodrigo

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.

ZimbiX

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:

  1. stash modified files (the file that you want to keep will remain untouched as it is not tracked.
  2. create a new branch and record the file there
  3. 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

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.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!