IT Guy

IT、AI / Machine Learning、IoT、Project Management、プログラミング、ITIL等々

Git / GitHub作業の流れ(基本コマンドまとめ)

1週間経つとまたド忘れするため(T_T)、自分用のメモ書きです。

シナリオ

やりたいことは、とりあえず個人用として、CodeをGitHubにあげておく。

全体の流れ

I. GitHubでRepository作成
II. Local PC側の作業 - 最初のみ
III. Local PC側の作業 - 日々の作業
IV. EclipseでGit

I. GitHubでRepository作成

1. GitHubCreate a new repositoryから新規Repositoryを作成しておく。

例)Repository「TestGit01」を作成

II. Local PC側の作業 - 最初のみ

  • 実はこの作業手順はRepository作成後、次の画面の「…or create a new repository on the command line」に詳細が書いてあるので、その通りにやればよい。

1. まずはローカルの作業フォルダーを作成

例)ローカルフォルダー「Local_TestGit01」を作成

2. Git bashでそのフォルダを開き、「git init」

例)

$ git init
Initialized empty Git repository in D:/A_Data/code/Local_TestGit01/.git/

3. 最初のファイルを作成し、「git add .」~「git commit -m "Commit Message"」

例)README.mdファイルをローカルで作成し、add - commit

$ git add .
$ git commit -m "1st Commit"
[master (root-commit) 489b5fc] 1st Commit
......
 1 file changed, 1 insertion(+)
 create mode 100644 README.md

4. GitHubへPush(「git remote add origin...」~「git push -u」)

いよいよローカルのGitからGitHubへPush!

$ git remote show  ←  一応、remoteの変数として何か登録されているのかを確認する

$ git remote add origin https://github.com/<GitHubユーザ名>/TestGit01.git

$ git push -u origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 206 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/<GitHubユーザ名>/TestGit01.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

以下のように正しくGitHubに反映されていればOK! image

III. Local PC側の作業 - 日々の作業

1. 後は、Codeなどある程度溜まったら、add ~ commit ~ pushの繰り返し

例)index.htmlファイルを追加し、add - commit - push

$ git add .

$ git commit -m "HTMLs Commit"
[master 54e51ca] HTMLs Commit
......
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 index.html

$ git push -u origin master

これで、以下のようにindex.htmlファイルも正しくGitHub側に追加された。 image

IV. EclipseでGit

1. GitHub側でRepository作成

例)Repository名:Java-Lang-Collection

2. Eclipse側での作業

Git Perspectiveで新規Repositoryを作成する。 image

Localのフォルダ名に注意。分かりやすく「C:\Users\Administrator\git{Git Hub Repository名}」に決める image

Java Projectを作成する。Locationに注意 image

後は、Eclipseでのコーディング後、都度、右クリック → Teamメニュー以下のコマンドリストでなんとかなる。

image

3. Eclipse側での作業(Local PC側の作業 - 日々の作業)

作業後、Projectフォルダ右クリック → Team → 「Add to Index」、「Commit...」の繰り返し。 Commitを選択すると、以下のように「Commit and Push...」ボタンがあるので、これでGitHubへまでPushできる。

image