LoginSignup
96
119

More than 1 year has passed since last update.

gitコマンド チートシート

Last updated at Posted at 2018-12-20

GIT チートシート

設定

用途 コマンド 備考
すべての設定の確認 git config -l --show-origin オプションで絞り込みが可能
--system:システム全体の設定
--global:ユーザ固有の設定
--local:リポジトリ固有の設定
特定の設定の確認 git config [設定項目]
設定の変更 git config --global [設定項目] [設置値] リポジトリ固有の設定の場合は--global--localに変更する
設定の削除 git config --global --unset [設定項目] リポジトリ固有の設定の場合は--global--localに変更する
ユーザ名設定 git config --global user.name "[name]"
メールアドレス登録 git config --global user.email "[email@address]"
コマンドラインの色 git config --global color.ui auto
ファイル名の大文字・小文字を検知できるようにする git config core.ignorecase false
短縮コマンドの登録
git [alias_command] で [git_command] が実行される"
git config --global alias.[alias_command] "[git_command]"
merge --no-ff をデフォルトにする git config --global --add merge.ff false
git config --global --add pull.ff only
チェックアウト時、コミット時の改行コード変換の設定を変更する git config --global core.autocrlf [値] ・true: チェックアウト時CRLF、コミット時LF
・input: チェックアウト時変換なし、コミット時LF
・false: 変換なし

リポジトリ作成

用途 コマンド
ローカルリポジトリ作成 git init
リモートからクローン作成 git clone [url]

確認

用途 コマンド
staged / modified / untracked の一覧 git status
[履歴] 現在のブランチの履歴 git log
[履歴] ファイルの履歴 git log --follow [file/path]
[差分] ブランチ間差分表示 git diff [first-brach] [second-branch]
[差分] ワーキングディレクトリ と ステージングエリア の差分を表示 git diff

ステージング

用途 コマンド
指定のファイルを staged にする(スペース区切りで複数選択) git add [file/path]
すべての modified を staged にする (--update) git add -u
すべての untracked とすべての modified を staged にする (--all) git add -A
カレントディレクトリのすべてのファイルを staged にする (cf. -A はリポジトリ全体) git add .
staged から modified にもどす git reset [file/path]
指定のファイルを部分的に staged にする git add -p [file/path]

コミット

用途 コマンド
コミット git commit -m "[comment]"
ステージング(untrackedは対象外) & コミット git commit -a -m "[comment]"
直前のコミットのコメントを修正する git commit --amend -m "[comment]"

チェックアウト

用途 コマンド
チェックアウト git checkout [branchname]
ブランチ作成 & チェックアウト git checkout -b [branchname]
特定のコミットからブランチを作成してチェックアウト git checkout -b [branchname] [コミットID]
リモートブランチからローカルブランチを作成してチェックアウト git checkout -b [branchname] [remote_branchname]

ブランチ

用途 コマンド
ローカルブランチの一覧 git branch
ローカルブランチとリモート追跡ブランチの一覧 git branch -a
リモート追跡ブランチの一覧 git branch -r
上流ブランチを確認する git branch -vv
ブランチ作成 git branch [branchname]
ブランチ名の変更 git branch -m [new_branchname]
ブランチ削除 git branch -d [branchname]
リベース (ブランチの付替え) git checkout [対象ブランチ]
git rebase [ブランチを生やすブランチ名]
リベースの中断 git rebase --abort
他ブランチのコミットを取り込む git cherry-pick [コミットID]
タグの作成 git tag [tagname]
リモートブランチからローカルブランチの作成 git branch [branchname] [remote_branchname]
リモートブランチの削除 git push --delete origin [remote_branchname]
ブランチの存在確認
git branch -a --format="%(refname:short)" | grep -e "^ブランチ名$" -i

マージ

マージはブランチの合流。リベースはブランチの付替え。

用途 コマンド 備考
マージ (ブランチの合流) git checkout [取込先ブランチ]
git merge [対象ブランチ] --no-ff
--no-ffをつけると必ずマージコミット
--ff-onlyをつけると必ずファストフォワード
マージの中止 git merge --abort マージでコンフリクト発生時、マージ前の状態に戻す(コンフリクト発生からなにも編集をしていない時)
マージの中止 git reset --hard HEAD マージでコンフリクト発生時、マージ前の状態に戻す(コンフリクト発生から編集を行ったが、コミットはしていない時)

リモートリポジトリ

用途 コマンド 備考
リモートリポジトリの一覧表示 git remote
リモートリポジトリのURLを一覧表示 git remote -v 一覧の(fetch)は取得用のURL、(push)は送信先のURLを表す。通常は一致する。
リモートリポジトリを切断する git remote remove [リモートリポジトリ名]
リモートリポジトリを追加する git remote add [リモートリポジトリ名] [URL]
リモートリポジトリのpush先だけ変更する git remote set-url --push [リモートリポジトリ名] [URL] URLをダミーの文字列にすると、pullは可能だが、push不可。
リモートリポジトリのすべてのブランチの更新履歴をリモート追跡ブランチに取り込む git fetch
リモート追跡ブランチを指定ローカルブランチにマージする git merge [origin/branchname] [branchname]
リモートブランチの変更をローカルブランチに反映させる・1 git fetch
git checkout [branchname]
git merge [origin/branchname]
リモートブランチの変更をローカルブランチに反映させる・2 git pull --all をつけると上流ブランチを設定しているローカルブランチすべてに反映させる
リモートブランチからローカルブランチの作成 git branch [branchname] [remote_branchname]
リモートブランチからローカルブランチを作成してチェックアウト git checkout -b [branchname] [remote_branchname]
現在のブランチの変更をリモートブランチに反映させる git push --all をつけるとすべてのローカルブランチを反映する
現在のブランチのリモートブランチを作成する git push --set-upstream origin [branchname]
リモートブランチの削除 git push --delete origin [remote_branchname]

ファイル編集・削除

用途 コマンド 備考
ファイル名の変更、ファイルの移動 git mv [old_file/path] [new_file/path]
ファイルを削除する git rm [file/path] ファイルを削除し、監視対象から外す。ファイルの指定にワイルドカードが使用可能。例:'*.xml'
ファイルを監視対象から外す git rm --cached [file/path] ファイルを残したまま、監視対象から外す。
ディレクトリを監視対象から外す git rm --cached -r [path]
ファイル名変更・コミット git mv [file_original] [file_renamed]
  • ファイル名を直接変更すると削除・作成とみなされることがある。ファイル名変更を追跡するにはgit rmを使用する。
  • ファイル名の大文字・小文字を変更する時、ファイル名を直接変更すると差分が認識されない。git rmを使用する。

スタッシュ(変更の一時保存)

用途 コマンド
modified と staged を保存し、HEADの状態までクリーンに戻す git stash
直近の記録をワーキングディレクトリに反映する (LIFO(後入れ先出し)) git stash pop
スタッシュの一覧を表示する git stash list
直近の記録を破棄する (LIFO) git stash drop

やり直す

用途 コマンド 備考
直前のコミットのコメントを変更する git commit --amend -m "[comment]"
commit_id 以降のすべてのコミットを取り消し git reset --soft [commit_id] ワーキングディレクトリとステージングエリアはリセットしない。
[commit_id]をHEAD^に変更すると直前のコミットを取り消す
commit_id 以降のすべてのコミットを取り消し git reset --hard [commit_id] ワーキングディレクトリとステージングエリアもリセットする。
[commit_id]をHEAD^に変更すると直前のコミットを取り消す
ローカルブランチをリモートブランチに一致させる git fetch
git reset --hard origin/branchname
マージを取り消す git merge --abort
コミットを打ち消すコミットをする git revert [commit_id]
ワークツリーの特定のファイルを以前の状態に戻す git checkout [commit_id] [file/path] [commit_id]を指定しなければ直前のコミットの状態に戻る。
直前のコミットに変更を追加する git add [file/path]
git commit --amend --no-edit
遡ってコメントを修正する git rebase -i HEAD~3
# viエディタで対象のコミットのpickをeditに変更し、保存
git commit --amend -m "[comment]"
git rebase --continue
削除したブランチを戻す git reflog
# viでHEADの履歴が表示される
# 消してしまったブランチの最後のコミットを見つけたらコミット番号をコピー
git branch [branchname] [コミット番号]

Appendix

用途 コマンド
コミット履歴を見やすくするコマンドを登録する git config --global alias.graph "log --graph --date-order --all --pretty=format:'%h %Cred%d %Cgreen%ad %Cblue%cn %Creset%s' --date=short"
コミット履歴を表示 git graph
viエディタの基本 編集モード: i
コマンドモード: Esc
保存: 「:w」
閉じる: 「:q」

新しいリポジトリを作成する

$ git init
$ git add -A
$ git commit -m "init"
$ git remote add origin [url]
$ git push -u origin master

すでに存在するローカルリポジトリをリモートにプッシュする

$ git remote add origin [url]
$ git push -u origin master

すでに存在するリモートリポジトリからローカルにコピーする

$ git clone [url]

参考

96
119
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
96
119