Programming

패턴별로 파일을 재귀 적으로 추가

procodes 2020. 6. 5. 22:08
반응형

패턴별로 파일을 재귀 적으로 추가


다른 디렉토리에있는 패턴 (또는 glob)별로 파일을 재귀 적으로 추가하려면 어떻게합니까?

예를 들어, 하나의 명령으로 A/B/C/foo.javaD/E/F/bar.java(및 다른 여러 Java 파일) 을 추가 하고 싶습니다 .

git add '*.java'

불행히도, 그것은 예상대로 작동하지 않습니다.


추가 할 파일 중 일부가 아직 추적되지 않은 경우 Sergio Acosta의 답변이 가장 좋습니다. git 이미 알고있는 파일로 자신을 제한하려면 git-ls-files필터와 결합 할 수 있습니다 .

git ls-files [path] | grep '\.java$' | xargs git add

Git은 기본적으로 쉘 문제이기 때문에이 작업을 수행하는 멋진 메커니즘을 제공하지 않습니다. 주어진 명령에 인수로 제공 할 파일 목록을 얻는 방법은 무엇입니까?


git add [path]/\*.java하위 디렉토리에서 Java 파일을 추가 하는 사용할 수 있습니다 (
예 : git add ./\*.java현재 디렉토리).

에서 git add문서 :

디렉토리 및 해당 서브 디렉토리 *.txt아래의 모든 파일에서 컨텐츠를 추가합니다 Documentation.

$ git add Documentation/\*.txt

*이 예에서 별표 는 쉘에서 인용됩니다. 이를 통해 명령은 디렉토리의 서브 디렉토리에있는 파일을 포함 할 수 있습니다 Documentation/.


약간 벗어난 주제 (특히 git 관련 아님)이지만 Linux / unix를 사용하는 경우 해결 방법은 다음과 같습니다.

find . -name '*.java' | xargs git add

공백이있는 경로가 예상되는 경우 :

find . -name '*.java' -print0 | xargs -0 git add

그러나 나는 그것이 당신이 요구 한 것이 아니라는 것을 알고 있습니다.


zsh당신 과 함께 실행할 수 있습니다 :

git add "**/*.java"

모든 *.java파일이 재귀 적으로 추가됩니다.


Sergey의 답변 (나를 신용하지 마십시오)이 효과가 있습니다.

You can use git add [path]/\*.java

최근 자식으로 :

$git version
git version 1.7.3.4

테스트 용 파일 :

$find -name .git -prune -o -type f -print | sort
./dirA/dirA-1/dirA-1-1/file1.txt
./dirA/dirA-1/dirA-1-2/file2.html
./dirA/dirA-1/dirA-1-2/file3.txt
./dirA/dirA-1/file4.txt
./dirB/dirB-1/dirB-1-1/file5.html
./dirB/dirB-1/dirB-1-1/file6.txt
./file7.txt

힘내 상태 :

$git status -s
?? dirA/
?? dirB/
?? file7.txt

* .txt 추가 :

$git add \*.txt

업데이트 된 상태 :

$git status -s
A  dirA/dirA-1/dirA-1-1/file1.txt
A  dirA/dirA-1/dirA-1-2/file3.txt
A  dirA/dirA-1/file4.txt
A  dirB/dirB-1/dirB-1-1/file6.txt
A  file7.txt
?? dirA/dirA-1/dirA-1-2/file2.html
?? dirB/dirB-1/dirB-1-1/file5.html

If you are already tracking your files and have made changes to them and now you want to add them selectively based on a pattern, you can use the --modified flag

git ls-files --modified | grep '<pattern>' | xargs git add

For example, if you only want to add the CSS changes to this commit, you can do

git ls-files --modified | grep '\.css$' | xargs git add

See man git-ls-files for more flags


I wanted to only add files that had a certain string based on git status:

git status | grep string | xargs git add

and then was able to git commit -m 'commit msg to commit all changed files with "string" in the title of the file


Just use git add *\*.java. This will add all .java files in root directory and all subdirectories.


As mentioned in "git: How do I recursively add all files in a directory subtree that match a glob pattern?", if you properly escape or quote your pathspec globbing (like '*.java'), then yes, git add '*.java'

Git 2.13 (Q2 2017) improves that for interactive add:

See commit 7288e12 (14 Mar 2017) by Jeff King (peff).
(Merged by Junio C Hamano -- gitster -- in commit 153e0d7, 17 Mar 2017)

add --interactive: do not expand pathspecs with ls-files

When we want to get the list of modified files, we first expand any user-provided pathspecs with "ls-files", and then feed the resulting list of paths as arguments to "diff-index" and "diff-files".
If your pathspec expands into a large number of paths, you may run into one of two problems:

  1. The OS may complain about the size of the argument list, and refuse to run. For example:

    $ (ulimit -s 128 && git add -p drivers)
    Can't exec "git": Argument list too long at .../git-add--interactive line 177.
    Died at .../git-add--interactive line 177.
    

That's on the linux.git repository, which has about 20K files in the "drivers" directory (none of them modified in this case). The "ulimit -s" trick is necessary to show the problem on Linux even for such a gigantic set of paths.
Other operating systems have much smaller limits (e.g., a real-world case was seen with only 5K files on OS X).

  1. Even when it does work, it's really slow. The pathspec code is not optimized for huge numbers of paths. Here's the same case without the ulimit:

    $ time git add -p drivers
      No changes.
    
    real  0m16.559s
    user    0m53.140s
    sys 0m0.220s
    

We can improve this by skipping "ls-files" completely, and just feeding the original pathspecs to the diff commands.

Historically the pathspec language supported by "diff-index" was weaker, but that is no longer the case.


Adding a Windows command line solution that was not yet mentioned:

for /f "delims=" %G in ('dir /b/s *.java') do @git add %G

put line in ~/.gitconfig

[alias] addt = !sh -c 'git ls-files | grep \"\\.$1*\" | xargs git add' -

If you want to add all modified java file can just do: git addt java

Samely, if you want to add all modified python file can just do: git addt py

참고URL : https://stackoverflow.com/questions/2855140/recursively-add-files-by-pattern

반응형