見出し画像

Git 全コマンド一覧 早見表

Git公式の基本的なコマンドのサンプルを表示する 

​
git help everyday 


Git公式のヘルプを表示する


git help -g


コミットメッセージからコミットを検索する


git log -S'<a term in the source>'


リモートブランチと同期してローカルの変更を上書きする


git fetch origin && git reset --hard origin/master && git clean -f -d


特定のコミット時点で管理されている全ファイルを表示する


git ls-tree --name-only -r <commit-ish>


ファーストコミットを取り消す


git update-ref -d HEAD


全てのコンフリクトしているファイルを表示する


git diff --name-only --diff-filter=U


特定のコミットで変更されたファイルを表示する


git diff-tree --no-commit-id --name-only -r <commit-ish>


最終コミットからステージングされていない変更を表示する


git diff


コミット対象のステージングされている変更を表示する


git diff --cached

その他の方法:
git diff --staged

ステージングされている変更とされていない変更の両方を表示する


git diff HEAD


masterにマージ済みのブランチを表示する


git branch --merged master


前にいたブランチに素早く移動する


git checkout -
git checkout @{-1}


masterにマージ済みのブランチを削除する


git branch --merged master | grep -v '^\*' | xargs -n 1 git branch -d
git branch --merged master | grep -v '^\*\|  master' | xargs -n 1 git branch -d # will not delete master if master is not checked out

全てのブランチとその親ブランチを最新のコミットと共に表示する

git branch -vv

どのリモートブランチを追跡するか選択する

git branch -u origin/mybranch

ローカルブランチを削除する

git branch -d <local_branchname>

リモートブランチを削除する

git push origin --delete <remote_branchname>


その他の方法:


git push origin :<remote_branchname>

ローカルタグを削除する

git tag -d <tag-name>

リモートタグを削除する

git push origin :refs/tags/<tag-name>

ローカルで変更したファイルをHEADまで戻す

git checkout -- <file_name>

正反対のコミットを作ることで、元のコミットを取り消す

git revert <commit-ish>

コミットを破棄する。ローカルブランチのみで行うことを推奨

git reset <commit-ish>

直前のコミットのコメントを変更する

git commit -v --amend

現在のブランチのコミット履歴を表示する

git cherry -v master

直前のコミットの実行者を変更する

git commit --amend --author='Author Name <email@address.com>'

直前のコミットの実行者をデフォルト値にリセットする

git commit --amend --reset-author --no-edit

リモートブランチのURLを変更する

git remote set-url origin <URL>

現在設定されている全てのリモートブランチを表示する

git remote


その他の方法:


git remote show

ローカルとリモートの全ブランチを表示する

git branch -a

リモートのブランチを表示する

git branch -r

ファイル全体ではなく、ファイル中の一部変更点だけをステージングする

git add -p

gitの補完機能をインストールする

curl -L http://git.io/vfhol > ~/.git-completion.bash && echo '[ -f ~/.git-
completion.bash ] && . ~/.git-completion.bash' >> ~/.bashrc

直近2週間の変更点を表示する

git log --no-merges --raw --since='2 weeks ago'


その他の方法:


git whatchanged --since='2 weeks ago'

masterからforkした時点よりあとのコミット履歴を表示

git log --no-merges --stat --reverse master..

コミットを別のブランチにcherry-pickする

git checkout <branch-name> && git cherry-pick <commit-ish>

特定のコミットが含まれているブランチを表示する

git branch -a --contains <commit-ish>


その他の方法:


git branch --contains <commit-ish>


エイリアスを作成する

git config --global alias.<handle> <command> 


git config --global alias.st status

前回コミットからの変更を一時保存する

git stash
その他の方法:


git stash save

前回コミットからの変更のうち、ステージングされていない変更を一時保存

git stash -k


その他の方法:


git stash --keep-index


git stash save --keep-index


​

前回コミットからの変更をgit管理されていないファイルの変更も含めて一時保存する

git stash -u
その他の方法:


git stash save -u


git stash save --include-untracked

前回コミットからの変更を名前をつけて一時保存する

git stash save <message>

前回コミットからの変更を全て一時保存する(ignoredしている、git管理されていない、git管理されている)

git stash -a
その他の方法:


git stash --all


git stash save --all

現在保存されているstashを表示する

git stash list

stashリストから削除せずに変更を適用する

git stash apply <stash@{n}>

最後のstashを適用し、適用したstashは削除する

git stash pop
その他の方法:


git stash apply stash@{0} && git stash drop stash@{0}


stashを全て削除する

git stash clear
その他の方法:


git stash drop <stash@{n}>

stashから1ファイルだけ取り出す

git checkout <stash@{n}> -- <file_path>
その他の方法:


git checkout stash@{0} -- <file_path>

Git管理しているファイルを全て表示する

git ls-files -t

Git管理していないファイルを全て表示する

git ls-files --others

gitignoreで無視しているファイルを全て表示する

git ls-files --others -i --exclude-standard

リポジトリーからワーキングツリーを作成する(Git2.5)

git worktree add -b <branch-name> <path> <start-point>

HEADからワーキングツリーを作成する

git worktree add --detach <path> HEAD

ファイルをGit管理下から外す。ファイルは削除しない

git rm --cached <file_path>
その他の方法:


git rm --cached -r <directory_path>

Git管理していないファイル/ディレクトリを削除する前に、ドライランで削除される対象を表示する

git clean -n

Git管理していないファイルを強制削除する

git clean -f

Git管理していないディレクトリーを強制削除する

git clean -f -d

全てのサブモジュールをアップデートする

git submodule foreach git pull
その他の方法:


git submodule update --init --recursive


git submodule update --remote

現在のブランチでmasterにマージされていないコミットを表示する

git cherry -v master
その他の方法:


git cherry -v master <branch-to-be-merged>

ブランチ名を変更する


git branch -m <new-branch-name>


その他の方法:


git branch -m [<old-branch-name>] <new-branch-name>

featureブランチをmasterにrebaseして、その後masterにマージする

git rebase master feature && git checkout master && git merge -

masterブランチをアーカイブする

git archive master --format=zip --output=master.zip


前回のコミットをメッセージを除いて変更する

git add --all && git commit --amend --no-edit


リモートブランチで削除されているブランチへの参照を取り除く

git fetch -p
その他の方法:


git remote prune origin

一番最初のコミットのハッシュを取得する

git rev-list --reverse HEAD | head -1


その他の方法:


git rev-list --max-parents=0 HEAD


git log --pretty=oneline | tail -1 | cut -c 1-40


git log --pretty=oneline --reverse | head -1 | cut -c 1-40

バージョンツリーを描画する

git log --pretty=oneline --graph --decorate --all


その他の方法:


gitk --all


git log --graph --pretty=format:'%C(auto) %h | %s | %an | %ar%d'

reflogsから参照されているコミットを含めてバージョンツリーを描画する

git log --graph --decorate --oneline $(git rev-list --walk-reflogs --all)

指定したサブツリーだけをoriginにpushする

git subtree push --prefix subfolder_name origin gh-pages

サブツリーを現在のリポジトリに追加する

git subtree add --prefix=<directory_name>/<project_name> --squash 
git@github.com:<username>/<project_name>.git master

指定したサブツリーだけをpullする

git subtree pull --prefix=<directory_name>/<project_name> --squash 
git@github.com:<username>/<project_name>.git master

指定したブランチをファイルに出力する

git bundle create <file> <branch-name>

バンドルからインポートする

git clone repo.bundle <repo-dir> -b <branch-name>

現在のブランチ名を表示する

git rev-parse --abbrev-ref HEAD

コミット時に特定の1ファイルだけ無視する (e.g. Changelog)

git update-index --assume-unchanged Changelog; git commit -a; git 
update-index --no-assume-unchanged Changelog

rebaseする前にstashする

git rebase --autostash

rebaseする前にstashする

git fetch origin pull/<id>/head:<branch-name>
その他の方法:


git pull origin pull/<id>/head:<branch-name>

現在のブランチの一番直近のタグを表示する

git describe --tags --abbrev=0

単語単位のdiffを表示する

git diff --word-diff

一般的なdiffツールで変更を表示する

git difftool [-t <tool>] <commit1> <commit2> <path>


Git管理しているファイルの変更を無視する

git update-index --assume-unchanged <file_name>

unchangedを無効にする

git update-index --no-assume-unchanged <file_name>

.gitignoreに記載があるファイルを削除する

git clean -X -f

削除したファイルを元に戻す

git checkout <deleting_commit>^ -- <file_path>

特定のコミットハッシュまでファイルを戻す

git checkout <commit-ish> -- <file_path>

pullしたときにmergeではなくrebaseするよう変更する

git config --global pull.rebase true


その他の方法:

 #git  < 1.7.9


git config --global branch.autosetuprebase always

全てのコンフィグとエイリアスの一覧を表示する

git config --list

ファイル名の大文字小文字変更を検知するように変更する

git config --global core.ignorecase false

カスタムエディターを追加する

git config --global core.editor '$EDITOR'

誤字を自動修正する

git config --global help.autocorrect 1

どのブランチにいるかを表示する

git name-rev --name-only <SHA-1>


ドライラン(ドライランをサポートしているコマンドに対して実行できる)

git clean -fd --dry-run

コミットを前回のコミットの修正としてマークする

git commit --fixup <SHA-1>

rebase時に上記のコミット順を隣に並べ替えてわかりやすくする

git rebase -i --autosquash

指定したファイルだけコミットする

git commit --only <file_path>

インタラクティブにステージングする

git add -i

Gitが無視しているファイルを表示する

git check-ignore *

Gitが無視しているファイルのステータスを表示する

git status --ignored

Branch1には入っていて、Branch2に入っていないコミットを表示する

git log Branch1 ^Branch2

直近n個のコミットログを表示する

git log -<n>
その他の方法:


git log -n <n>

コンフリクトの解決法を記録して、今後同じ理由でのコンフリクトが見つかったら同じ方法で自動的に解決する

git config --global rerere.enabled 1

コンフリクトしたファイルをエディタで開く

git diff --name-only | uniq | xargs $EDITOR

現在のプロジェクトが使用しているオブジェクト数とディスク使用量を表示する

git count-objects --human-readable

到達不能オブジェクトを削除する

git gc --prune=now --aggressive

作業リポジトリを gitwebで一時的にブラウジングする


git instaweb [--local] [--httpd=<httpd>] [--port=<port>] [--browser=
<browser>]

コミットログで署名も表示する

git log --show-signature

グローバル設定を削除する

git config --global --unset <entry-name>

履歴を持たない新しい空ブランチを作成する



git checkout --orphan <branch_name>

別ブランチのファイルを表示する

git show <branch_name>:<file_name>

ルートとマージコミットのみ表示する

git log --first-parent

直前の2コミットをインタラクティブにrebaseする

git rebase --interactive HEAD~2

作業中のブランチを表示する

git checkout master && git branch --no-merged

二分探索でバグの発生元を発見する

git bisect start                    # Search start 


git bisect bad                      # Set point to bad commit 


git bisect good v2.6.13-rc2         # Set point to good commit|tag 


git bisect bad                      # Say current state is bad 


git bisect good                     # Say current state is good 


git bisect reset                    # Finish search 

pre-commitフックが仕掛けられているときに、チェックをスルーしてコミットする

git commit --no-verify

特定のファイルの変更履歴を表示する (リネームされていても)

git log --follow -p -- <file_path>

特定のブランチをクローンする

git clone -b <branch-name> --single-branch https://github.com/user/repo.git

新しいブランチを作って移動する

git checkout -b <branch-name>


その他の方法:


git branch <branch-name> && git checkout <branch-name>

コミット時にファイルモードの変更を無視する

git config core.fileMode false

ターミナルの出力に色を付けない

git config --global color.ui false

ターミナルの出力の色設定を個別に変更する

git config --global <specific command e.g branch, diff> <true, false or 
always>

ローカルブランチを新しい順に表示する

git for-each-ref --sort=-committerdate --format='%(refname:short)' 
refs/heads/

文字列か正規表現でGit管理下のファイルの行を検索する

git grep --heading --line-number 'foo bar'

最新のリポジトリだけcloneする

git clone https://github.com/user/repo.git --depth 1


指定の文字で全てのブランチのコミットログを検索する


git log --all --grep='<given-text>'


masterから指定のブランチに向けての最初のコミットを表示する

git log --oneline master..<branch-name> | tail -1


その他の方法:


git log --reverse master..<branch-name> | head -6

ステージングしたファイルをステージングする前に戻す

git reset HEAD <file-name>

リモートリポジトリに強制的にpushする


git push -f <remote-name> <branch-name>


リモートリポジトリを追加する


git remote add <remote-nickname> <remote-url>


ファイルの全行に対して、最終更新日と更新者を表示する

git blame <file-name>

ユーザ毎のコミット回数とメッセージを一覧表示する

git shortlog

誰かの作業を上書きしない場合、リモートに強制的にpushする

git push --force-with-lease <remote-name> <branch-name>

ユーザが貢献した行数を表示する

git log --author='_Your_Name_Here_' --pretty=tformat: --numstat | gawk '{ 
add += <!-- @doxie.inject start -->; subs += <!-- @doxie.inject end -->; loc 
+= <!-- @doxie.inject start --> - <!-- @doxie.inject end --> } END { printf 
"added lines: %s removed lines: %s total lines: %s


", add, subs, loc }' -
その他の方法:


git log --author='_Your_Name_Here_' --pretty=tformat: --numstat | awk '{ 
add += <!-- @doxie.inject start -->; subs += <!-- @doxie.inject end -->; loc 
+= <!-- @doxie.inject start --> - <!-- @doxie.inject end --> } END { printf 
"added lines: %s, removed lines: %s, total lines: %s


", add, subs, loc }' - # on Mac OSX

全てのマージをリバートする

git revert -m 1 <commit-ish>

ブランチのコミット回数を表示する

git rev-list --count <branch-name>

エイリアス git undo

git config --global alias.undo '!f() { git reset --hard $(git rev-parse --abbrev-
ref HEAD)@{${1-1}}; }; f'

オブジェクトノートを追加する

git notes add -m 'Note on the previous commit....'

全てのオブジェクトノートを表示する

git log --show-notes='*'

別のリポジトリからコミットを適用する

git --git-dir=<source-dir>/.git format-patch -k -1 --stdout <SHA1> | git am 

特定のリモートブランチからfetchする

git fetch origin master:refs/remotes/origin/mymaster

2つのブランチから共通の祖先を見付ける

diff -u <(git rev-list --first-parent BranchA) <(git rev-list --first-parent 
BranchB) | sed -ne 's/^ //p' | head -1

pushしてないコミットを表示する

git log --branches --not --remotes


その他の方法:


git log @{u}..


git cherry -v

空白以外の変更をapplyする

git diff --ignore-all-space | git apply --cached

ローカル/グローバルのGitコンフィグを編集する

git config [--global] --edit

指定範囲内をblameする

git blame -L <start>,<end>

Git変数を表示する

git var -l | <variable>


整形済みのパッチを作成する

git format-patch -M upstream..topic

リポジトリのルート絶対パスを取得する

git rev-parse --show-toplevel

特定期間のログを表示する

git log --since='FEB 1 2017' --until='FEB 14 2017'


指定者以外のログを表示する

git log --perl-regexp --author='^((?!excluded-author-regex).*)

ペンディング中の変更のまとめを作成

git request-pull v1.0 https://git.ko.xz/project master:for-linus

リモートリポジトリのブランチを表示する

git ls-remote git://git.kernel.org/pub/scm/git/git.git

Git管理していないファイルをバックアップする

git ls-files --others -i --exclude-standard | xargs zip untracked.zip

エイリアスを一覧表示する

git config -l | grep alias | sed 's/^alias\.//g'


その他の方法:


git config -l | grep alias | cut -d '.' -f 2

git statusを簡潔に表示する

git status --short --branch

昨日のコミットをチェックアウトする

git checkout master@{yesterday}

ローカルブランチをリモートリポジトリにpushして追跡する

git push -u origin <branch_name>

ブランチのベースを変更する

git rebase --onto <new_base> <old_base>

HTTPsではなくSSHをリモートへの接続に使用する

git config --global url.'git@github.com:'.insteadOf 'https://github.com/'

まとめ

使わない物が沢山ある様に思いますが、大体の動きを網羅していると思います。辞書として使用しているので、随時更新していきたいと思います。

この記事が参加している募集

noteの書き方

この記事が気に入ったらサポートをしてみませんか?