Skip to content

Instantly share code, notes, and snippets.

@steakyukke
Created January 14, 2020 08:28
Show Gist options
  • Save steakyukke/8acf5237fbd3e5e38bdf804cc84d7e51 to your computer and use it in GitHub Desktop.
Save steakyukke/8acf5237fbd3e5e38bdf804cc84d7e51 to your computer and use it in GitHub Desktop.
Gitコマンド集

Git基本のコマンド

  • クローン

    • $ git clone <リポジトリ名>
  • ステージング

    • $ git add <ファイル名>
    • $ git add <ディレクトリ名>
  • ステージング(変更ファイル全て)

    • $ git add .
  • コミット

    • $ git commit -m "<メッセージ>"
  • 現在の変更状況を確認する

    • $ git status
  • ステージング前の変更差分を確認する

    • $ git diff
    • $ git diff <ファイル名>
  • ステージング後の変更分

    • $ git diff --staged
  • 変更履歴を確認する

    • $ git log
    • $ git log --oneline
    • $ git log -p <ファイル名>
    • $ git log -n <コミット数>
  • ファイルの削除を記録する

    • $ git rm <ファイル名>
    • $ git rm -r <ディレクトリ名>
  • ファイルの移動を記録する

    • $ git mv <旧ファイル名> <新ファイル名>
  • GitHubにリモートリポジトリを新規追加する

    • $ git remote add origin <リポジトリURL>
  • GitHubにプッシュ

    • $ git push <リモート名> <ブランチ名>
    • $ git push origin master
  • ファイルへの変更を取り消す

    • $ git checkout -- <ファイル名>
    • $ git checkuot -- <ディレクトリ名>
    • $ git checkuot -- .
  • ステージした変更を取り消す

    • $ git reset HEAD <ファイル名>
    • $ git reset HEAD <ディレクトリ名>
    • $ gir reset .
  • 直前のコミットをやり直す ※プッシュ前だと可能

    • $ git commit --amend

GitHubとやり取りするコマンド

  • リモート表示

    • $ git remote
    • $ git remote -v
  • リモートリポジトリを新規追加する

    • $ git remote add <リポジトリ名>
  • リモートから情報を取得する(フェッチ)

    • $ git fetch <リモート名>
  • リモートから情報を取得してマージする(プル)

    • $ git pull <リモート名> <ブランチ名>
    • $ git pull
    • ※下記コマンドと同じ - $ git fetch origin master - $ git merge origin/master
  • リモートの詳細情報を表示する

    • $ git remote show <リモート名>
      • 表示される情報
        • fetchとpushのURLリモートブランチ
        • git pull の挙動
        • git push の挙動
  • リモートの変更・削除

    • $ gir remote rename <旧リモート名> <新リモート名>

ブランチとマージのコマンド

  • ブランチを新規追加する ※ブランチの作成だけ、切替えはしない

    • $ gir branch <ブランチ名>
  • ブランチの一覧を表示

    • $ git branch
    • $ gir branch -a
  • ブランチを切り替える

    • $ git checkout <既存ブランチ名>
  • ブランチの作成と切替え (-bオプションをつける)

    • $ git checkout -b <新規ブランチ名>
  • 変更履歴をマージする ※作業中のブランチにマージする

    • $ git merge <ブランチ名>

    • $ git merge <リモート名/ブランチ名>

      • ex)$ git merge origin/master
  • ブランチを変更する ※作業中のブランチ

    • $ git branch -m <ブランチ名>
  • ブランチを削除する

    • $ git branch -d <ブランチ名>
    • 強制削除
      • $ git branch -D <ブランチ名>

リベースのコマンド

  • リベースで履歴を整えた形で変更を統合する
    • $ git rebase <ブランチ名>

タグ付けのコマンド

  • タグの一覧を表示する

    • $ git tag
    • $ git tag -l "検索文字列" <= 指定した文字列が含まれるタグを表示する
  • タグ作成

    • $ git tag -a [タグ名] -m “[メッセージ]"
  • タグのデータ表示(タグ作成者情報、日時、注釈、コミット)

    • $ git show [タグ名]
  • タグをリモートリポジトリに送信する

    • $ git push [ブランチ名] [タグ名]
    • $ git push origin [ブランチ名] --tags <= 未送信タグを一斉送信

スタッシュのコマンド

  • 作業を一次避難

    • $ git stash
  • 避難した作業を確認する

    • $ git stash list
  • 避難した作業を復元する

    • $ git stash apply <= 最新の作業を復元
    • $ git stash apply stash@{N} <= N番目の作業を復元
  • N番目のスたッシュしたファイルの変更差分を表示

    • $ git stash -p stash@{N}
  • スタッシュを全削除

    • $ git stash clear
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment