githubへAndroid projectの追加

gitでrepositoryの作成からremoteへcommitまで行う


◆環境

PC:Ubuntu 16.04
Android Studio:3.1.3
Android version:5.0.1


◆repositoryの作成

1.githubのアカウントを取る
2.githubの右上の+マークからNew repositoryを選択
3.適当なRepository nameをつける
4.公開範囲は無料のPublicを選択
5.Initialize this repository with a READMEにチェックする
6.Add .gitignoreでJavaを選択
7.Add a licenseでApache License 2.0を選択
8.Create repositoryで完了


◆localのgitの準備

1.適当なディレクトリに移動し作成したrepositoryをcloneする
cloneするrepositoryのURLはrepositoryのページの右の方にあるClone or downloadボタンを押すとClone with HTTPSというポップアップみたいなのに書かれているのでそれをコピーする
ここではhome/gitディレクトリを作成

~/git$ git clone https://github.com/[git username]/[repository name].git

2.repositoryがgitディレクトリにダウンロードされてることを確認する
repository作成時に作ったREADME.mdとLICENSEがダウンロードされている

3.このディレクトリにAndroid Studioからプロジェクトを作成する

4.remoteのURLをgitに設定する
originというキーワードにgithubのURLを設定する

~$ git remote add origin https://github.com/[git username]/[repository name].git

◆gitのaddからremoteへのpushまで

1.git statusコマンドを使うとこのようにUntracked filesがたくさん出てくる

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .gradle/
        .idea/
        app/
        build.gradle
        ・
        ・
        ・

2.git branchコマンドで現在のbranchを確認する
masterになっている
これはgithubのrepositoryにmasterブランチしかなく、ここに何も編集していないAndroid projectを入れる

$ git branch
* master

3.Android projectをaddする
-Aオプションは全てのファイルをaddする

$ git add -A

4.commitする
commitはlocalのrepositoryに変更が追加される
-mオプションのあとにコメントをつける
-aオプションを使用するとviで変更履歴のファイルが開いてコメントを記入できる

$ git commit -m "Add a new project"

5.pushする
pushはremote(github)にファイルを追加する
originは先程設定したgithubのrepositoryのURLのことで、masterはbranch名
githubのorigin repositoryのmaster branchにAndroid projectのファイルが送られる

$ git push origin master

6.github上でファイルが追加されたことを確認する