0
0

CodeCommitとGitLabを操作するテスト

Posted at

CodeCommitの設定

AWS CLIインストール
https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/getting-started-install.html

IAMユーザ→セキュリティ認証情報タブ→アクセスキー

aws configure

ここでアクセスキーを入力するよ。以下のファイルが生成される。これでawsにアクセスできるはず

疎通確認
aws ec2 describe-instances
\Users\XXXXX.aws/credential
[default]
aws_access_key_id = XXXX
aws_secret_access_key = YYY
\Users\XXXXX.aws/config
[default]
region = ap-northeast-1
output = json

これも必要になるかも
IAMユーザ→セキュリティ認証情報タブ→AWS CodeCommit の HTTPS Git 認証情報 (1)→生成ボタン押すとユーザIDとパスワードが表示される。gitの操作で必要

Git BASHのセットアップ

当作業はWindowsでGit BASHを使います。Macの場合はコマンドは流用できると思います。

移行元データ取得方法

git clone --mirror <移行元のリポジトリURL>
# git clone https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/test-mygration-zangief_master
cd <リポジトリ名>

httpsを使います。

移行元データの作り方(これはテストするときに使う)

本来はもともとあるリポジトリを使うのでこの作業はありません。

CodeCommitでデフォルトブランチがmasterのリポジトリを作る

ブランチ

  • master
  • dev
  • stg

手順

  1. リポジトリを作る
    CodeCommitのUI上でtest-mygration-zangief_masterを作る

  2. 準備

    git config --global core.autocrlf input
    

    改行コードを変換(LFをCRLFへ)しない設定です。Macの場合は必要がないはずです。

  3. データ登録

    # git clone
    git clone https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/test-mygration-zangief_master
    cd test-mygration-zangief_master
    # create branch
    # master
    git checkout -b master
    echo master > master.txt
    git add master.txt
    git commit -m "initial commit" master.txt
    git push origin master
    # dev
    git checkout master
    git checkout -b dev master
    echo dev > dev.txt
    git add dev.txt
    git commit -m "initial commit" dev.txt
    git push origin dev
    # stg
    git checkout master
    git checkout -b stg master
    echo stg > stg.txt
    git add stg.txt
    git commit -m "initial commit" stg.txt
    git push origin stg
    

CodeCommitでデフォルトブランチがmainのリポジトリを作る

ブランチ

  • main
  • dev
  • stg

手順

  1. リポジトリを作る
    CodeCommitのUI上でtest-mygration-zangief_mainを作る
    この状態ではブランチはない

  2. データ登録

    # git clone
    git clone https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/test-mygration-zangief_main
    cd test-mygration-zangief_main
    # create branch
    # main
    git checkout -b main
    echo main > main.txt
    git add main.txt
    git commit -m "initial commit" main.txt
    git push origin main
    # dev
    git checkout main
    git checkout -b dev main
    echo dev > dev.txt
    git add dev.txt
    git commit -m "initial commit" dev.txt
    git push origin dev
    # stg
    git checkout main
    git checkout -b stg main
    echo stg > stg.txt
    git add stg.txt
    git commit -m "initial commit" stg.txt
    git push origin stg
    

移行方針

移行方針1 (テスト)

ブランチ

  • 古くからあるリポジトリはmasterがデフォルトブランチ
  • masterブランチのまま移行する
master -> master
dev -> dev
stg -> stg

移行手順(テストで作ったデータを移行します)

  1. リポジトリ(project)を作る
    GitLabのUI上でNew projectCreate blank project
    Project name: test-mygration-zangief_master
    Visibility Level: private
    Initialize repository with a README: チェックを外す

  2. アクセスキー(パーソナルトークン)がいるかも
    pushするときに聞かれる。権限はapiでいいが、実際の現場ではパーソナルトークンは使わないかもしれない。現場の運用に合わせよう

    https://itpfdoc.hitachi.co.jp/manuals/JCS/JCSM71020001/function/git/accesstoken/index.html

  3. リモートURLを新しいリポジトリに設定

    cd test-mygration-zangief_master # CodeCommitからcloneしたリポジトリです
    git checkout master
    git remote set-url origin https://gitlab.com/hello512737/test-mygration-zangief_master.git
    git remote -v
    
  4. すべてのデータを新しいリポジトリにプッシュ

    git push origin master
    git push origin dev
    git push origin stg
    

    すべてのデータ(コミット履歴、ファイル、タグなど)を移行することが可能です

移行方針2(テスト)

ブランチ

  • 新しいリポジトリはmainがデフォルトブラン
  • そのまま移行する
main -> main
dev -> dev
stg -> stg

移行手順(テストで作ったデータを移行します)

  1. リポジトリ(project)を作る
    GitLabのUI上でNew projectCreate blank project
    Project name: test-mygration-zangief_main
    Visibility Level: private
    Initialize repository with a README: チェックを外す

  2. リモートURLを新しいリポジトリに設定

    cd test-mygration-zangief_main # CodeCommitからcloneしたリポジトリです
    git checkout main
    git remote set-url origin https://gitlab.com/hello512737/test-mygration-zangief_main.git
    git remote -v
    
  3. すべてのデータを新しいリポジトリにプッシュ

    git push origin main
    git push origin dev
    git push origin stg
    

    すべてのデータ(コミット履歴、ファイル、タグなど)を移行することが可能です

移行方針3(テスト)

ブランチ

  • 新しいリポジトリはmainがデフォルトブラン
  • デフォルトブランチをmainに変更する
master -> main
dev -> dev
stg -> stg

移行手順(テストで作ったデータを移行します)

  1. リポジトリ(project)を作る
    GitLabのUI上でNew projectCreate blank project
    Project name: test-mygration-zangief_master_main
    Visibility Level: private
    Initialize repository with a README: チェックを外す

  2. リモートURLを新しいリポジトリに設定

    cd test-mygration-zangief_master # CodeCommitからcloneしたリポジトリです
    git checkout master
    git remote set-url origin git remote set-url origin https://gitlab.com/hello512737/test-mygration-zangief_master_main.git
    git remote -v
    
    
  3. ローカルの master ブランチを main に名前変更します。このステップでローカルリポジトリのブランチ名が変更されます。

    git branch -m master main
    
  4. すべてのデータを新しいリポジトリにプッシュ

    git push origin main:refs/heads/main
    git push origin dev
    git push origin stg
    

    すべてのデータ(コミット履歴、ファイル、タグなど)を移行することが可能です

移行方針4(これが大事)

もともとあるリポジトリを移行する
masterをcloneして、mainに切り替えるパターンを行う。これが分かればmaster->mastermain->mainはわかるよね。

  1. リポジトリ(project)をcloneする

    git clone https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/test-mygration-zangief_master
    # リモートブランチの取得
    git fetch --all
    # ローカルブランチ作成
    git checkout -b dev origin/dev
    git checkout -b stg origin/stg
    
  2. ローカルの master ブランチを main に名前変更します。このステップでローカルリポジトリのブランチ名が変更されます。

    git branch -m master main
    
  3. すべてのデータを新しいリポジトリにプッシュ

    git push origin main:refs/heads/main
    git push origin dev
    git push origin stg
    

    すべてのデータ(コミット履歴、ファイル、タグなど)を移行することが可能です

その他

リポジトリ疎通確認

git ls-remote https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/test-codepipeline-zangief

圧縮・解凍方法

# 圧縮
tar -czvf test-codepipeline-zangief.tar.gz test-codepipeline-zangief

# 解凍
tar -xzvf test-codepipeline-zangief.tar.gz

GitLabのリポジトリ(project)の削除方法

UI上でリポジトリクリック→SettingsGeneralAdvancedDelete this projectDelete Project

pushのエラー例

@DESKTOP-TBB7HV0 MINGW64 ~/gitbash/test-mygration-zangief_master (master)
$ git push --mirror
Enumerating objects: 9, done.
Counting objects: 100% (9/9), done.
Delta compression using up to 16 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (9/9), 645 bytes | 645.00 KiB/s, done.
Total 9 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
remote:
remote: To create a merge request for dev, visit:
remote:   https://gitlab.com/hello512737/test-mygration-zangief_master/-/merge_requests/new?merge_request%5Bsource_branch%5D=dev
remote:
remote:
remote: To create a merge request for stg, visit:
remote:   https://gitlab.com/hello512737/test-mygration-zangief_master/-/merge_requests/new?merge_request%5Bsource_branch%5D=stg
remote:
To https://gitlab.com/hello512737/test-mygration-zangief_master.git
 * [new branch]      dev -> dev
 * [new branch]      master -> master
 * [new branch]      stg -> stg
 ! [remote rejected] origin/dev -> origin/dev (deny updating a hidden ref)
 ! [remote rejected] origin/master -> origin/master (deny updating a hidden ref)
 ! [remote rejected] origin/stg -> origin/stg (deny updating a hidden ref)
error: failed to push some refs to 'https://gitlab.com/hello512737/test-mygration-zangief_master.git'

このエラーは、--mirror オプションを使用してリモートリポジトリにすべての参照(ブランチ、タグ、リモート設定など)をプッシュしようとした際に、リモートでの特定の参照(hidden refs)が拒否されたことを示しています。これは通常、GitLabや他のリモートリポジトリが内部的に使用するための参照を含むためで、直接更新することはできません。

この問題を解決するには、以下のいずれかのアプローチを試してください。

アプローチ 1: --mirror を使わずに個々のブランチをプッシュする
--mirror を使用せず、個々のブランチを手動でプッシュします。

コードをコピーする
git push origin master
git push origin dev
git push origin stg

最近コマンドが違う

git switch --create main # checkoutの新しいコマンドかな
touch README.md
git add README.md
git commit -m "add README"
git push --set-upstream origin main # なんだろうか
0
0
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
0
0