Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git stash single untracked file?

Tags:

git

git-stash

Question:

I have a couple of edited files as listed below, and also I have some newly created files

Stash File List

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.
like image 366
songyy Avatar asked Dec 12 '16 07:12

songyy


People also ask

How do I stash specific untracked files?

In order to stash untracked files, add the “–include-untracked” option to your “git stash” initial command. Alternatively, you can simply use the “-u” which is equivalent to the untracked longer version.

Does git stash work for untracked files?

Stashing untracked or ignored files By default, running git stash will stash: changes that have been added to your index (staged changes) changes made to files that are currently tracked by Git (unstaged changes)

How do I stash a single file?

To stash a specific file, use the “git stash push” command and specify the file you want to stash. However, the other tracked files that may be modified in your current working directory are untouched.

Why do I have untracked files in git?

Untracked files are those that are in the repo's directory but have not yet been added to the repo's index with git add .

How to Git stash tracked untracked and Ignored files?

How to Git Stash Tracked, Untracked and Ignored Files? You can stash all files (which will include tracked, untracked and ignored files) by using the --all (or -a shorthand) flag with the git stash command like so: Hope you found this post useful.

How do I stash all files in Git?

You can stash all files (which will include tracked, untracked and ignored files) by using the --all (or -a shorthand) flag with the git stash command like so: Hope you found this post useful. It was published 1 year ago.

How to stash untracked files without staging?

As of version 1.7.7 you can use git stash --include-untracked or git stash save -u to stash untracked files without staging them. Add (git add) the file and start tracking it. Then stash. Since the entire contents of the file are new, they will be stashed, and you can manipulate it as necessary.

What is the difference between GIT reset and Git stash?

@Eggon after you pop/apply the stashed changes, git reset will clear changes to the index, but leave them in the working tree, because the default mode is --mixed. In git bash, stashing of untracked files is achieved by using the command git stash removes any untracked or uncommited files from your workspace.


2 Answers

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.

like image 62
Kasper van den Berg Avatar answered Sep 29 '22 16:09

Kasper van den Berg


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.

like image 44
rodrigo Avatar answered Sep 29 '22 17:09

rodrigo