Gitコマンドチートシート

2025-09-18

開発環境

はじめに

開発時によく利用する Git コマンドを簡単にまとめます。

リポジトリの初期化

Terminal window
git init

ブランチの作成

Terminal window
git switch -c branch-name
# または
git branch branch-name

ブランチの切り替え

Terminal window
git checkout develop # develop ブランチに切り替え
# または
git switch develop # develop ブランチに切り替え

ブランチ一覧の表示

Terminal window
git branch

ブランチの削除

以下のコマンドで、ローカルリポジトリのブランチを削除します。

Terminal window
git branch -d branch-name

スカッシュ(Squash)

プッシュしていないローカルリポジトリの連続したコミットを1つのコミットにまとめることができます。

例えば、以下のようにfileB.txtに関するコミットを3つしていたとします。

Terminal window
git log --oneline
# result
e2653da (HEAD -> main) add see you in fileB.txt
0af67ed add hello in fileB.txt
a1c6e80 add fileB.txt
54aef51 add fileA.txt

それを以下の流れで1つのコミットにまとめることができます。

  1. git rebaseのiオプションでインタラクティブに操作します。
    Terminal window
    git rebase -i HEAD~3
  2. まとめるコミットの一番最初を”pick”にする(デフォルトでpickになっている)
  3. 2個目以降のまとめるコミットの先頭を”squash” or “s""にし、保存する。 変更前
    Terminal window
    pick a1c6e80 add fileB.txt
    pick 0af67ed add hello in fileB.txt
    pick e2653da add see you in fileB.txt
    変更後
    Terminal window
    pick a1c6e80 add fileB.txt
    s 0af67ed add hello in fileB.txt
    s e2653da add see you in fileB.txt
  4. コミットメッセージを記載する。

git logを見てみると3つのコミットが1つのコミットになっています。

Terminal window
git log --oneline
# result
c7a7339 (HEAD -> main) add fileB.txt and write message
54aef51 add fileA.txt

参考

同じカテゴリの記事