일상/컴퓨터

[GitHub] Git 깃허브 공개 저장소 비공개로 복제/포크

미적미적달팽이 2024. 10. 13. 21:52

참고한 글

https://stackoverflow.com/questions/10065526/github-how-to-make-a-fork-of-public-repository-private

 

GitHub: How to make a fork of public repository private?

How can I fork a public repository, but make my fork private? I do have the subscription to support private repositories.

stackoverflow.com

https://docs.github.com/ko/repositories/creating-and-managing-repositories/duplicating-a-repository

 

리포지토리 복제 - GitHub Docs

리포지토리의 미러를 포크하지 않고 유지하려면 특수 클론 명령을 실행한 다음 새 리포지토리에 미러 푸시할 수 있습니다.

docs.github.com

 

Git UI에서는 Public repository를 Private로 복제할 수가 없어서 비공개로 복제하고자 하였다

 


비공개 저장소 만들기

https://github.com/new

 

GitHub: Let’s build from here

GitHub is where over 100 million developers shape the future of software, together. Contribute to the open source community, manage your Git repositories, review code like a pro, track bugs and fea...

github.com

 

자기 프로필에 새로운 비공개 저장소를 하나 만든다.

 

 

 

Git Bash 작업

이제 복제할 저장소에서 메타 데이터를 가져온다. 어차피 비공개 저장소로 복제하는 용도이기에 다 가져올 필요는 없는 듯

git clone --bare https://github.com/exampleuser/public-repo.git

https://github.com/exampleuser/public-repo.git 에는 자신이 복제하고 싶은 공개 저장소 주소를 넣어야 한다

cd public-repo.git
git push --mirror https://github.com/yourname/private-repo.git

복제하고 싶은 저장소의 데이터를 자신의 비공개 데이터로 완전히 복제를 해준다. https://github.com/yourname/private-repo.git 에는 자신이 만든 저장소 주소를 넣어준다.

cd ..
rm -rf public-repo.git

복제가 잘 완료되면 bare한 원본 저장소를 삭제해도 좋다

 

 

대용량 복제

복제하고 싶은 저장소가 대용량의 파일을 포함하고 있다면 lfs를 이용해서 복사해줘야 한다.

git lfs fetch --all
git push --mirror https://github.com/EXAMPLE-USER/NEW-REPOSITORY.git
git lfs push --all https://github.com/EXAMPLE-USER/NEW-REPOSITORY.git

 

 

복제 오류

복제하다가 문제가 발생할 수 있다.

로컬 저장소에서 문제가 발생한 객체나 파일을 확인하기 위해, 저장소의 무결성 검사

git fsck

로컬 저장소가 손상되었을 가능성이 있다면, 클린 상태의 새로운 클론을 만들고 필요한 데이터만 다시 푸시

git clone --mirror <repository_url>

트리 정리

git filter-branch --index-filter 'git rm --cached --ignore-unmatch <file_path>' -- --all

새로운 리포지토리를 생성하고 중요한 파일만 다시 클론

git clone --mirror <repository_url>

git push 프로토콜 변경

git config --global push.default simple
git config --global http.postBuffer 524288000

 

 

 

또는 SSH를 이용한 해결 방법도 있다.

인증 문제 등이 발생하면 HTTP 대신 SSH 프로토콜을 사용하여 저장소에 연결해서 복제를 시도한다.

ls -al ~/.ssh

먼저 SSH키가 존재하는지 확인한다. 없다면 새로 생성해줘야한다.

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

키를 생성할 때, 기본 경로 (~/.ssh/id_rsa)를 사용하고, 패스프레이즈는 원한다면 비워 둘 수 있다.

SSH 키가 이미 있거나 새로 생성한 후, 공개 키를 GitHub에 등록해야한다.

cat ~/.ssh/id_rsa.pub

https://github.com/settings/keys

 

GitHub: Let’s build from here

GitHub is where over 100 million developers shape the future of software, together. Contribute to the open source community, manage your Git repositories, review code like a pro, track bugs and fea...

github.com

위 코드로 나온 값을 복사하고 사이트에 들어가 New SSH KEY를 눌러서 저장을 해준다.

ssh -T git@github.com

이 코드를 실행했을 때 Hi username! You've successfully authenticated. 가 나오면 성공!

git push --mirror

이후 다시 복제를 시도해본다.

 

 

나는 SSH 시도와 무결성 검사에서 Git이 동일한 파일을 중복으로 트리 객체에 포함시킨 상태로 인한 문제가 발생했었다. 이때 적용해본 방법이다.

오류 내용은 이렇게 나온다.

 remote: error: object <id>: duplicateEntries
 error in tree <id>: duplicateEntries: contains duplicate file entries

윗줄은 SSH로 push 시도 시에 나온 결과이고, 아래는 git frsk로 발견한 문제이다.

중복된 파일을 제거하고 기록을 수정할 수 있는 git filter-repo를 사용하여 중복 파일 문제를 해결하기 위해 설치를 해준다.

pip install git-filter-repo
git filter-repo --path-glob '<path_to_problematic_files>' --invert-paths

이 명령은 <path_to_problematic_files>에서 중복된 파일을 Git 기록에서 완전히 제거한다. 

또는 아래 코드를 이용해서 문제를 일으키는 객체들을 수동으로 제거하거나 수정할 수 있다.

git replace --edit <id>

다시 git frsk로 무결성 검사를 하면 이전과 다르게 깔끔한 결과가 나오는 것을 확인할 수 있다.

$ git fsck
Checking object directories: 100% (/), done.
Checking objects: 100% (/), done.
Verifying commits in commit graph: 100% (/), done.

그런 다음 git push --mirror를 하면 잘 복제가 된 것을 확인할 수 있다.

 

반응형