LoginSignup
3
4

More than 3 years have passed since last update.

Gitコマンドリファレンス

Last updated at Posted at 2019-01-07

はじめに

Gitのコマンドを個人的なをまとめとして記載しています。
他にも便利なコマンドなどがあれば、コメントで教えて頂けると助かります。

Git初期設定

# ~/.gitconfigに追記される
$ git config --global user.name "your name"
$ git config --global user.email "your mail address"
$ git config --global fetch.prune true

.gitconfigに以下を追記

.gitconfig(抜粋)
[alias]
    st = status
    cm = commit
    cmm = commit -m
    ch = checkout
    br = branch
    ps = push
    pl = pull
    ad = add
    l = log
    ss = stash
    ssap = stash apply
    sspp = stash pop
    ssl = stash list
    ft = fetch
    rs = reset
    mg = merge

基本コマンド

  • リモートリポジトリをローカルにコピー
$ git clone git@XXXXXX.git
  • git によるリポジトリの開始
$ git init
  • 状態を確認する
$ git status
  • 変更をインデックスに追加(add)
# すべて追加
$ git add .

# 個別ファイルを追加
$ git add file_name
  • 変更をリポジトリに追加(commit)
$ git commit –m "message"
  • 変更をリモートリポジトリに追加
$ git push origin branch_name
  • コミットの履歴を確認
$ git log
  • 任意のファイルの修正履歴を確認
$ git log –p file_name

取り消し系コマンド

  • ワークツリーの内容を取り消す(注意:変更内容が消えます!!)
# すべての変更を取り消す
$ git checkout .

# 個別ファイルの変更を取り消す(Sourcetreeの破棄と同じ操作)
git checkout file_name
  • インデックスに追加した内容を取り消す(アンステージ)
# すべてアンステージ
$ git reset HEAD .

# 個別ファイルをアンステージ
$ git reset HEAD file_name
  • 直前のコミットを取り消す
# コミットのみを取り消す
$ git reset --soft HEAD^

# コミットとその内容すべてを取り消す(注意:変更内容が消えます!!)
$ git reset --hard HEAD^
  • コミットを取り消すコミットを作成する
$ git revert commit_number
  • 特定のコミットの状態に戻す・特定のコミットから新規ブランチ作成
# detatch HEAD の状態になりややこしいので非推奨
$ git checkout commit_number

# commit_numberの状態の新規ブランチを作成
$ git branch new_branch_name commit_number

ブランチ操作

  • カレントブランチから新規ブランチ作成
$ git branch branch_name

# 以下でもOK
$ git checkout –b branch_name
  • ブランチの一覧を表示
$ git branch

# リモートブランチを表示
$ git branch -r

# ローカル、リモートすべてのブランチを表示
$ git branch -a
  • ブランチを切り替える
$ git checkout branch_name
  • ブランチを削除する
$ git branch –d branch_name

# 取り込まれていないブランチを強制的に削除
$ git branch –D branch_name
  • リモートブランチをローカルに持ってくる
$ git checkout –b branch_name origin/branch_name
  • ブランチをマージする(マージ先に移動した状態で)
$ git merge branch_name
  • マージを取り消す
$ git merge --abort

リモート操作

  • リモートリポジトリを追加
$ git add remote origin git@XXXXX.git
  • リモートリポジトリの変更を取得
$ git fetch origin
$ git fetch origin branch_name
  • リモートリポジトリの変更をマージ
$ git merge origin/branch_name
  • リモートリポジトリの変更を取得し、マージする(fetch+merge)
$ git pull origin branch_name
  • リモートリポジトリの変更を取得し、マージする(リベースバージョン)
    ※不要なマージコミットを作成したくないときに利用します
$ git pull --rebase origin master

# 競合が起きた場合、修正した後
$ git rebase --continue
# リベースマージを取り消す
$ git rebase --abort
  • ローカルを強制的にリモートブランチに合わせる
$ git reset --hard origin/master
  • リモートブランチを削除する
$ git push --delete origin branch_name

表示関係

  • 任意のファイルの過去の状態を表示
$ git show commit_number:file_name
  • ワークツリーとインデックスの差分
$ git diff
  • HEADとインデックスの差分
$ git diff --cached
  • HEADとワークツリーの差分
$ git diff HEAD
  • コミット同士やブランチ同士の差分
    (ちなみに、Sourcetreeの場合は、Commandボタンを押しながら2つのコミットをクリックするとコミット間の全ての差分を表示できます)
# コミット同士の差分
$ git diff commit1 commit2

# ブランチ同士の差分
$ git diff branch1 branch2
  • 以前の状態と現在の状態のファイル単位の差分
$ git diff commit_number -- file_name

保存系コマンド

  • ワークツリーの変更を保存(スタッシュする)
$ git stash
  • スタッシュ一覧を表示
$ git stash list
  • 保存した内容(スタッシュ)を復元する
# 最新のスタッシュを適用し、削除する
$ git stash pop
# N番目のスタッシュを適用し、削除する
$ git stash pop stash@{N}

# 最新のスタッシュを適用し、残す
$ git stash apply
# N番目のスタッシュを適用し、残す
$ git stash apply stash@{N}
  • スタッシュを削除する
# 最新のスタッシュを削除
$ git stash drop
# N番目のスタッシュを削除
$ git stash drop stash@{N}
# スタッシュを全件削除
$ git stash clear

競合解決方法

  • 方法1:pullコマンドによる競合解決
# 1. リモートの変更を取り込む
$ git pull origin branch_name

# 2. ファイルが競合した場合、
# git statusでboth modifiedで表示されているファイルが競合したファイル
$ git status

# 3. 競合を手動で解決

# 4. addしてコミットする
$ git add file_name
$ git commmit -m "resolve conflict"
  • 方法2:mergeコマンドによる競合解決

# 1. リモートの変更を取得
$ git fetch origin branch_name

# 2. ブランチ間の差異を確認し、問題がないかを確認

# 3. マージ(以下どちらか)
$ git merge FETCH_HEAD
$ git merge origin/branch_name

# 4. ファイルが競合した場合、
# git statusでboth modifiedで表示されているファイルが競合したファイル
$ git status

# 5. 競合を手動で解決

# 6. addしてコミットする
$ git add file_name
$ git commmit -m "resolve conflict"
  • 方法3:rebaseコマンドによる競合解決
# 1. リモートの変更を取得
$ git fetch origin branch_name

# 2. リベース
$ git rebase origin/branch_name

# 3. ファイルが競合した場合、
# git statusでboth modifiedで表示されているファイルが競合したファイル
$ git status

# 4. 競合を手動で解決

# 5. addする(コミットは不要)
$ git add file_name

# 6. リベースを続ける
$ git rebase --continue
3
4
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
3
4