gitlab 이나 github 등을 이용하면서 git branch, tag, commit history 등 git repository 자체를 export 해서 다른 repository 에 import 해야할 경우가 있다.

이때 gitlab 이나 github 등의 UI 에서 export 를 제공해주는 경우도 있지만, 그러지 못한 환경일 경우, git command 로 export 하는 방법을 알아본다.

Export

1. --mirror 옵션으로 git clone

git clone --mirror {git repository url}
# ex)
$ git clone --mirror https://github.com/leechwin/leechwin.github.com
   --mirror
       Set up a mirror of the source repository. This implies --bare. Compared to --bare,
       --mirror not only maps local branches of the source to local branches of the
       target, it maps all refs (including remote-tracking branches, notes etc.) and sets
       up a refspec configuration such that all these refs are overwritten by a git remote
       update in the target repository.

2. git clone 을 수행하면 xxx.git 폴더가 생기고 다음과 같은 내용이 들어있다.

xxx.git
.
├── branches
├── config
├── description
├── HEAD
├── hooks
│   ├── applypatch-msg.sample
│   ├── commit-msg.sample
│   ├── post-update.sample
│   ├── pre-applypatch.sample
│   ├── pre-commit.sample
│   ├── prepare-commit-msg.sample
│   ├── pre-push.sample
│   ├── pre-rebase.sample
│   └── update.sample
├── info
│   └── exclude
├── objects
│   ├── info
│   └── pack
│       ├── pack-bf556429f20d728f5f1e40739c8e886b0ac1eca7.idx
│       └── pack-bf556429f20d728f5f1e40739c8e886b0ac1eca7.pack
├── packed-refs
└── refs
    ├── heads
    └── tags

3. 위의 xxx.git 폴더를 압축하여 필요한곳으로 파일로 전송할 수도 있다.

Import

1. export 된 파일의 압축을 풀고 해당 xxx.git 폴더안에서 다음명령을 통해 새로운 repository 로 연동할 수 있다.

# 폴더 안으로 이동
$ cd xxx.git

# 새로운 repository 설정
$ git remote set-url --push origin {new git repository url}
# ex)
$ git remote set-url --push origin https://github.com/leechwin/new.leechwin.github.com

# 새로운 repository 에 push
$ git push --mirror

다른 Git 간에 특정 branch 를 복사하고 싶을때

# 새로 이동될 git
$ git clone {git repository url}
$ cd xxx
# 복사할 대상 git 을 upstream 으로 설정
$ git remote add upstream {git repository url}
$ git fetch upstream

# 복사할 대상 git 의 branch 로 부터 branch 생성
# git checkout -b {생성할 branch 이름} {원격 저장소의 branch 이름}
$ git checkout -b test-branch upstream/test-branch
// 새로 추가된 branch를 upstream에서 내 repository의 remote를 따라가도록 설정
$ git branch -u origin/{생성할 branch 이름}
$ git push origin
Posted by leechwin
,