How to recover only one specific stash file?

Asked

Viewed 537 times

8

Suppose I did have a.txt, b.txt and c.txt that have been modified in a given branch and that I have used the command git stash in all of them to hide the changes.

If I rotate the git stash pop, I will recover all the files, and they returned to the current job.

However, I would like to know: How can I do to recover only the file b.txt stored through that git stash, as the above example?

2 answers

8


you can apply the use of git checkout or git show to restore a specific file.

git checkout stash@{0} -- <nome do arquivo>

To list the files:

git stash list
// stash@{0}: nome da branch que foi realizado o `stash`: Descrição
// stash@{1}: nome da branch que foi realizado o `stash`: Descrição

Note: When listing the entries stash that you currently own. Each entry stash receive a numbering (for example, stash@{0} is the most recent entry, stash@{1} is the previous, etc.), the name of the branch that was current when the input was made, and a short description of the confirmation on which the input was based.

To know which file:

git stash show -p stash@{0}
  • 1

    I gave a +1. Tip... Try to explain better about the number 0 within the {n}

  • 1

    @Wallacemaxters supported.

  • 1

    I didn’t understand what the @{0} - and it has to have that same hyphen: - before the file name? It would be nice to use the author’s answer data to put an example as didactic as the question

  • 2

    @hugocsl updated

  • 1

    @hugocsl related

0

step 1 : list existing stashes.

git stash list
stash@{0}: nome da branch que foi realizado o `stash`: Descrição
stash@{1}: nome da branch que foi realizado o `stash`: Descrição
stash@{2}: nome da branch que foi realizado o `stash`: Descrição
stash@{N}: nome da branch que foi realizado o `stash`: Descrição

Passo2: locate the stash that has your file, if it is the last sera or stash@{0} from {0} to {n} stashes.

git stash show --name-only -p stash@{0}
a.txt
b.txt
c.txt

show --name-only: displays only the file name show : displays the contents of the file

Step 3: Recover the stash file.

git checkout stash@{0} b.txt

Browser other questions tagged

You are not signed in. Login or sign up in order to post.