Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: How do you checkout all deleted files?

In git I am familiar with how to checkout individual files that have been deleted using the git checkout -- [<paths>...] syntax (which is recommended when you do git status.

To get all the files you could create a list and give the list as the argument to the above command.

However when you just want all the files that have been deleted (i.e. rm -rf in your cwd and then you want to restore all files) generating that list is inelegant.

How do you checkout all deleted files?

like image 659
Trevor Boyd Smith Avatar asked Jan 11 '17 14:01

Trevor Boyd Smith


People also ask

Does git show deleted files?

Listing all the deleted files in all of git history can be done by combining git log with --diff-filter . The log gives you lots of options to show different bits of information about the commit that happened at that point.

Does git checkout delete local files?

git checkout does not affect untracked files. Git only manages tracked files, and it works fairly hard to avoid letting you lose data (which is critical).

What to do when git deleted files?

Recovering Deleted Files with the Command Line Git provides ways to recover a deleted file at any point in this life cycle of changes. If you have not staged the deletion yet, simply run `git restore <filename>` and the file will be restored from the index.

How to find the commit of a deleted file in Git?

To find the right commit, first check the history for the deleted file: $ git log -- <filename>. You can either work with the last commit that still had the file, or the commit that deleted the file. In the first case, just checkout the file from that commit: $ git checkout <commit hash> -- <filename>. In the second case, checkout the file ...

What is the use of checkout in Git?

The git checkout command is used for switching branches or restoring working tree files. It operates on files, commits, and branches. It allows switching between multiple features in just a single repository. The git checkout command works with the git branch command.

How do I find deleted files in ouput?

Now that we have our complete list of deleted files, we can 'git rm' them: The command essentially says, "Use grep to find all the lines ouput by 'git status' that have the word 'deleted' in it, then grab the 3rd column on the line and run 'git rm' on it."

Is it possible to recover deleted code on Git?

As a dev, you must have deleted your code in error one time or the other. It is possible to recover those files on Git. In very easy and simple to describe steps, we’ll look over peculiar file losses and how to recover them on Git. While we’re at it, you’ll learn what Git is, what makes it special, and why you need one as a dev. Let’s go, shall we?


Video Answer


2 Answers

Generating the list is not that hard:

git diff --no-renames --name-only --diff-filter=D

To make it suitable for git checkout, use -z and xargs -0:

git diff --no-renames --name-only --diff-filter=D -z |
    xargs -0 git checkout --

Note that using git checkout -f -- . is quite different from the above, as git checkout -f -- . will overwrite files that are modified but not yet added to the index, while the above will only extract, from the index, files that are still in the index but are no longer in the work-tree.

(If you have no such modified files, git checkout -f -- . will work, but then so will git checkout -- ..)

like image 112
torek Avatar answered Sep 27 '22 22:09

torek


when you just want all the files that have been deleted (i.e. rm -rf in your cwd and then you want to restore all files)

You want

git checkout-index -a
like image 22
jthill Avatar answered Sep 27 '22 21:09

jthill