Git rm several files?

前端 未结 11 1952
长发绾君心
长发绾君心 2020-12-15 05:08

How do I easily remove several files without manually typing the full paths of all of them to git rm? I have plenty of modified files I\'d like to keep so remov

相关标签:
11条回答
  • 2020-12-15 05:22

    I found git rm's handling of wild cards annoying. Find can do the trick in one line: find . -name '*.c' -exec git rm {} \; the {} is where the file name will be substituted. The great thing about find is it can filter on a huge variety of file attributes not just name.

    0 讨论(0)
  • 2020-12-15 05:25

    You can simply use:

    git add -u
    
    0 讨论(0)
  • 2020-12-15 05:27

    You can give wildcards to git rm.

    e.g.

    git rm *.c
    

    Or you can just write down the names of all the files in another file, say filesToRemove.txt:

    path/to/file.c
    path/to/another/file2.c
    path/to/some/other/file3.c
    

    You can automate this:

    find . -name '*.c' > filesToRemove.txt
    

    Open the file and review the names (to make sure it's alright).

    Then:

    cat filesToRemove.txt | xargs git rm
    

    Or:

    for i in `cat filesToRemove.txt`; do git rm $i; done
    

    Check the manpage for xargs for more options (esp. if it's too many files).

    0 讨论(0)
  • 2020-12-15 05:30

    Easy way:

    • Delete files in your file explorer
    • use git ls-files --deleted | xargs git add to stage them. They will be removed in the remote once you push.

    Git way: Referencing what @CpILL said (https://stackoverflow.com/a/34889424/6538751) use

    • find . -name 'DeleteMe*.cs' -exec git rm {} \;

    you can utilize wildcards.

    0 讨论(0)
  • 2020-12-15 05:31

    For removing multiple files at once, you might want to checkout the answer here

    You can delete the files that you don't want and run this command: git rm $(git ls-files --deleted)

    0 讨论(0)
  • 2020-12-15 05:32

    You could also check out Cygwin, as it provides much of the Unix/Linux/*BSD functionality on Windows. Cygwin includes a Bash shell and find(1), among other tools mentioned above. (I usually have 2-4 Cygwin mintty terminals up at one time on Windows 7 because I find Cygwin that handy.)

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