LoginSignup
14
8

More than 3 years have passed since last update.

初心者がCodeCommitから新しいリポジトリを作成した後、クローンして作業ブランチを作る

Last updated at Posted at 2019-08-22

はじめに

今まで誰かが既に中に何かを作ったリポジトリからしかクローン・作業してきませんでした。
今回、自分でCodeCommitでリポジトリを作成するところからやってみて色々詰まったので、自分用のメモです。
そんな背景があるので、根本的に非効率・おかしい点があるかもしれません、その場合はすみません。

記事の前提

  • Mac PC
  • git, AWS CLIがインストール済み
  • IAMアカウントとSSHキーが紐付けてある
  • SSHキーを紐付けたIAMのクレデンシャルが登録してある

動作確認環境

  • バージョン
    • bash: GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
    • git: 2.17.0
    • AWS CLI: aws-cli/1.16.221 Python/3.7.4 Darwin/18.7.0 botocore/1.12.211

1. 新規リポジトリを作成する

  • AWSコンソールを開き、サービス一覧からAWS CodeCommitを選択し、「リポジトリを作成」

  • 作成したリポジトリのページを開き、「URLのクローン」 > 「SSHのクローン」
    (今回はSSHを前提に話します)

2. 新規作成したリポジトリをクローンする

  • コマンドライン上のリポジトリを落としてきたい場所で下記コマンドを実行
$ git clone ssh://git-codecommit.{region}.amazonaws.com/v1/repos/sample-repository
Cloning into sample-repository...
warning: You appear to have cloned an empty repository.

最後の warningでは「空のリポジトリ落としてきてるっぽいよ」て言われています。

  • リポジトリの中に移動
$ cd sample-repository

この状態のブランチを確認すると、何もいません。gitコマンドでは「マスターブランチにいる」と言われていましたが、ここでは表示されません。

$ git branch
# 何も表示されない
# gitステータス確認
$ git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)

# gitコミットlog確認
$ git log
fatal: your current branch 'master' does not have any commits yet
  • (任意)試しにSourcetreeで確認

Sourcetreeを見ても、何もできないし何もない。
sourcetree_initial_status.png

3. masterに最初のコミットをする

何も変更を行わずにコミットします。 --allow-emptyで空でもコミットできるようになります。

$ git commit --allow-empty -m "first commit"
[master (root-commit) xxxxxxx] first commit
  • コミット内容を確認
# gitステータス確認
$ git status
On branch master
Your branch is based on 'origin/master', but the upstream is gone.
  (use "git branch --unset-upstream" to fixup)

nothing to commit, working tree clean

# gitコミットlog確認
$ git log
commit xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (HEAD -> master)
Author: Your Name <Your Email Address>
Date:   Thu Aug 22 12:43:48 2019 +0900

    first commit

補足: --allow-emptyオプション

--allow-emptyオプションをつけない場合、何か新規ファイルがないと、下記のように表示され、コミットさせてくれないです。

$ git commit -m "First commit"
On branch master

first commit

nothing to commit

余談: Your branch is based on 'origin/master', but the upstream is gone.の意味

最初のコミットをした後の git statusで表示されるようになるこちら↓の意味を簡単に調べたので追記します。

Your branch is based on 'origin/master', but the upstream is gone.
(use "git branch --unset-upstream" to fixup)

今いるローカルの masterブランチは、もともとこのブランチのリモートの masterブランチ(= 'origin/master')から作られています (=Your branch is based on 'origin/master')。
が、そのリモートの masterブランチ自体の中身は空ですので、空=「存在しない(= upstream is gone)ブランチを追跡しているよ」と心配されます。

そして、git branch --unset-upstreamでこれを修復できる、つまりリモートの masterブランチの追跡をやめることができる、ということです。
また、 git branch --unset-upstream <branchname>、追跡をやめたいリモートブランチの指定をすることができるみたいです。

参考:
- "Your branch is based on 'origin/master', but the upstream is gone" - V&V
- git branch コマンド - Qiita

4. 最初のコミットをoriginに向けてpushする

  • 下記コマンドを実行
$ git push origin master
Counting objects: 2, done.
Writing objects: 100% (2/2), 147 bytes | 147.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To ssh://git-codecommit.{region}.amazonaws.com/v1/repos/sample-repository
 * [new branch]      master -> master

これが終わるとようやくブランチが現れます。

  • ブランチを確認
$ git branch
* master
  • (任意)Sourcetreeにブランチが現れたことを確認

sourcetree_after_first_commit.png

あとはSourceTreeなりコマンドラインなりCodeCommitなりで新規ブランチを生やしましょう!

  • 例:gitコマンドでmasterブランチから開発用ブランチ(例: develop)を作成・ブランチ切り替える場合
$ git checkout -b develop
Switched to a new branch 'develop'

# ブランチ確認
$ git branch
* develop
  master
  • (任意)gitで作成した開発用ブランチをSourcetreeで確認する

sourcetree_checkout_branch.png

余談: masterからはコミットしない場合

masterブランチからコミットしない方がいいとよく聞くので、私は最初、masterをいじらずそこから新しいブランチを作成・チェックアウトし、上記のようにpushしました。
すると、masterのリモートの存在が消えてしまい、作ったブランチからmasterを作成しないといけなくなって面倒でした。
また、この方法では、最後にmasterブランチを作り直した後、作業用ブランチに切り替えるのを忘れそうです。
masterでコミットする中身は空ですので、はじめに説明した方法で良さそうです。

  • 例: 開発用ブランチ(例: develop)から "first commit"した場合
# リポジトリ内に移動してからのコマンド

$ git checkout -b "develop"
Switched to a new branch 'develop'

$ git commit --allow-empty -m "first commit"
[develop (root-commit) 321da3b] first commit

$ git push origin develop
Counting objects: 2, done.
Writing objects: 100% (2/2), 148 bytes | 148.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To ssh://git-codecommit.{region}.amazonaws.com/v1/repos/another-sample-repository
 * [new branch]      develop -> develop


$ git checkout -b "master"
Switched to a new branch 'master'
Your branch is based on 'origin/master', but the upstream is gone.
  (use "git branch --unset-upstream" to fixup)

$ git branch --unset-upstream

# ブランチの切り替えを忘れずに
$ git checkout develop
14
8
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
14
8