Programing

git 저장소에서 디렉토리를 제거하는 방법은 무엇입니까?

lottogame 2020. 9. 27. 12:41
반응형

git 저장소에서 디렉토리를 제거하는 방법은 무엇입니까?


내 GitHub 저장소에 2 개의 디렉토리가 있습니다. 그중 하나를 삭제하고 싶습니다. 전체 저장소를 삭제하고 다시 만들지 않고 어떻게 할 수 있습니까?


git 및 local에서 디렉토리 제거

두 디렉토리 모두에서 'master'를 체크 아웃 할 수 있습니다.

git rm -r one-of-the-directories
git commit -m "Remove duplicated directory"
git push origin <your-git-branch> (typically 'master', but not always)

git에서 디렉토리를 제거하지만 로컬이 아닙니다.

주석에서 언급했듯이 일반적으로 원하는 것은 git에서이 디렉토리를 제거하지만 파일 시스템 (로컬)에서 완전히 삭제하지 않는 것입니다.

이 경우 사용 :

git rm -r --cached myFolder

폴더 / 디렉토리를 로컬이 아닌 git 저장소에서만 제거하려면 3 개의 간단한 명령을 시도하십시오.


디렉토리를 제거하는 단계

git rm -r --cached FolderName
git commit -m "Removed folder from repository"
git push origin master

다음 커밋에서 해당 폴더를 무시하는 단계

다음 커밋에서 해당 폴더를 무시하려면 루트에 .gitignore 라는 파일을 하나 만들고 그 폴더 이름을 그 폴더에 넣으십시오. 원하는만큼 넣을 수 있습니다.

.gitignore 파일은 다음과 같습니다.

/FolderName

디렉토리 제거


어떤 이유로 karmakaze가 말한 내용이 작동하지 않으면 사용하려는 디렉토리를 삭제하거나 파일 시스템 브라우저 (예 : Windows 파일 탐색기)를 사용하여 삭제할 수 있습니다. 디렉토리를 삭제 한 후
git add -A
다음 명령을 실행
git commit -m 'deleting directory'
한 다음
git push origin master.


이것을 시도 할 수 있습니다. git rm -rf <directory_name>

디렉터리를 강제로 삭제합니다.


디렉토리에서 파일을 제거하면 ( git rm다른 답변에서 설명했듯이) git에 관한 한 더 이상 디렉토리가 존재하지 않습니다. 빈 디렉터리를 커밋하거나 제거 할 수 없습니다.

이것은 명시 적으로해야하는 Subversion과는 달리 git 용 페이지가 "어리석은 콘텐츠 추적기"라고 설명 svn rm emptyfolder/하는 이유입니다.man

An answer on "How do I add an empty directory to a git repository" links to the FAQ on this subject:

Currently the design of the git index (staging area) only permits files to be listed, and nobody competent enough to make the change to allow empty directories has cared enough about this situation to remedy it.

Directories are added automatically when adding files inside them. That is, directories never have to be added to the repository, and are not tracked on their own.

You can say "git add <dir>" and it will add files in there.

If you really need a directory to exist in checkouts you should create a file in it. .gitignore works well for this purpose; you can leave it empty, or fill in the names of files you expect to show up in the directory.


Go to your git Directory then type the following command: rm -rf <Directory Name>

After Deleting the directory commit the changes by: git commit -m "Your Commit Message"

Then Simply push the changes on remote GIT directory: git push origin <Branch name>


You can delete the folder locally and then push, as follow:

git rm -r folder_name
git commit -m "your commit"
git push origin master

I usually use git add --all to remove files / folders from remote repositories

rm -r folder_name
git add --all
git commit -m "some comment"
git push origin master

master can be replaced by any other branch of the repository.


for deleting Empty folders

i wanted to delete an empty directory(folder) i created, git can not delete it, after some research i learned Git doesn't track empty directories. If you have an empty directory in your working tree you should simply removed it with

rm -r folderName

There is no need to involve Git.


You can use Attlasian Source Tree (Windows) (https://www.atlassian.com/software/sourcetree/overview). Just select files from tree and push button "Remove" at the top. Files will be deleted from local repository and local git database. Then Commit, then push.


One of my colleague suggested BFG Repo-Cleaner which I think powerful. It is not only delete unwanted data but also clean your repository from any related commit information.


To add new directory:

mkdir <YOUR-DIRECTORY>

But now Git is not aware by this new directory, because Git keep tracks of file not directories DIRECTORY

git status

Git won't be aware with the change we've made, so we add hidden .keep file to make Git aware by this new change.

touch /YOUR-directory/.keep

Now, if you hit git status Git will be aware with the changes.

And If you want to delete the directory, you should use this command.

rm -r <YOUR-DIRECTORY>

And If you checked by using git status, you will see the directory has been removed.


I already had committed the folder before and want to remove the directory in the history as well.

I did the following:

Add folder to .gitignore:

echo Folder_Name/ >> .gitignore

Remove from all commits:

git filter-branch --index-filter 'git rm -rf --cached --ignore-unmatch Folder_Name/' --prune-empty --tag-name-filter cat -- --all

remove the refs from the old commits:

git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d

Ensure all old refs are fully removed

rm -Rf .git/logs .git/refs/original

Perform a garbage collection

git gc --prune=all --aggressive

push you changes to the online repository:

git push

You are done here.

그러나 다음을 사용하여 모든 분기에 모든 변경 사항을 푸시 할 수 있습니다. 그러나이 명령에주의하십시오!

git push origin --all --force
git push origin --tags --force

그 후 폴더는 git에서 제거되었지만 로컬 디스크에서는 삭제되지 않았습니다.


첫 번째 git 명령은 무엇이든 삭제하기 전에 당신이 누구인지 알아야합니다.

  1. 자식 초기화
  2. git config user.name "someone"
  3. git config user.email "someone@someplace.com"
  4. 자식 rm -r
  5. git commit -m "dir 삭제"
  6. git push origin master

참고 URL : https://stackoverflow.com/questions/6313126/how-to-remove-a-directory-from-git-repository

반응형