LoginSignup
4
5

More than 5 years have passed since last update.

Gitコマンド一覧

Posted at

- 名前とメールアドレスの設定

$ git config --global user.name "Firstname Lastname"
$ git config --global user.email "your_email@example.com"

- 名前とメールアドレスの確認

$ git config user.name
$ git config user.email

- 設定内容の確認

$ git config --list
$ git config 設定名

- SSH Keyを作成

$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
オプション 説明
-t 鍵タイプを指定する
-b 鍵の長さを指定する
-C 鍵のコメントを新規作成する
  • 秘密鍵: id_rsa
  • 公開鍵: id_rsa.pub

- 公開鍵の内容を確認

$ cat ~/.ssh/id_rsa.pub

- 公開鍵の内容をクリップボードへコピー(Windows)

$ clip < ~/.ssh/id_rsa.pub

- GitHubと接続確認

$ ssh -T git@github.com

- 日本語の文字化け対策

$ git config --global core.quotepath false

- コマンドプロンプトの日本語の文字化け対策(一時的)

$ set LANG=ja_JP.UTF-8

- 改行コードの自動変換をOFFする

$ git config --global core.autocrlf false

- 出力内容の色分け

$ git config --global color.ui auto

- リポジトリを初期化

$ git init

- ステージング・エリアへファイルを追加

$ git add ファイル名
  • 作業ディレクトリ内の全てをステージング・エリアへ追加
$ git add -A

- コミット

  • 1行のコミットメッセージを記述
$ git commit -m "コミットメッセージ"
  • 詳細なコミットメッセージを記述
$ git commit

- addとコミットを同時に行う(追跡されたファイル全てが対象)

$ git commit -am "コミットメッセージ"

- 直前のコミットを取り消す

$ git reset --hard HEAD^

- 直前のコミットメッセージを修正

$ git commit --amend
$ git commit --amend -m "コミットメッセージ"

- コミット後の変更を取り消す

$ git reset --hard HEAD

- コミットを取り消す(指定したコミットの状態に戻す)

$ git reset --hard ハッシュ値

- HEADに複数のコミットをまとめる

$ git rebase -i HEAD~数字

先頭以外のpickをfixupと書き換えて保存する。

- 一時的に特定のコミットに移動する

$ git checkout ハッシュ値

- リポジトリで行われた作業のログとハッシュ値を全て確認

git resetを取り消したい場合に便利(Garbage Collectionに注意)

$ git reflog

- リポジトリの状態を確認

$ git status
$ git status -s
状態 説明
?? まだ追跡されていない新しいファイル
A ステージング・エリアに追加されたファイル
M 変更されたファイル
  • 1文字目はステージされたファイルの状態
  • 2文字はファイルが変更されたかどうか

- コミットログを確認

$ git log
  • ファイルの差分を表示
$ git log -p
$ git log -p ファイル名

- 変更差分を確認

  • 作業ディレクトリとステージング・エリアの変更差分
$ git diff
  • 作業ディレクトリと最新コミットの変更差分
$ git diff HEAD

- ブランチの一覧表示 & 現在のブランチを確認

$ git branch

- ブランチの作成

$ git branch ブランチ名

- ブランチの切替

$ git checkout ブランチ名

- ブランチを作成し、切り替える

$ git checkout -b ブランチ名

- 1つ前のブランチに切り替える

$ git checkout -

- ブランチをマージする

$ git checkout マージ先ブランチ
$ git merge --no-ff マージするブランチ名

- ブランチを削除する

$ git branch -d ブランチ名

- ブランチ名を変更する

$ git branch -m 旧ブランチ名 新ブランチ名

- ブランチのツリー表示

$ git log --graph

- コミットしていない状態の変更ファイルを全て退避する

$ git stash

- stashを一覧表示する

$ git stash list

- 退避したstashを取り出す

$ git stash apply stash@{数字}

- stashを削除する

$ git stash drop stash@{数字}

- stashの取り出しと削除を同時に行う

$ git stash pop stash@{数字}

- リモートリポジトリを登録

$ git remote add 追加するリモートリポジトリ名 git@github.com:ユーザ名/リポジトリ名.git
$ git remote add origin git@github.com:ユーザ名/リポジトリ名.git

- リモートリポジトリを削除

$ git remote rm リモートリポジトリ名
$ git remote rm origin

- リモートリポジトリを一覧表示

$ git remote
$ git remote -v

- リモートリポジトリ名を変更

$ git remote rename 旧リモートリポジトリ名 新リモートリポジトリ名

- リモートリポジトリへ送信

$ git push origin ブランチ名
$ git push origin master
  • 同時に上流ブランチとして設定
$ git push -u origin ブランチ名

- 上流ブランチの設定状況を確認

$ git branch -vv

- リモートリポジトリを含む全てのブランチを一覧表示

$ git branch -a

- リモートリポジトリを取得

$ git clone git@github.com:ユーザ名/リポジトリ名.git

- リモートリポジトリの別ブランチに切り替える(ローカルリポジトリにブランチ作成)

$ git checkout -b ブランチ名 リモートリポジトリ名/ブランチ名
$ git checkout -b fix-A origin/fix-A

- 最新のリモートリポジトリのブランチを取得(リモートリポジトリからローカルリポジトリを更新)

$ git pull リモートリポジトリ名 ブランチ名
$ git pull origin master
$ git pull origin fix-A
  • pull(fetch&merge)
$ git fetch リモートリポジトリ名

$ git fetch origin
$ git checkout master
$ git merge origin/master
4
5
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
4
5