Git: How do you check all deleted files?

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

To get all the files, you can create a list and provide the list as an argument to the specified command.

However, when you just want all the files that were deleted (i.e. rm -rf in your cwd, and then you want to restore all the files), this list is not elegantly generated.

How to check all deleted files?

+6
source share
6 answers

Creating a list is not so difficult:

 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 -- . very different from the above as git checkout -f -- . will overwrite files that have been modified but not yet added to the index, while the above will only retrieve files from the index that are still in the index but no longer in the working tree.

(If you do not have such modified files, git checkout -f -- . Will work, but then git checkout -- . Will be.)

+8
source

I usually do

 1. $ git checkout . 

to check for changes to follow

 2. $ git clean -fd 

This will delete unnecessary files. f for files and d for directories.

You can also make a dry move to step 2 by doing

 git clean -fdn 

This will delete the files and directories that will be deleted.

Read more about undo changes

+1
source

The git checkout --force seems to be doing the trick. The manual page for the --force parameter says: "[The --force parameter] is used to discard local changes."

On the git-checkout man page:

  -f, --force When switching branches, proceed even if the index or the working tree differs from HEAD. This is used to throw away local changes. When checking out paths from the index, do not fail upon unmerged entries; instead, unmerged entries are ignored. 
0
source

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

Do you want to

 git checkout-index -a 
0
source

If changes to the delete files have not been made, you can use git checkout -- . .

If changes to the delete files have been committed, you can use git reset --hard HEAD~ .

0
source

I was able to do this with:

 for f in $(git diff --no-renames --name-only --diff-filter=D);do git checkout -- $f ;done 
0
source

Source: https://habr.com/ru/post/1013990/


All Articles