コマンド チートシート
このシミュレーターで使えるコマンドを場面別にまとめました。すべて本物の Git と同じ書き方です。本物の Git で使う便利なオプションや、シミュレーターに無いコマンドは 実務でよく使うコマンド を参照してください。
始める
git init # このフォルダをリポジトリにする
git clone <URL> # リモートを複製する
git config user.name "名前" # 作者名(本物では --global を付けて一度設定)
git config user.email "メール"
git config --global init.defaultBranch main # 本物の Git で最初のブランチを main にする状態を見る
git status # いまの状態(迷ったらこれ)
git status -s # 短縮表示
git diff # ワークツリー ↔ ステージ
git diff --staged # ステージ ↔ HEAD
git diff <a> <b> # コミット/ブランチ間
git diff <a> <b> -- <file> # ファイルを絞る記録する
git add <file> # ステージに載せる
git add . # 全部載せる
git commit -m "メッセージ"
git commit -am "メッセージ" # 追跡済みファイルを add + commit
git commit --amend -m "..." # 直前のコミットを作り直す履歴を見る
git log # 詳細
git log --oneline # 1 行ずつ
git log --oneline --graph --all # 全ブランチをグラフで
git log -3 # 最新 3 件
git show <commit> # コミットの内容と差分
git show HEAD~1 # 1 つ前
git reflog # HEAD の移動履歴(復旧用)ブランチ
git branch # 一覧
git branch -a # リモート追跡ブランチも
git branch -v # 最新コミット付き
git switch -c <name> # 作って移動
git switch <name> # 移動
git switch - # 直前のブランチへ
git branch -d <name> # 削除(マージ済み)
git branch -D <name> # 強制削除
git branch -m <new> # 現在のブランチをリネームマージ・rebase
git merge <branch> # 現在のブランチに取り込む
git merge --no-ff <branch> # 必ずマージコミットを作る
git merge --abort # マージを中止
git rebase <branch> # 現在のブランチを branch の上に載せ直す
git rebase --continue # コンフリクト解消後に続行
git rebase --abort # rebase を中止
git cherry-pick <commit> # 特定のコミットだけ取り込む取り消す
git restore <file> # 編集を捨てる(⚠️)
git restore --staged <file> # ステージから降ろす
git reset --soft HEAD~1 # コミット取り消し、変更はステージに
git reset HEAD~1 # コミット取り消し、変更はワークツリーに
git reset --hard HEAD~1 # コミットも変更も捨てる(⚠️)
git reset --hard <commit> # そのコミットの状態に戻す(⚠️)
git revert <commit> # 打ち消すコミットを作る(push 済みはこれ)
git rm <file> # ファイルを削除して記録
git rm --cached <file> # 追跡だけ外す(ファイルは残す)
git mv <old> <new> # リネーム一時退避
git stash # 変更を退避
git stash list # 一覧
git stash pop # 戻して消す
git stash apply # 戻すが残す
git stash drop # 消すリモート
git remote -v # 登録一覧
git remote add origin <URL> # 登録
git push -u origin <branch> # 初回 push(送り先を記憶)
git push # 2 回目以降
git push origin --delete <branch> # リモートのブランチを削除
git fetch # 情報を取得(ファイルは変えない)
git pull # fetch + merge
git pull --rebase # fetch + rebase本物の Git で分岐した状態から git pull するには、先に git config --global pull.rebase false(merge)か true(rebase)を設定しておきます。
タグ
git tag # 一覧
git tag v1.0 # 現在のコミットにタグ
git tag -d v1.0 # 削除シェル(このシミュレーター)
ls / ls -a # ファイル一覧
cat <file> # 中身を表示
echo "text" > <file> # 書き込み(上書き)
echo "text" >> <file> # 追記
echo -e "1\n2" > <file> # 複数行
touch <file> # 空ファイル
rm <file> # 削除
vim <file> # エディタを開く
tree # ツリー表示
clear # 画面を消す(Ctrl+L)
help # コマンド一覧キーボード: ↑↓ で履歴、Tab で補完、Ctrl+C で入力を消す、Ctrl+L で画面クリア。
「これが分からない」の対応表
| 症状 | 見るところ |
|---|---|
| add と commit の違いが分からない | 3 つのエリア |
| HEAD~1 って何 | コミットと HEAD |
| ブランチを切り替えたらファイルが消えた | ブランチとマージ |
| CONFLICT と出た | コンフリクト |
| push が rejected された | リモートリポジトリ |
| PR って何をすればいいの | プルリクエスト |
| 壊した気がする | やり直しと復旧 |
| 入れたくないファイルが status に出る | .gitignore を使いこなす |
| 自分の PC で Git を始めたい | インストールと初期設定 |
| 用語の意味が分からない | 用語集 |