Day 10 ~ 12:はじめての Git

/
#cebu#git

10日目

First

what is Git

プログラムのソースコードなどの変更履歴を記録・追跡するための分散型バージョン管理システム

prepare to use Git

undefined
1
git init

共同開発の流れ

  1. コード変更
  2. 共有準備
  3. 共有

コマンド

共有する準備

undefined
1
# 共有したいファイルを選択
2
git add index.html

コミット

選択したファイルを記録する

undefined
1
# git commit -m "appropriate message"
2
git commit -m "Create index.html"

共有

リモートを登録

リモートとは、Git 共有ファイルの置き場所で、origin はリモート名のこと

undefined
1
git remote add origin URL名

push: リモートにファイルアップロード

undefined
1
git putsh origin mastaer

pull: リモート上のファイルをDL

undefined
1
git pull origin master

その他コマンド

undefined
1
git status # display changed files
2
git diff # displya changed codes diff
3
git log # display commit history
4
git log -p # diplay commit history and changed contents

11日目

Github上でリポジトリ作成

Image from Gyazo

ローカルリポジトリ作成

undefined
1
echo "# self-repo" >> README.md
2
git init
3
git add README.md
4
git commit -m "first commit"
5
git remote add origin https://github.com/oriverk/self-repo.git
6
git push -u origin master
7
# =>
8
# Branch 'master' set up to track remote branch 'master' from 'origin'.

Gitリモートの確認

undefined
1
git remote -v
2
# =>
3
# origin https://github.com/oriverk/self-repo.git (fetch)
4
# origin https://github.com/oriverk/self-repo.git (push)

Git log

undefined
1
git log
2
# =>
3
# commit e2282c28ea661e588143201d9109ed9572f3d276 (HEAD -> master, origin/master)
4
# Author: oriverk <[email protected]>
5
# Date: Wed Mar 20 22:09:38 2019 +0000

自分で push してみる

undefined
1
touch tes1.txt
2
touch tes2.txt
3
4
git add tes1.txt
5
git add tes2.txt
6
git commit -m "second commit"
7
8
# =>
9
# [master 5b5b220] second commit
10
# 2 files changed, 0 insertions(+), 0 deletions(-)
11
# create mode 100644 tes1.txt
12
# create mode 100644 tes2.txt
13
14
git log
15
# =>
16
# commit 5b5b220fce07f6772b9aeeca1fe60d9ba0e63c74 (HEAD -> master)
17
# Author: oriverk <[email protected]>
18
# Date: Wed Mar 20 22:25:49 2019 +0000
19
# second commit
20
21
git push origin master
22
# =>
23
# Counting objects: 3, done.
24
# Compressing objects: 100% (2/2), done.
25
# Writing objects: 100% (3/3), 283 bytes | 141.00 KiB/s, done.
26
# Total 3 (delta 0), reused 0 (delta 0)
27
# To https://github.com/oriverk/self-repo.git
28
# e2282c2..5b5b220 master -> master
Image from Gyazo

interactive mode

対話モードの呼び出し

undefined
1
git add -i # interactive mode
2
# =>
3
# 1: status 2: update 3: revert 4: add untracked
4
# 5: patch 6: diff 7: quit 8: help

更新分をステージ

undefined
1
git add -i
2
# select no5( patch ) and then select index.txt
3
# => diplay diff
4
5
# githubに反映
6
git commit -m "cat pass"
7
git push origin master

ファイルを戻す

undefined
1
rm tes1.txt
2
# let file go back to previous ver.
3
git checkout tes1.txt
4
5
# let file go back to specific ver. file
6
git log
7
# commit 5b5b220fce07f6772b9aeeca1fe60d9ba0e63c74
8
# Author: oriverk <[email protected]>
9
# Date: Wed Mar 20 22:25:49 2019 +0000
10
11
# let file go back to the "second commit" ver. tes1.txt
12
git checkout 5b5b220fce07f6772b9aeeca1fe60d9ba0e63c7 tes1.txt
13
14
# to the latest ver.
15
git reset --hard HEAD
16
# =>
17
# HEAD is now at 48bfffa cat pass
18
# 最新版なので、tes1.txtも"cat pass"のバージョンに戻ってる

ブランチの確認、作成

現在のブランチを確認

undefined
1
git branch -v
2
# =>
3
# master 48bfffa cat pass

新規のブランチ作成

undefined
1
git switch -c other-bran

edit file and then push to other-bran

undefined
1
cat /etc/mysql/conf.d/mysql.cnf >> tes1.txt
2
cat /etc/apache2/conf-available/javascript-common.conf >> tes2.txt
3
cat /etc/passwd >> tes3.txt
4
cat /etc/nsswitch.conf >> tes4.txt
5
6
git commit -m :"for other-bran"
7
git push origin other-bran

リモートの変更をローカルに反映

undefined
1
# masterブランチに移動
2
git checkout master
3
# 反映
4
git pull origin master

認証情報の追加

push 時に毎回パスワ等聞かれるので、作成する

undefined
1
git config --global --edit
2
# ユーザ名とメールを記入し、コメントイン

sshのkeyを作成

すでに授業で作成したので、実行はパス

undefined
1
ssh-keygen -t rsa -b 4096 -C "自分の[email protected]"
2
cat ~/.ssh/id_rsa.pub

リポジトリの Setting 内の DeployKeys に上の cat の結果をコピーアンドペースト これで、push を、パスワなどを聞かれずに実行できる。


12日目

ブランチの確認

undefined
1
# 現在のブランチを確認
2
git branch -l
3
# 実行
4
master
5
* new-branch-jaji
6
7
# 現在のブランチとその情報を確認
8
git branch -v
9
# 実行
10
master 50dc6c7 added
11
* new-branch-joji be070c0 他のフィアルを追加
12
13
# リモートを含む、ブランチを確認
14
git fetch
15
git branch -a
16
17
# =>
18
# vagrant@vagrant:~/cebu_lessons/mactan-repo$ git branch -a
19
# master
20
# * new-branch-joji
21
# remotes/origin/master
22
# remotes/origin/new-branch-joji
23
# remotes/origin/test-repo

Git fetch

リモートブランチの最新の履歴だけを取得 これをした状態で Git merge をすると、git pull と同じ履歴になる

git pull = git fetch + git pull