Programing

이미 디스크에서 삭제 된 Git 리포지토리에서 여러 파일 제거

lottogame 2020. 9. 28. 07:51
반응형

이미 디스크에서 삭제 된 Git 리포지토리에서 여러 파일 제거


사용 rm( not git rm ) 에서 4 개의 파일을 삭제 한 Git 리포지토리가 있으며 내 Git 상태는 다음과 같습니다.

#    deleted:    file1.txt
#    deleted:    file2.txt
#    deleted:    file3.txt
#    deleted:    file4.txt

수동으로 다음과 같이 각 파일을 추가하지 않고 Git에서 이러한 파일을 제거하려면 어떻게해야합니까?

git rm file1 file2 file3 file4

이상적으로 git add .는 가능한 경우 동일한 방식으로 작동하는 것을 찾고 있습니다.


Git 1.x의 경우

$ git add -u

이것은 이전에 추적 된 파일 삭제를 포함하여 추적 된 파일을 자동으로 스테이징하도록 git에 지시합니다.

Git 2.0의 경우

전체 작업 트리를 준비하려면 :

$ git add -u :/

현재 경로 만 준비하려면 :

$ git add -u .

git ls-files --deleted -z | xargs -0 git rm 

당신이 찾고있는 것일 수도 있습니다 .. 그것은 나를 위해 작동합니다 ..


당신이 사용할 수있는

git add -u

삭제 된 파일을 준비 영역에 추가하려면 커밋합니다.

git commit -m "Deleted files manually"

단순히 실행하는 경우 :

git add -u

git은 삭제 한 파일이 실제로 다음 커밋의 일부 여야한다는 것을 알기 위해 색인을 업데이트합니다. 그런 다음 "git commit"을 실행하여 해당 변경 사항을 확인할 수 있습니다.

또는 다음을 실행하는 경우 :

git commit -a

이러한 변경 사항 (및 기타)을 자동으로 적용하고 커밋합니다.

업데이트 : 삭제 된 파일 만 추가하려면 다음을 시도하십시오.

git ls-files --deleted -z | xargs -0 git rm
git commit

당신은 아마 -A :

git add -A

이것은 git add -u와 유사하지만 새 파일도 추가합니다. 이것은 대략 hg의 addremove명령 과 동일 합니다 (이동 감지는 자동이지만).


삭제 된 파일 준비하려면 :

for x in $(git status | grep deleted | awk '{print $2}'); do git rm $x; done

또는 (xargs 방식) :

git status | awk '/deleted/ {print $2}' | xargs git rm

나중에 편리하게 사용할 있도록 선호하는 명령 세트의 별명을 지정할 수 있습니다 .


git rm test.txt

실제 파일을 삭제하기 전 또는 후.


'--all'또는 '--update'옵션과 함께 git-add를 사용하면 원하는 것보다 더 많은 것을 얻을 수 있습니다. 새 파일 및 / 또는 수정 된 파일도 색인에 추가됩니다. 다른 파일을 건드리지 않고 git에서 삭제 된 파일을 제거하려는 경우에 대한 bash 별칭 설정이 있습니다.

alias grma='git ls-files --deleted -z | xargs -0 git rm'

파일 시스템에서 제거 된 모든 파일은 삭제 된대로 색인에 추가됩니다.


정말 중요하지는 않지만 선택한 답변에 동의하지 않습니다.

git add -u 

... 작업 트리에서 해당 파일이 제거 된 경우 인덱스에서 파일을 제거하지만 추적 된 파일의 수정 된 새 내용도 준비합니다.

git rm $(git ls-files --deleted)

... 반면에 추적 된 삭제 된 파일 만 RM됩니다.

그래서 내 견해로는 후자가 더 나은 선택입니다.


이것이 유일한 변경 사항이라면 간단하게

git commit -a

모든 변경 사항을 커밋합니다. 여기에는 삭제 된 파일이 포함됩니다.


git ls-files --deleted | xargs git rm 

삭제 된 파일 만 추가하는 가장 좋은 옵션입니다.

Here is some other options.

git add .  => Add all (tracked and modified)/new files in the working tree.

git add -u => Add all modified/removed files which are tracked.

git add -A => Add all (tracked and modified)/(tracked and removed)/new files in the working tree.

git commit -a -m "commit message" - Add and commit modified/removed files which are tracked.

git add -u

-u --update Only match against already tracked files in the index rather than the working tree. That means that it will never stage new files, but that it will stage modified new contents of tracked files and that it will remove files from the index if the corresponding files in the working tree have been removed.

If no is given, default to "."; in other words, update all tracked files in the current directory and its subdirectories.


That simple solution works fine for me:

git rm $(git ls-files --deleted)

If you want to add it to your .gitconfig do this:

[alias]
  rma = !git ls-files --deleted -z | xargs -0 git rm

Then all you have to do is run:

git rma

git ls-files --deleted -z | xargs -0 git rm --cached

This will remove all deleted files that were previous tracked by git, as well as handle the case where your filenames have spaces in them.

Depending on your POSIX variant, you may need to use xargs -0 -r: this will cause xargs to gracefully exit when piped null content.

EDIT: --cached and --deleted flags are used in tandem to safeguard against accidentally deleting files that have not already been deleted.


The following will work, even if you have a lot of files to process:

git ls-files --deleted | xargs git rm

You'll probably also want to commit with a comment.

For details, see: Useful Git Scripts


Try it.

-a
--all
Tell the command to automatically stage files that have been modified and deleted, but new files you have not told Git about are not affected.

git add . && git commit -m -a "Your commit"

or

git add --all && git commit -m "Your commit"

Please use -t to see which command is actually being ran

I just tweaked Virender answer to do same:

git ls-files --deleted -z | xargs -t -0 git rm

None of the flags to git-add will only stage removed files; if all you have modified are deleted files, then you're fine, but otherwise, you need to run git-status and parse the output.

Working off of Jeremy's answer, this is what I got:

git status |  sed -s "s/^.*deleted: //" | grep "\(\#\|commit\)" -v | xargs git rm
  1. Get status of files.
  2. For deleted files, isolate the name of the file.
  3. Remove all the lines that start with #s, as well as a status line that had the word "deleted" in it; I don't remember what it was, exactly, and it's not there any longer, so you may have to modify this for different situations. I think grouping of expressions might be a GNU-specific feature, so if you're not using gnutils, you may have to add multiple grep -v lines.
  4. Pass the files to git rm.

Sticking this in a shell alias now...


As mentioned

git add -u

stages the removed files for deletion, BUT ALSO modified files for update.

To unstage the modified files you can do

git reset HEAD <path>

if you like to keep your commits organized and clean.
NOTE: This could also unstage the deleted files, so careful with those wildcards.


git commit -m 'commit msg' $(git ls-files --deleted)

This worked for me after I had already deleted the files.


I needed the same and used git gui "stage changed" button. it also adds all.

And after "stage changed" I made "commit" ...

so my working directory is clean again.


You can use git add -u <filenames> to stage the deleted files only.

For example, if you deleted the files templates/*.tpl, then use git add -u templates/*.tpl.

The -u is required in order to refer to files that exist in the repository but no longer exist in the working directory. Otherwise, the default of git add is to look for the files in the working directory, and if you specify files you've deleted there, it won't find them.


something like

git status | sed -s "s/^.*deleted: //" | xargs git rm 

may do it.


Adding system alias for staging deleted files as command rm-all

UNIX alias rm-all='git rm $(git ls-files --deleted)'

WINDOWS doskey rm-all=bash -c "git rm $(git ls-files --deleted)"

Note

Windows needs to have bash installed.


(Yet another variation)

I wanted to delete all the already deleted from the disk files but from one specific folder, leaving the other folders untouched. The following worked for me:

git ls-files --deleted  | grep <folder-name> | xargs git rm

git rm $(git ls-files -d)

Removes all files listed by the git ls-files command (-d show only deleted files). Doesn't work for files with spaces in the filename or path, but simple to remember


For visual studio project

'git ls-files --deleted | sed 's/(.*)/"\1"/'| xargs git rm' 

which is useful when the deleted file path has space


The most flexible solution I have found to date is to

git cola

And select all deleted files I want to stage.

(Note I usually do everything commandline in git, but git handles removed files a bit awkward).

참고URL : https://stackoverflow.com/questions/1402776/how-do-i-commit-all-deleted-files-in-git

반응형