Git Git/SourceTree使用方法
目次
個人での利用
リモートリポジトリ(remote)
git remote -v
ステージング(add)
コミット(commit)
git commit -m "コメント"
	コンフリクト解消後のコミット
	git commit
	コミットコメント変更
	git commit --amend
	viコマンドで変更して保存
コミットログ(log)
git log
	特定ファイル
	git log 〜/〜.php
	パス指定
	git log -p 〜/〜
	特定ブランチ
	git log --first-parent staging
	まとめる
	git log --word-diff -p 〜/〜php
	古い順に並べる
	git log --reverse
	日付指定
	git log --after '2023/03/01' --before '2023/03/31'
	ユーザー指定
	git log --committer=aaa
	git log --author=aaa
	マージ除く
	git log --no-merges
	git log --merges(マージコミットのみ)
	コメントのみ
	git log --oneline
グレップ(grep)
git grep
	単語単位
	git grep -w '検索文字'
	正規表現
	git grep -E '検索文字'
	&検索
	git grep -e '検索文字1' --and -e '検索文字2'
	パス指定
	git grep '検索文字' -- 'app/controllers/'
	パス除外
	git grep '検索文字' -- ':!app/controllers/'
	行番号表示
	git config --global grep.lineNumber true
ショウ(show)
git showcommit abcdefg (HEAD -> staging, origin/staging)
Merge: aaaaa bbbbb
aaaa というコミットとbbbbというコミットがマージされてabcdefgになったという意味
	指定のコミットの状態表示
	git show コミットID(SHA-1)
コミット追跡(blame)
git blame 〜/〜.php
	行指定
	git blame 〜/〜.php -L 999
ブランチ(branch)
git branch
	ブランチ一覧(リモート含む)
	git branch -a
	現在のブランチ
	git branch --contains=HEAD
	現在のコミットからTESTブランチを作成
	git branch test
	beforeブランチからafterブランチを作成
	git branch after before
	ブランチを削除
	git branch -d test
	現在のブランチにマージされていないブランチ一覧
	git branch --no-merged
	現在のブランチにマージされているブランチ一覧
	git branch --merged
	リモートブランチを削除
	git push origin --delete branch_name
	ブランチ詳細一覧
	git show-branch
	親ブランチ
	git show-branch | grep '*'
ローカルブランチを複製
チェックアウト(checkout)
git checkout ブランチ名
	リモートブランチをローカルにチェックアウト
	git checkout -b ローカルブランチ名 origin/リモートブランチ名
	特定ファイルのみチェックアウト
	git checkout ローカルブランチ名 --ファイルパス
	特定ファイルのみ元に戻す
	git checkout --ファイルパス

過去/未来のコミットに移動
別ブランチのコミットに移動
→ 作業コピーのファイル状態は移動先のコミット状態に変更される
マージ(merge)
git merge FETCH_HEAD
	別ブランチを現ブランチにマージ
	git merge 別ブランチ
	コンフリクト時
	コンフリクトファイルを修正
	git commit
「別ブランチの状態に合わせる」も参照
リベース(rebase)
git rebase 別ブランチ
	コンフリクト等によって中断した場合の再開
	git rebase 
--continue
チェリーピック(cherrypick)
git cherry-pick [取込対象コミットID]
リセット(reset)
git reset (オプション) (戻す位置)戻す対象
| オプション | HEAD | インデックス | 作業ツリー | 
|---|---|---|---|
--hard | 
〇 | 〇 | 〇 | 
--mixed | 
〇 | 〇 | |
--soft | 
〇 | 
戻す位置
| 位置 | 機能 | 
|---|---|
| HEAD^ | 直前のコミット | 
| HEAD | 現在のコミット | 
	HEAD、インデックス、作業ツリーを戻す
	git reset --hard ~
	HEAD、インデックスを戻す
	git reset --mixed ~
	HEADを戻す
	git reset --soft ~
	HEAD^(直前のコミット)を戻す
	git reset --hard HEAD^
	コミット後の変更を戻す
	git reset --hard HEAD
	コミット後の変更を戻す(特定ファイルのみ)
	「特定ファイルのみ元に戻す」参照
	別ブランチの状態に合わせる
	git reset --hard origin/master
リバート(revert)
git revert コミットID
	コミットの打ち消し(ファイル単位)
	git checkout コミットID ファイル名
	git commit
	マージの打ち消し
	1) git show
	「ショウ」参照
	revertする際はaaaaaに戻すのか、bbbbbに戻すのかの選択が必要
	aaaaaを1、bbbbbを2で指定する
	2) git revert -m 1 abcdefg
履歴からファイル削除(filter-branch)
git filter-branch -f --tree-filter 'rm -f test/test.txt' HEAD
スタッシュ(stash)
チームでの利用
クローン(clone)
git clone https://github.com/パス.git
	ssh
	git clone git@github.com:パス.git
	git -c core.sshCommand="ssh clone git@github.com:パス.git -i ~/.ssh/秘密鍵.pem
フェッチ(fetch)
git fetch origin master
プル(pull)
git pull origin masterレポジトリ名設定
git remote add 別名 https://github.com/ユーザ名/レポジトリ.git【強制pull】
コンフリクトを無視してリモートに合わせる
①フェッチ
git fetch origin master
②リモート追跡ブランチのorigin/masterまで戻す
git reset --hard origin/master
「別ブランチの状態に合わせる」参照
プッシュ(push)
git push origin master
	ローカルのcommitをリモートへ強制更新
	git-hubのブランチでの設定が必要
	git push origin master -f
	リポジトリ名:originのmasterブランチへ、ローカルのtestブランチのコミットを更新
	git push origin test:master
他
別フォルダからGit操作
git -C フォルダ gitコマンド
git -C test branch

