How to remove all files in a Git repository that are not in the working directory?

前端 未结 3 1694
半阙折子戏
半阙折子戏 2021-02-07 17:53

I\'m in the process of splitting up an old suite of applications which originally resided in a single Subversion repository.

I\'ve converted it over to a Git repository

3条回答
  •  情书的邮戳
    2021-02-07 18:11

    Helping to the second answer: "Maybe others can chime in on how to find out the previous name of a tracked file in case of renames."

    This will return the files in your project and the files from which they are renamed.

    for file in `git ls-files`; do git log --follow --name-only --pretty=format: $file | sort -n -b | uniq | sed '/^\s*$/d'; done

    You can use them to exclude from the list.

    The whole solution is:

    for file in `git ls-files`; do git log --follow --name-only --pretty=format: $file | sort -n -b | uniq | sed '/^\s*$/d'; done > current.txt

    git log --raw |awk '/^:/ { if (! printed[$6]) { print $6; printed[$6] = 1 }}'|while read f;do if [ ! -f $f ]; then echo $f;fi;done | sort > hist.txt

    diff --new-line-format="" --unchanged-line-format="" hist.txt current.txt > for_remove.txt

提交回复
热议问题