LoginSignup
13
10

More than 3 years have passed since last update.

GitHubからGitLabへGitのリポジトリを移動させる

Last updated at Posted at 2019-09-19

GitHubからGitLabへリポジトリを移動させる

GitHubもGitLabも好きだけど事情によりGitHubにあるリポジトリをGitLabへ移動させる必要があったのでその際の作業履歴を残しておきます。
※ここで行うのはmasterブランチしかないプロジェクトでの作業なので、他ブランチある場合は要注意です

1.GitHubから最新の状態を取得する

ローカルでfetchしてpullとかする

2.GitLabにプロジェクト作成

頑張って作成する。
ここではtest-projectというプロジェクトを作成したことにします

3.以下のコマンドを順番に実行していく

ローカルにあるプロジェクトディレクトリへ移動

$ cd [your-project-dir]

既存のリポジトリの名前(origin)を変更

$ git remote rename origin old-origin

リモートリポジトリを追加(GitLabのプッシュ先)

$ git remote add origin git@gitlab.com:**********/test-project.git

GitLabへプッシュ

$ git push -u origin --all
Enumerating objects: 1329, done.
Counting objects: 100% (1329/1329), done.
Delta compression using up to 4 threads
Compressing objects: 100% (1279/1279), done.
Writing objects: 100% (1329/1329), 459.36 KiB | 1.17 MiB/s, done.
Total 1329 (delta 880), reused 0 (delta 0)
remote: Resolving deltas: 100% (880/880), done.
To gitlab.com::**********/test-project.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

オプション

  • -u [--set-upstream] ・・・上流ブランチを設定する。
    これを設定しておけば今後はパラメータ不要でgit pushできるようになる

  • --all・・・すべてのブランチをpush

タグ情報をリモートに反映

$ git push -u origin --tags
Everything up-to-date

オプション

  • --tag・・・すべてのタグをpush

4.GitLabに作成したプロジェクトに反映されているか確認

一応コマンドからも確認しておく

$ git remote -v
old-origin      git@github.com:**********/github-no-test.git (fetch)
old-origin      git@github.com:**********/github-no-test.git (push)
origin  git@gitlab.com:********/test-project.git (fetch)
origin  git@gitlab.com:********/test-project.git (push)

番外編:git push -u origin --allで失敗する

$ git push -u origin --all
The authenticity of host 'gitlab.com (35.231.145.151)' can't be established.
ECDSA key fingerprint is SHA256:HbW3g8zUjNSksFbqTiUWPWg2Bq1x8xdGUrliXFzSnUw.(※)
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'gitlab.com,35.231.145.151' (ECDSA) to the list of known hosts.
git@gitlab.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

(※)念のためpush先を確認しておく つSSH host keys fingerprints

みたいなエラーが出る場合はSSH公開鍵がGitLabに登録されていないのが原因だと思うので
https://gitlab.com/profile/keys
から~/.ssh/id_rsa.pubの中身をコピーしてkey部分に張り付けて追加してください。
(ないなら作成する必要があります)

おわり

  • ちなみに当方WindowsユーザーでMS大好きです。
  • SurfaceLaptop3のUSキーボード版を日本でも出してくれ

参考

13
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
10