LoginSignup
77
86

More than 5 years have passed since last update.

【これさえ使えれば大丈夫】よく使うgitコマンド全集

Last updated at Posted at 2016-05-24

はじめに

gitコマンドを、使用しそうなシーン別にまとめてみた。
これ以外にも様々なコマンドやオプションや存在するので、詳しくは公式のリファレンスを参考に。

gitの現状を確認する

version

バージョン確認
$ git --version

help

ヘルプの表示
$ git help <name_of_command>

status

コミット対象として指定されたファイルを確認
$ git status

log

全コミットログを確認
$ git log 
特定ファイルのコミットログを表示
$ git log <name_of_file>

config

現在の設定を確認
$ git config

show

指定対象の情報を確認
$ git show <object>

レポジトリに対する操作

init

ローカルレポジトリの作成
$ git init

remote

リモートレポジトリの作成
$ git remote add <name_of_repositry> <URL_of_repositry>
リモートレポジトリ一覧の表示
$ git remote
リモートレポジトリを削除
$ git remote remove <name_of_repositry>

clone

リモートレポジトリをクローン(ローカルレポジトリが同時生成される)
$ git clone git@github.com:hogehoge...

ブランチを操作する

branch

ブランチ一覧の表示
$ git branch
ブランチの作成
$ git branch <new_branch_name>
ブランチの名前変更
$ git branch -m <old_branch_name> <new_branch_name>
ブランチの削除
$ git branch -d <name_of_branch>
リモートのブランチ(ローカルには無いもの)を新しくローカルにコピー
$ git branch [local_branch_name] origin/[remort_branch_name]
#(ローカルでのチェックアウトも行う場合は branch -b で実行)

checkout

ブランチのチェックアウト(切り替え)
$ git checkout <wanna_channge_branch_name> 
ブランチを新規作成し、そのブランチにチェックアウト
$ git checkout -b <new_branch_name>
指定ファイルを作業ツリーに取り出す(ブランチのチェックアウトは行わない)
$ git checkout <name_of_file>
履歴を戻って、そこからブランチを作成
$ git checkout [commit_id] -b [new_branch_name]

プルする

pull

リモートレポジトリの内容をpull(指定がない場合はoriginが自動選択される)
$ git pull <name_of_repository>
プル実行後に自動コミットを行わない
$ git pull <name_of_repository> --no-commit

変更点を確認する

diff

現在の作業ツリー内で、まだindexにあがっていないファイルの変更点を表示
$ git diff
指定ファイルの変更履歴を表示
$ git diff <name_of_file>
2コミット間の指定ファイルの差分を表示
$ git diff <commit_ID> <commit_ID> <name_of_file>

fetch

リモートレポジトリの情報をローカルにダウンロード(.git内のFETCH_HEADに保存)
$ git fetch
フェッチ対象を指定して実行
$ git fetch <name_of_repository>

ファイルを検索する

grep

リポジトリ内から指定テキストに合致するファイルを探し、合致した行を表示
$ git grep <condition_of_search> 
$ git grep <condition_of_search> -chched (インデックスを対象に)
$ git grep <condition_of_search> -no-index (管理対象外ファイルを検索)

ファイルの名前を変更する

mv

ファイル名を変更する
$ git mv <old_name> <new_name>

変更点を一時保存する

stash

作業ツリーの変更点をstashに保存
$ git stash save <"message">
stashのリストを確認
$ git stash list
スタッシュを復元(指定がない場合は最後のスタッシュ)スタッシュは破棄する
$ git stash pop <name_of_stash>
最後のスタッシュを復元、スタッシュ自体は残る
$ git stash apply
stashを削除
$ git stash drop <name_of_stash>

ファイルを削除する

rm

ファイルを削除する(この履歴をコミットすることでリモートにも反映される)
$ git rm <name_of_file> <name_of_file> 
拡張子をしていして、まとめて削除する(例では拡張子txtをまとめて削除)
$ git rm *.txt 

コミットを打ち消す為のコミットを行う

revert

指定したコミットにおける変更を打ち消すコミットを実行
$ git revert <commit_ID>

コミットやaddを取り消す

reset

indexにあがったファイルの情報を取り消す(作業ツリーには影響なし)
$ git reset <name_of_file>
指定ファイルに関して、indexの内容を指定コミット時の状態まで戻す
$ git reset <commit_ID> <name_of_file>
作業ツリー、indexどちらも変更なしでコミットを取り消す
$ git reset --soft <commit_ID>
indexのみリセットしてコミットを取り消す
$ git reset --mixed <commit_ID>
indexをリセットし、作業ツリーを指定コミットの状態まで戻す
$ git reset --hard <commit_ID>
indexから指定ファイルを取り除く
$ git reset HEAD [file_name]

オプションに関しては、こちらが詳しくまとまっています。

  • やってしまったコミット(だけ)をなかったことにしたかったら、--soft
  • addもなかったことにしたかったら、オプションなし
  • ファイルの変更自体をなかったことにしたかったら、--hard

言い換えると

  • 現在のファイルの中身が変わっちゃ困るなら、--hardはやっちゃダメ
  • addしたものを忘れられちゃこまるなら、オプションなしでは危ない。--softをつけよう。
  • コミット位置を変えたくないなら、git reset使うな…ではなく、戻す先をHEADにしておけば安心。

なのだそう。

タグを付ける

tag

タグ情報の表示
$ git tag
直前のコミットに対してタグを付ける
$ git tag <name_of_tag>
コミットを指定してタグ付けを行う
$ git tag <name_of_tag> <commit_ID>
タグを削除する
$ git tag -d <name_of_tag>
タグを検証する
$ git tag -v <name_of_tag>

コミットする

add

コミットするファイルを指定して、indexにあげる
$ git add <name_of_file>
全ての変更をindexにあげる
$ gid add . 
ディレクトリを指定し、その配下のファイルを全てindexにあげる
$ git add <name_of_directry>/ 

commit

indexのファイルをコミットする
$ git commit -m <commit_message>
変更されている全てのファイルを、インデックスに上げる過程を省いてコミット
$ git commit -m <commit_message> -a  
直前のコミットを変更する形でコミット
$ git commit --amend

コミット履歴を書き換える

rebase

指定ブランチで行ったコミットを、その派生元ブランチに適応させる
$ git rebase <base_branch_name> <name_of_branch>
rebase中の競合発生した場合、今までの作業を全て中止する
$ git rebase --abort <base_branch_name> <name_of_branch> 
rebase中の競合発生で中断した作業を、続きから再開させる
$ git rebase --continue <base_branch_name> <name_of_branch> 

プッシュする

push

変更をリモートにプッシュする(指定が省略された場合はoriginが選択)
$ git push <name_of_repositry>
ローカルに存在する全てのブランチをプッシュする
$ git push --all
ローカルブランチがリモートブランチの子孫でない場合も、強制的にプッシュ
$ git push <name_of_repository> -f

プルリクエストを出す

request-pull

プルリクエストを出す(指定レポジトリに対して、最初のコミットから最後のコミットまで)
$ git request-pull <first_commit_ID> <URL_of_repository> <last_commit_ID>

マージする

merge

ブランチを指定してマージ
$ git merge <name_of_branch>
競合を起こした場合、マージを中止して元の状態に戻す
$ git merge --abort

mergetool

マージ時に発生した競合を解決するためのツールを起動
$ git mergetool -t <name_of_tool>
使用できるツール一覧を確認
$ git mergetool --tool-help
77
86
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
77
86