Git 勉強会3
全体目標
gitを使って開発ができるようになる
方針
基本的にはCLI(コマンドベース)
CLIができたらGUIだってできるだろうって考え
今回の概要
- rebase
- コマンド一覧
- ssh
- githubからsshを使ってプロジェクトをclone
- stash
- プルリクベース開発
- blame
- diff
- 特定期間の変更一覧
- author修正
- grep
コマンド集
rebase
commitの履歴を編集する機能
head
〜2つのコミットを編集するとした場合
git rebase -i head~2
実行するとこんなこんな感じで表示される
pick 0c1b4b9 hoge追加
pick 9859ba9 hogehoge追加
pickの所を以下のコマンドに書き換えて保存するとrebaseが実行される
コマンド一覧
p, pick = use commit
r, reword = use commit, but edit the commit message
e, edit = use commit, but stop for amending
s, squash = use commit, but meld into previous commit
f, fixup = like "squash", but discard this commit's log message
x, exec = run command (the rest of the line) using shell
d, drop = remove commit
ssh
https
ではなく、ssh
を使って認証もできる
ssh
では公開鍵認証という技術を利用してる
git clone git@github.com:facebook/react.git
sshの場合の利点はリポジトリにアクセスする際にIDとパスワードを毎回求められません
githubからsshを使ってプロジェクトをclone
- 公開鍵作成
-
id_rsa
とid_rsa.pub
ができる-
id_rsa
が秘密鍵 -
id_rsa.pub
が公開鍵(publicの略)
-
-
- githubに公開鍵を登録
- 「clone or download」でsshのアドレスを選ぶ
stash
現在の作業を一時的に退避させるコマンドです
作業中に別のブランチで作業しないといけなくなった時に、移動しようとするとエラーが出る時があります
そんな時に使えるのがstash
コマンドです
git stash
Link: Stash | 逆引きGit | サルでもわかるGit入門
プルリクベース開発
githubの機能であるプルリクエストを使用した開発手法です
(github以外にもある 例:bitbucket, gitlab, gitbucket)
プルリクベースの開発モデルとしては「github flow」を検索してみましょう
blame
誰が何行目のコードをコミットしたのかという犯人探しのコマンド
git blame file_name
特定の行数を調べたい時は
# 5行目
git blame -L 5 file_name
# 先頭から5行目
git blame -L ,5 file_name
# 5行目から10行目まで
git blame -L 5,10 file_name
# 5行目から3行
git blame -L 5,+3 file_name
# 5行目までの3行
git blame -L 5,-3 file_name
diff
git diff --stat --name-only commitid1 commitid2
author修正
git commit --amend
ではcommiterは修正できてもauthorは修正できない
詳しくはLink先で
Link: CommitとAuthorを修正する時は--authorではなく--reset-authorを使おう
grep
普通のgrep
と違ってgitで管理しているファイルだけを対象にgrepできる
git grep -e 'regex'
# and や or を設定できる
git grep -e 'aaa' --and -e 'bbb' --or -e 'ccc'
-
-i / --ignore-case
大文字/小文字区別しない -
-n / --line-number
行数を表示する
regexの部分は正規表現を入れて検索する
switch
新しくブランチを切る時や、ブランチを切り替える時に使うことができる
# old
git checkout -b hoge
# new
git switch -c hoge
-c
は--create
の略
ブランチの切り替えは
# old
git checkout master
# new
git switch master
restoreコマンド
変更したファイルを変更前の状態(直前のコミット)に戻す時に使用する
# old
git checkout hoge.txt
# new
git restore hoge.txt