Search code examples
gitstaging

How to move a file from the staging area to the working tree in Git?


For example, I have a file called test1.txt in my local folder. I've added it to the staging area using git add -A. Now how can I move test1.txt from the staging area to the working tree?


Solution

  • What worked for me was:

    git restore --worktree test1.txt
    

    I mean, trying to clarify:

    1. Step 1: I write a new file test1.txt with the following content:

    This is test1.txt first content

    1. Step 2: I execute git add test1.txt. Now this first content of test1.txt is staged.

    2. Step 3: I modify test1.txt, with the following content:

    This is test1.txt first content. And now this is new content

    Now there is modified content in test1.txt in the working tree that differs from the content of test1.txt in staging.

    1. Step 4: I want to discard last changes of test1.txt in my working tree and recover the content of test1.txt from the staging. So I execute the following:

      git restore --worktree test1.txt

    Now I can see that test1.txt has only the first content, the one that I staged in Step 2. If I used --staged flag it wouldn't restore the content of test1.txt.