How can I checkout an untracked file in a git stash?

eakst7

Assume I have stashed some changes using one of:

git stash -u
git stash --include-untracked

I can checkout an individual file from the stash via

git checkout stash@{0} -- filename

That works if git knows about "filename", but doesn't work if "filename" is untracked. Is there any way to checkout untracked files from a stash?

Jan Krüger

git stash internally creates special black magic merge commits to store different parts of your changes. The merge commit has the original base commit (what you had at the top of the branch when you stashed) as its first parent, a throwaway commit representing the index contents at time of stashing as its second parent, and (only if you used --include-untracked) a throwaway commit containing the untracked files as its third parent.

So, the merge commit references the untracked files (as one of its parents)... but it doesn't actually include those files in its own tree (if that doesn't make any sense, either you've got a few things to learn yet about Git's internals... or you know too much about merge commits and this whole construct just seems too horrible to think about ;)).

In short... to access the untracked parts of your stash, access its third parent: git checkout stash@{0}^3 -- filename

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How can I git stash a specific file?

From Dev

Can I have "git stash" to automatically include untracked files by default?

From Dev

Can I have "git stash" to automatically include untracked files by default?

From Dev

How can I switch git branches when there is an untracked file error?

From Dev

git checkout -b dev origin/dev being stopped by file I can't stash or ignore

From Dev

How to git stash only untracked files?

From Java

How do you stash an untracked file?

From Java

How can I rename a git stash?

From Dev

How can I share a git stash?

From Dev

Git stash to patch with untracked files

From Dev

How can I delete untracked files and folders in git

From Dev

How can I create a GIT Stash from a Commit?

From Dev

How can I create a GIT Stash from a Commit?

From Dev

Git: after merge I see a new untracked file `\032\032` I can't get rid of

From Dev

How to hide the untracked file of git in RStudio using?

From Dev

How to add untracked file in last git commit?

From Dev

How to hide the untracked file of git in RStudio using?

From Dev

How to interactively add untracked file in Git?

From Dev

Untracked files lost after git stash, working on branch and stash pop

From Dev

How can I git pull and git checkout correctly?

From Dev

How do I move an unstaged file to untracked?

From Dev

How do I move an unstaged file to untracked?

From Dev

Lost work doing a git checkout, how can I get it back

From Dev

Lost work doing a git checkout, how can I get it back

From Dev

How can I checkout to a remote tracking branch in git?

From Dev

How can I define an alias for a Git subcommand (e.g. for `list` in `git stash list`)?

From Java

How would I extract a single file (or changes to a file) from a git stash?

From Dev

How do I undo a partial git stash?

From Dev

Stuck on Git on Windows: Can't checkout, can't stash, can't commit

Related Related

  1. 1

    How can I git stash a specific file?

  2. 2

    Can I have "git stash" to automatically include untracked files by default?

  3. 3

    Can I have "git stash" to automatically include untracked files by default?

  4. 4

    How can I switch git branches when there is an untracked file error?

  5. 5

    git checkout -b dev origin/dev being stopped by file I can't stash or ignore

  6. 6

    How to git stash only untracked files?

  7. 7

    How do you stash an untracked file?

  8. 8

    How can I rename a git stash?

  9. 9

    How can I share a git stash?

  10. 10

    Git stash to patch with untracked files

  11. 11

    How can I delete untracked files and folders in git

  12. 12

    How can I create a GIT Stash from a Commit?

  13. 13

    How can I create a GIT Stash from a Commit?

  14. 14

    Git: after merge I see a new untracked file `\032\032` I can't get rid of

  15. 15

    How to hide the untracked file of git in RStudio using?

  16. 16

    How to add untracked file in last git commit?

  17. 17

    How to hide the untracked file of git in RStudio using?

  18. 18

    How to interactively add untracked file in Git?

  19. 19

    Untracked files lost after git stash, working on branch and stash pop

  20. 20

    How can I git pull and git checkout correctly?

  21. 21

    How do I move an unstaged file to untracked?

  22. 22

    How do I move an unstaged file to untracked?

  23. 23

    Lost work doing a git checkout, how can I get it back

  24. 24

    Lost work doing a git checkout, how can I get it back

  25. 25

    How can I checkout to a remote tracking branch in git?

  26. 26

    How can I define an alias for a Git subcommand (e.g. for `list` in `git stash list`)?

  27. 27

    How would I extract a single file (or changes to a file) from a git stash?

  28. 28

    How do I undo a partial git stash?

  29. 29

    Stuck on Git on Windows: Can't checkout, can't stash, can't commit

HotTag

Archive