Unstage a deleted file in git

后端 未结 7 1756
我在风中等你
我在风中等你 2020-12-04 04:28

Usually, to discard changes to a file you would do:

git checkout -- 

What if the change I want to discard is deleting the file?

相关标签:
7条回答
  • 2020-12-04 05:03

    From manual page,

    git-reset - Reset current HEAD to the specified state
    git reset [-q] [<tree-ish>] [--] <paths>...
    In the first and second form, copy entries from <tree-ish> to the index.
    

    for example, when we use git reset HEAD~1 it reset our current HEAD to HEAD~1

    so when we use git reset 'some-deleted-file-path'

    git assume 'some-deleted-file-path' as some commit point and try to reset out current HEAD to there.

    And it ends up fail

    fatal: ambiguous argument 'some-deleted-file-path': unknown revision or path not in the working tree.
    
    0 讨论(0)
  • 2020-12-04 05:06

    Both questions are answered in git status.

    To unstage adding a new file use git rm --cached filename.ext

    # Changes to be committed:
    #   (use "git rm --cached <file>..." to unstage)
    #
    #   new file:   test
    

    To unstage deleting a file use git reset HEAD filename.ext

    # Changes to be committed:
    #   (use "git reset HEAD <file>..." to unstage)
    #
    #   deleted:    test
    

    In the other hand, git checkout -- never unstage, it just discards non-staged changes.

    0 讨论(0)
  • 2020-12-04 05:06

    The answers to your two questions are related. I'll start with the second:

    Once you have staged a file (often with git add, though some other commands implicitly stage the changes as well, like git rm) you can back out that change with git reset -- <file>.

    In your case you must have used git rm to remove the file, which is equivalent to simply removing it with rm and then staging that change. If you first unstage it with git reset -- <file> you can then recover it with git checkout -- <file>.

    0 讨论(0)
  • 2020-12-04 05:09

    I tried the above solutions and I was still having difficulties. I had other files staged with two files that were deleted accidentally.

    To undo the two deleted files I had to unstage all of the files:

    git reset HEAD .
    

    At that point I was able to do the checkout of the deleted items:

    git checkout -- WorkingFolder/FileName.ext
    

    Finally I was able to restage the rest of the files and continue with my commit.

    0 讨论(0)
  • 2020-12-04 05:12

    If it has been staged and committed, then the following will reset the file:

    git reset COMMIT_HASH file_path
    git checkout COMMIT_HASH file_path
    git add file_path
    

    This will work for a deletion that occurred several commits previous.

    0 讨论(0)
  • 2020-12-04 05:24

    Assuming you're wanting to undo the effects of git rm <file> or rm <file> followed by git add -A or something similar:

    # this restores the file status in the index
    git reset -- <file>
    # then check out a copy from the index
    git checkout -- <file>
    

    To undo git add <file>, the first line above suffices, assuming you haven't committed yet.

    0 讨论(0)
提交回复
热议问题