developer tip

일반 Git 저장소를 베어 저장소로 변환하는 방법은 무엇입니까?

copycodes 2020. 10. 3. 10:57
반응형

일반 Git 저장소를 베어 저장소로 변환하는 방법은 무엇입니까?


'일반'Git 저장소를 베어 저장소로 어떻게 변환 할 수 있습니까?

주요 차이점은 다음과 같습니다.

  • 일반 git 저장소에는 .git모든 관련 데이터를 포함하는 저장소 내부 폴더가 있으며 다른 모든 파일은 작업 복사본을 만듭니다.

  • 베어 Git 저장소에는 작업 복사본이 없으며 폴더 (라고 부르겠습니다 repo.git)에는 실제 저장소 데이터가 포함되어 있습니다.


간단히 말해서의 내용을 repo의 내용으로 repo/.git바꾼 다음 저장소에 이제 베어 저장소라고 알립니다.

이렇게하려면 다음 명령을 실행하십시오.

cd repo
mv .git ../repo.git # renaming just for clarity
cd ..
rm -fr repo
cd repo.git
git config --bool core.bare true

이것은 git clone --bare새로운 위치로하는 것과는 다릅니다 (아래 참조).


귀하의 방법은 작동하는 것처럼 보입니다. 베어 저장소의 파일 구조는 .git 디렉토리 내부에 있습니다. 하지만 실제로 변경된 파일이 있는지는 모르겠습니다. 실패하면

git clone --bare /path/to/repo

이름 충돌을 피하기 위해 다른 디렉토리에서 수행해야 할 수 있으며 원하는 위치로 다시 이동할 수 있습니다. 그리고 원본 저장소가있는 곳을 가리 키도록 구성 파일을 변경해야 할 수도 있습니다.


다음 링크가 도움이 될 것이라고 생각합니다.

GitFaq : 기존의 비 베어 리포지토리를 베어로 만들려면 어떻게합니까?

$ mv repo/.git repo.git
$ git --git-dir=repo.git config core.bare true
$ rm -rf repo

특별히 파일 시스템에서 비트를 조작 할 필요가없는 한, 베어가 아닌 저장소의 베어 버전을 만드는 것은 정말 간단합니다 (여기에 다른 여러 게시물에서 언급 됨). git의 핵심 기능의 일부입니다.

git clone --bare existing_repo_path bare_repo_path


사용을 고려하십시오

git clone --mirror path_to_source_repository

로부터 문서 :

소스 저장소의 미러를 설정합니다. 이것은 베어를 의미합니다. --bare와 비교하여 --mirror는 소스의 로컬 브랜치를 대상의 로컬 브랜치에 매핑 할뿐만 아니라 모든 참조 (원격 추적 브랜치, 메모 등 포함)를 매핑하고 이러한 모든 참조가되도록 refspec 구성을 설정합니다. 대상 저장소에서 git 원격 업데이트로 덮어 씁니다.


네트워크 경로의 저장소로 푸시하고 싶었지만 저장소가 베어로 표시되지 않는 한 git은 그렇게 할 수 없습니다. 내가 필요한 것은 구성을 변경하는 것뿐입니다.

git config --bool core.bare true

파일을 깨끗하게 유지하지 않으려면 파일을 조작 할 필요가 없습니다.


나는 대답을 읽었고 이것을했습니다.

cd repos
mv .git repos.git
cd repos.git
git config --bool core.bare true # from another answer
cd ../
mv repos.git ../
cd ../
rm -rf repos/ # or delete using a file manager if you like

이것은의 내용을 repos/.git맨손으로 남겨 둘 것입니다repos.git


Here's what I think is safest and simplest. There is nothing here not stated above. I just want to see an answer that shows a safe step-by-step procedure. You start one folder up from the repository (repo) you want to make bare. I've adopted the convention implied above that bare repository folders have a .git extension.

(1) Backup, just in case.
    (a) > mkdir backup
    (b) > cd backup
    (c) > git clone ../repo
(2) Make it bare, then move it
    (a) > cd ../repo
    (b) > git config --bool core.bare true
    (c) > mv .git ../repo.git
(3) Confirm the bare repository works (optional, since we have a backup)
    (a) > cd ..
    (b) > mkdir test
    (c) > cd test
    (d) > git clone ../repo.git
(4) Clean up
    (a) > rm -Rf repo
    (b) (optional) > rm -Rf backup/repo
    (c) (optional) > rm -Rf test/repo

Simply read

Pro Git Book: 4.2 Git on the Server - Getting Git on a Server

which boild down to

$ git clone --bare my_project my_project.git
Cloning into bare repository 'my_project.git'...
done.

Then put my_project.git to the server

Which mainly is, what answer #42 tried to point out. Shurely one could reinvent the wheel ;-)


Here is a little BASH function you can add to your .bashrc or .profile on a UNIX based system. Once added and the shell is either restarted or the file is reloaded via a call to source ~/.profile or source ~/.bashrc.

function gitToBare() {
  if [ -d ".git" ]; then
    DIR="`pwd`"
    mv .git ..
    rm -fr *
    mv ../.git .
    mv .git/* .
    rmdir .git

    git config --bool core.bare true
    cd ..
    mv "${DIR}" "${DIR}.git"

    printf "[\x1b[32mSUCCESS\x1b[0m] Git repository converted to "
    printf "bare and renamed to\n  ${DIR}.git\n"
    cd "${DIR}.git"
  else
    printf "[\x1b[31mFAILURE\x1b[0m] Cannot find a .git directory\n"
  fi
}

Once called within a directory containing a .git directory, it will make the appropriate changes to convert the repository. If there is no .git directory present when called, a FAILURE message will appear and no file system changes will happen.


The methods that say to remove files and muck about with moving the .git directory are not clean and not using the "git" method of doing something that's should be simple. This is the cleanest method I have found to convert a normal repo into a bare repo.

First clone /path/to/normal/repo into a bare repo called repo.git

git clone --bare /path/to/normal/repo

Next remove the origin that points to /path/to/normal/repo

cd repo.git
git remote rm origin

Finally you can remove your original repo. You could rename repo.git to repo at that point, but the standard convention to signify a git repository is something.git, so I'd personally leave it that way.

Once you've done all that, you can clone your new bare repo (which in effect creates a normal repo, and is also how you would convert it from bare to normal)

Of course if you have other upstreams, you'll want to make a note of them, and update your bare repo to include it. But again, it can all be done with the git command. Remember the man pages are your friend.


In case you have a repository with few local checkedout branches /refs/heads/* and few remote branch branches remotes/origin/* AND if you want to convert this into a BARE repository with all branches in /refs/heads/*

you can do the following to save the history.

  1. create a bare repository
  2. cd into the local repository which has local checkedout branches and remote branches
  3. git push /path/to/bare/repo +refs/remotes/origin/:refs/heads/

I used the following script to read a text file that has a list of all my SVN repos and convert them to GIT, and later use git clone --bare to convert to a bare git repo

#!/bin/bash
file="list.txt"
while IFS= read -r repo_name
do
 printf '%s\n' "$repo_name"
 sudo git svn clone --shared --preserve-empty-dirs --authors-file=users.txt file:///programs/svn/$repo_name 
 sudo git clone --bare /programs/git/$repo_name $repo_name.git
 sudo chown -R www-data:www-data $repo_name.git
 sudo rm -rf $repo_name
done <"$file"

list.txt has the format

repo1_name
repo2_name

and users.txt has the format

(no author) = Prince Rogers <prince.rogers.nelson@payesley.park.org>

www-data is the Apache web server user, permission is needed to push changes over HTTP


First, backup your existing repo:

(a)  mkdir backup

(b)  cd backup

(c)  git clone non_bare_repo

Second, run the following:

git clone --bare -l non_bare_repo new_bare_repo

Oneliner for doing all of the above operations:

for i in `ls -A .`; do if [ $i != ".git" ]; then rm -rf $i; fi; done; mv .git/* .; rm -rf .git; git config --bool core.bare true

(don't blame me if something blows up and you didn't have backups :P)


Wow, it's simply amazing how many people chimed in on this, especially considering it doesn't seem that not a single on stopped to ask why this person is doing what he's doing.

The ONLY difference between a bare and non-bare git repository is that the non-bare version has a working copy. The main reason you would need a bare repo is if you wanted to make it available to a third party, you can't actually work on it directly so at some point you're going to have to clone it at which point you're right back to a regular working copy version.

That being said, to convert to a bare repo all you have to do is make sure you have no commits pending and then just :

rm -R * && mv .git/* . && rm -R .git

There ya go, bare repo.

참고URL : https://stackoverflow.com/questions/2199897/how-to-convert-a-normal-git-repository-to-a-bare-one

반응형