👨‍💻

AWSの「Git+CodeCommit」のチュートリアルを学習してみた

2024/03/20に公開

AWSの「Git+CodeCommit」のチュートリアルを学習してみた

学習する経緯

これまでGitはBacklogでふわっと管理されているものしか使ったことがありませんでした。しかし、新規参画案件ではガッツリGit+CodeCommitの知識が求められるため、このチュートリアルを受講することになりました。
https://docs.aws.amazon.com/ja_jp/codecommit/latest/userguide/getting-started.html

チュートリアルの流れ

  1. CodeCommit リポジトリを作成する
  2. ローカルリポジトリを作成する
  3. 最初のコミットを作成する
  4. 最初のコミットをプッシュする
  5. CodeCommit リポジトリを共有し、別のコミットをプッシュしてプルする
  6. ブランチを作成して共有する
  7. タグを作成して共有する
  8. アクセス許可を設定する
  9. クリーンアップする

ステップ 1: CodeCommit リポジトリを作成する

AWSコンソールからCodeCommitに遷移し、チュートリアル通りにリポジトリを作成します。オプションは[Description (説明)] のみ追加しました。

ステップ 2: ローカルリポジトリを作成する

今回はHTTPS(GRC)で作成を進めていきます。
GRCはgit-remote-codecommitの略語で、IAMユーザー認証情報を利用しCodeCommitに接続できるようです。

まず、リポジトリのデフォルトブランチの名前をmainに設定します。リポジトリ毎に異なるデフォルトブランチ名を使用したい場合は、--globalではなく--localに設定します。

git config --global init.defaultBranch main

設定を確認します。

git config --global init.defaultBranch
main

次に、git cloneを実行します。URLは先ほど作成したリポジトリを選択して、[URLのクローン]から[HTTPS(GRC)]を選択すればコピーされます。

git clone codecommit::ap-northeast-1://MyDemoRepo my-demo-repo
Cloning into 'my-demo-repo'...
warning: You appear to have cloned an empty repository.

以下のような結果になればOKです。

ls -l
total 0
drwxr-xr-x. 3 ec2-user ec2-user 18 Mar 17 09:33 my-demo-repo

ステップ 3: 最初のコミットを作成する

  1. 作成したリポジトリ配下に移動します。
cd my-demo-repo/
  1. コミット用にテストファイルを2つ作成します。
touch cat.txt dog.txt
  1. テストファイルに内容を記載し、保存します。

cat.txt:

The domestic cat (Felis catus or Felis silvestris catus) is a small, usually furry, domesticated, and carnivorous mammal.

dog.txt:

The domestic dog (Canis lupus familiaris) is a canid that is known as man's best friend.
  1. git configを実行し、ユーザ名とEメールアドレスをローカルリポジトリに追加します。これにより、誰のコミットか識別しやすくなります。
git config --local user.name "your-user-name"
git config --local user.email your-email-address
  1. git addを実行して、変更をステージングします。ステージングとは、次のコミットに含める変更内容を一時的に保存する場所です。
git add cat.txt dog.txt
  1. git statusでステージングされた内容を確認します。
git status
On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   cat.txt
        new file:   dog.txt
  1. git commitを実行してコミットします。
git commit -m "Added cat.txt and dog.txt"
[main (root-commit) <commit-id-1>] Added cat.txt and dog.txt
 2 files changed, 2 insertions(+)
 create mode 100644 cat.txt
 create mode 100644 dog.txt
  1. git logでコミットした内容を確認します。
git log
commit <commit-id-1> (HEAD -> main)
Author: git-practice <mailaddress>
Date:   Sun Mar 17 09:47:30 2024 +0000

    Added cat.txt and dog.txt

ステップ 4: 最初のコミットをプッシュする

ローカルリポジトリから CodeCommit リポジトリにコミットをプッシュします。

git push -u origin main
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 2 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 328 bytes | 328.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
To codecommit::ap-northeast-1://MyDemoRepo
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.

コマンドの解説:

  • -u: 現在のローカルブランチとリモートブランチを関連づけます。今後、リモートとブランチを明示的に紐付けなくてもOKになります。
  • origin: リモートリポジトリの名前です。
  • main: プッシュ先のリモートブランチ名です。

ステップ 5: CodeCommit リポジトリを共有し、別のコミットをプッシュしてプルする

複数人開発を実施する場合、リポジトリに関する情報を他メンバーにも共有する必要があります。ここでは、別ディレクトリを作成して、仮想的に同じチームのメンバーと一緒に開発している工程をイメージしましょう。

  1. 別ディレクトリを作成し、移動します。
mkdir tmp
cd tmp/
  1. git cloneコマンドを実行します。MyDemoRepoはCodeCommitの名称で、shared-demo-repo/tmpディレクトリでGitが作成するディレクトリ名です。
git clone codecommit://MyDemoRepo shared-demo-repo
Cloning into 'shared-demo-repo'...
remote: Counting objects: 4, done.
Unpacking objects: 100% (4/4), 328 bytes | 328.00 KiB/s, done.
  1. クローンしたディレクトリに移動します。
cd shared-demo-repo
  1. git configで仮想メンバーのユーザ名とEメールアドレスを設定します。
git config --local user.name "other-user-name"
git config --local user.email other-email-address
  1. サンプルファイルを新規で追加し、内容を記載します。

horse.txt:

The horse (Equus ferus caballus) is one of two extant subspecies of Equus ferus.
  1. git addを実行して、変更を共有リポジトリにステージングします。
git add horse.txt
  1. git commitを実行し、変更を共有リポジトリにコミットします。
git commit -m "Added horse.txt"
[main <commit-id-2>] Added horse.txt
 1 file changed, 1 insertion(+)
 create mode 100644 horse.txt
  1. ローカルリポジトリ(shared-demo-repo)のデフォルトブランチから、CodeCommitリポジトリにプッシュします。
git push -u origin main
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 2 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 344 bytes | 344.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To codecommit://MyDemoRepo
   <commit-id-1>..<commit-id-2>  main -> main
branch 'main' set up to track 'origin/main'.
  1. ローカルリポジトリ(my-demo-app)に切り替えて、git pullを実行し変更を取り込みます。
cd ../my-demo-repo
git pull
remote: Counting objects: 3, done.
Unpacking objects: 100% (3/3), 344 bytes | 344.00 KiB/s, done.
From codecommit::ap-northeast-1://MyDemoRepo
   <commit-id-1>..<commit-id-2>  main       -> origin/main
Updating <commit-id-1>..<commit-id-2>
Fast-forward
 horse.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 horse.txt
  1. horse.txtが存在していればOKです。
ls
cat.txt  dog.txt  horse.txt
  1. git logでコミットを確認します。
git log
commit <commit-id-2> (HEAD -> main, origin/main)
Author: menberB <menberBmailaddress>
Date:   Sun Mar 17 13:24:07 2024 +0000

    Added horse.txt

commit <commit-id-1>
Author: git-practice <mailaddress>
Date:   Sun Mar 17 09:47:30 2024 +0000

    Added cat.txt and dog.txt

ステップ 6: ブランチを作成して共有する

このステップでは、ローカルリポジトリにブランチを作成して、いくつかの変更を加えた後、そのブランチをCodeCommitリポジトリにプッシュします。その後、CodeCommitリポジトリから共有リポジトリにブランチをプルします。

ブランチを使用することで、異なるバージョンのリポジトリの内容を個別に開発できます。複数人で開発する場合、お互いに影響を与えずに新しい開発作業を進めることができます。それぞれの機能開発が進んだ後、それらをより安定したブランチにマージ(統合)します。

  1. ローカルリポジトリからブランチの名前とローカルリポジトリで作成した最初のコミットIDを指定し、MyNewBranchを実行します。コミットのIDがわからない場合は、git logで取得可能です。
git checkout -b MyNewBranch <commit-id-1>
Switched to a new branch 'MyNewBranch'
  1. git pushを実行して、ローカルリポジトリに新しいブランチを送信します。
git push origin MyNewBranch
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 2 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 328 bytes | 328.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
To codecommit::ap-northeast-1://MyDemoRepo
 * [new branch]      MyNewBranch -> MyNewBranch
  1. shared-demo-repoに切り替えます。
cd ../tmp/shared-demo-repo/
  1. 新しいブランチをプルします。
git fetch origin
From codecommit://MyDemoRepo
 * [new branch]      MyNewBranch -> origin/MyNewBranch
  1. ブランチがプルされたことを確認します (git branch --allはリポジトリ内のすべてのブランチのリストを表示します)。
git branch --all
* main
  remotes/origin/HEAD -> origin/main
  remotes/origin/MyNewBranch
  remotes/origin/main
  1. 新しいブランチに切り替えます。
git checkout MyNewBranch
branch 'MyNewBranch' set up to track 'origin/MyNewBranch'.
Switched to a new branch 'MyNewBranch'
  1. ブランチを切り替えたことを確認します。
git status
On branch MyNewBranch
Your branch is up to date with 'origin/MyNewBranch'.

nothing to commit, working tree clean
  1. ブランチのコミットリストを表示します。
git log
commit <commit-id-1> (HEAD -> MyNewBranch, origin/MyNewBranch)
Author: git-practice <mailaddress>
Date:   Sun Mar 17 09:47:30 2024 +0000

    Added cat.txt and dog.txt
  1. mainブランチに戻り、そのコミットのリストを確認します。MyNewBranchは最初のコミットしか確認できなかったことに対して、mainブランチでは先ほどのhorse.txtのコミットが最新であることを確認します。
git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.

git log
commit <commit-id-2> (HEAD -> main, origin/main, origin/HEAD)
Author: menberB <menberbmailaddress>
Date:   Sun Mar 17 13:24:07 2024 +0000

    Added horse.txt

commit <commit-id-1>
Author: git-practice <mailaddress>
Date:   Sun Mar 17 09:47:30 2024 +0000

    Added cat.txt and dog.txt

ステップ6まで実施してみました。
これ以降のステップはまた今度更新しますね。

Discussion