0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Git 勉強会3

Posted at

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_rsaid_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
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?