简体   繁体   中英

If I run `git checkout -​- .` on branch. Is there any way I can get those files back?

If I have a load of uncommitted file changes and for whatever reason run git checkout -- on a file and then realise I need it back - is there any means of doing this?

Have tried git checkout HEAD <filename> but I imagine HEAD is not where those files exist anymore?

No, git did not write these file states anywhere in the scenario you describe.

However, depending on your context, if you use an advanced enough editor it might have kept your local changes.

Try out the below commands maybe one of them will help you out.

If the deletion has not been committed, the command below will restore the deleted file in the working tree.

$ git checkout -- <file>

You can get a list of all the deleted files in the working tree using the command below.

$ git ls-files --deleted

If the deletion has been committed, find the commit where it happened, then recover the file from this commit.

$ git rev-list -n 1 HEAD -- <file>
$ git checkout <commit>^ -- <file>

In case you are looking for the path of the file to recover, the following command will display a summary of all deleted files.

$ git log --diff-filter=D --summary

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM