どうも、solobochiです。
4/11にデータアントレプレナーフェロープログラムのe-Learningが開始されました。
学んだことを色々とまとめていきたいと思います。
で、まずはGitHubから、と思ったのですが、そもそもGitすら使ったことがなく、GitとGitHubの違いからわからなかったので
まずは、Gitから。
目次
Gitとは?
Gitは、ローカル環境(ex.自分のPC環境)でリポジトリ管理をできるようにするためのバージョン管理システム。
※GitHubは、Gitを仕組みを利用して世界中のエンジニアがソースコードなどを公開できるような仕組みとしてGitHubができた。
引用:https://tracpath.com/bootcamp/learning_git_firststep.html
Gitの設定
・ユーザ名の設定
1 2 3 |
git config --global user.name "solobochi" |
・メールアドレス設定
1 2 3 |
git config --global user.email "xxxxxx@xxxxxx" |
・色の設定
1 2 3 |
git config --global color.ui true |
※コマンドごとに色分けすることも可能:https://qiita.com/kouchi67/items/ed9b51098075031b7323
リポジトリ管理
・開発環境からステージング環境へ(=ワークツリーからインデックス)(コミットファイルの指定)
1 2 3 |
git add abc.py |
・ステージング環境からリポジトリ環境へ(=インデックスからリポジトリ)
1 2 3 |
git commit abc.py |
リポジトリとは? リポジトリとは:プログラムの保存領域、貯蔵庫(←byサルでもわかるGit) └リモートリポジトリ:サーバに配置してみんなで共有する
└ローカルリポジトリ:それぞれのクライアント上で作業・保存する
引用:https://backlog.com/ja/git-tutorial/intro/intro1_2.html
Push/Pull
・Push(プッシュ):ローカルリポジトリからリモートリポジトリにアップロードする時のコマンド
・Pull(プル):リモートリポジトリからローカルリポジトリにダウンロードする時のコマンド
・Clone(クローン):リモートリポジトリを丸ごとダウンロードし複製する
共有リポジトリ管理
・共有リポジトリ作成
👉git init --bare
└ --bareをつけることで共有リポジトリ扱い(管理ファイルのみの扱いとなりコミットなどは行えなくなる)
└ git initだけだとローカルリポジトリの作成
👉git init --bare --shared/--shared=true
└ --sharedをつけることで複数人で共有できる
ローカルリポジトリ管理
・リーカルリポジトリ作成
👉git init
実際にやってみる
step
1パターン1 空ファイルを作成して、インデックスへ追加、インデックスからリポジトリへ追加
step
2パターン2 ファイルを修正して、インデックスへ追加、インデックスからリポジトリへ追加
step
3パターン3 別のファイルを作成して、インデックスへ追加、インデックスから共有リポジトリへ追加
パターン1 空ファイルを作成して、インデックスへ追加、インデックスからリポジトリへ追加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
git init ※いったんローカルリポジトリのみ touch sample.py $ git status On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) sample.py nothing added to commit but untracked files present (use "git add" to track) |
👉インデックスにアップする前にgit statusを確認すると、nothing added となる。
1 2 3 4 5 6 7 8 9 10 11 |
git add sample.py $ git status On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: sample.py |
👉インデックスにアップした状態でgit statusを確認すると、changes to be commitedとなる。
1 2 3 4 5 6 7 |
$ git commit -m "初版作成" [master (root-commit) 2670841] 初版作成 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 sample.py |
💡無事、commitできた。ローカルリポジトリへの登録完了!
1 2 3 4 5 |
$ git status On branch master nothing to commit, working tree clean |
👉リポジトリ登録後の状態をgit statusで確認すると、nothing to commitとなる。
1 2 3 4 5 6 7 8 9 |
$ git log commit 26708415d820bc56e965e6c17a4840ba83d25066 (HEAD -> master) Author: solobochi <xxxxx@xxxx> Date: Sat Apr 13 15:59:25 2019 +0900 初版作成 |
パターン2 ファイルを修正して、インデックスへ追加、インデックスからリポジトリへ追加
・先ほどのファイルを修正し、インデックスおよびリポジトリへ追加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
$ vi sample.py $ cat sample.py ##sample.py## $ git status On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: sample.py no changes added to commit (use "git add" and/or "git commit -a") |
👉インデックスに更新ファイルがないと、no changes added to commitとなる。
1 2 3 4 5 6 7 8 9 10 |
$ git add sample.py $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: sample.py |
👉インデックスに更新ファイルがあると、changes to be commited(コミットすべし)となる。
1 2 3 4 5 6 7 8 9 10 11 |
$ git commit -m "2019/04/13 ヘッダー追記" [master 9c2e48e] 2019/04/13 ヘッダー追記 1 file changed, 2 insertions(+) $ git status On branch master nothing to commit, working tree clean |
💡無事、commitできた。ローカルリポジトリへの登録完了!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$ git log commit 9c2e48eb1e6ec239b10c1a5320b9764e8c1fc38d (HEAD -> master) Author: solobochi <xxxx@xxx> Date: Sat Apr 13 16:27:13 2019 +0900 2019/04/13 ヘッダー追記 commit 26708415d820bc56e965e6c17a4840ba83d25066 Author: solobochi <xxxx@xxx> Date: Sat Apr 13 15:59:25 2019 +0900 初版作成 |
パターン3 既存ファイルを修正してインデックスへ追加、インデックスから共有リポジトリへ追加。
一方で別の新規ファイルを作成して、ワークツリーに放置。
・共有リポジトリ(リモートリポジトリ)作成
1 2 3 |
git init --bare --shared |
・既存ファイルの修正
1 2 3 4 5 6 7 8 9 10 11 12 |
$ vi sample.py $ cat sample.py ##sample.py## import os $ git add sample.py |
・別の新規ファイルを作成しておく
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
$ touch sample1.py $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: sample.py Untracked files: (use "git add <file>..." to include in what will be committed) HEAD config description hooks/ info/ sample1.py ^^^^^^^^^^^ |
1 2 3 4 5 |
$ git commit -m "import追記" [master 088306b] import追記 1 file changed, 2 insertions(+) |
👉修正してインデックスにアップした1ファイルのみ、commitされる
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$ git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) HEAD config description hooks/ info/ sample1.py nothing added to commit but untracked files present (use "git add" to track) |
👉インデックスにアップしていない新規ファイルは残存したまま。
これで、gitを使ったファイルのバージョン管理についてわかりました。
次回はGitHub。
以上です。