Skip to content

Git コマンド集

TIP

GitHubをgitでする場合はこちらのコマンドになります。

初期設定

sh
git config --global user.name "XXXX"
git config --global user.email "XXXX@hogehoge.com"

リポジトリの作成とプッシュ

sh
git init
git add .
git commit -m "Initial commit"
git remote add origin https://GitHub.com/XXXX/XXXXXX.git
git push -u origin master

リモートリポジトリの操作

  • クローン
    sh
    git clone https://GitHub.com/XXXX/XXXXXX.git
  • 変更の取得
    sh
    git pull
    # または
    git fetch
    git merge origin/master

ファイルの操作

  • ファイルの登録(コミットするため)
    sh
    git add <ファイル>
  • コミット
    sh
    git commit -m "コミットメッセージ"
  • 変更の確認
    sh
    git status
  • 差分の確認
    sh
    git diff <ファイル>

コミット履歴

  • 履歴の表示
    sh
    git log
  • 特定コミットの内容表示
    sh
    git show <コミットのハッシュ>

プッシュ

sh
git push origin <ブランチ>

変更の取り消し

  • add の取り消し

    sh
    git reset HEAD <ファイル>
  • commit の取り消し

    sh
    git reset --hard HEAD^
    • --hard: コミットとワークディレクトリの内容も書き換え
    • --soft: ワークディレクトリはそのままでコミットだけ取り消し
    • HEAD^: 直前のコミット
    • HEAD~{n}: n個前のコミット
  • commit の打ち消し

    sh
    git revert <コミットのハッシュ>
  • コミットメッセージの修正

    sh
    git commit --amend "新しいコミットメッセージ"
  • push の取り消し

    sh
    git reset --hard <戻したいコミットのハッシュ>
    git push -f

ブランチ操作

  • 作成
    sh
    git branch <ブランチ>
  • 切り替え
    sh
    git checkout <ブランチ>
  • 作成 & 切り替え
    sh
    git checkout -b <ブランチ>
  • 名前変更
    sh
    git branch -m <古いブランチ> <新しいブランチ>
  • 削除
    sh
    git branch -d <ブランチ>
  • リモートへ反映
    sh
    git push -u origin <ローカルのブランチ>
  • リモートブランチをローカルへ
    sh
    git branch <ブランチ> origin/<ブランチ>
  • リモートブランチをローカルへ & 切り替え
    sh
    git checkout -b <ブランチ> origin/<ブランチ>
  • 全ブランチ確認
    sh
    git branch -a
  • ブランチ比較
    sh
    git diff <ブランチ> <ブランチ>
  • マージ
    sh
    git merge <ブランチ>
  • 必ずマージコミットを作る
    sh
    git merge --no-ff <ブランチ>
  • リベース
    sh
    git rebase <ブランチ>

    ※ mergeの場合は分岐元、rebaseの場合は分岐先のブランチで実行

スタッシュ

  • 変更点を一旦退避
    sh
    git stash save
  • 一覧表示
    sh
    git stash list
  • 作業を戻す
    sh
    git stash apply <stash>
  • 作業を消す
    sh
    git stash drop <stash>
  • 全て消す
    sh
    git stash clear

ファイル操作

  • 削除
    sh
    git rm -f <ファイル>
  • リネーム
    sh
    git mv <元のファイル> <変えたいファイル>
  • 最新コミットの状態に戻す
    sh
    git checkout HEAD <ファイル>
  • 指定コミットまで戻す
    sh
    git checkout <コミットのハッシュ> <ファイル>
  • .gitignore を無視して追加
    sh
    git add -f <ファイル>
  • ディレクトリだけ登録(.gitkeepを作成)
    sh
    touch <ディレクトリ>/.gitkeep