Git コマンド チートシート

git config

名前の設定

git config --global user.name 'YOUR NAME'

メールアドレスの設定

git config --global user.email 'yourmail@example.com'

カラー表示にする

git config --global color.ui true 

綺麗にlogを表示するための設定

git config --global alias.graph "log --graph --date-order --all --pretty=format:'%h %Cred%d %Cgreen%ad %Cblue%cn %Creset%s' --date=short"

以降は

git graph

で綺麗にログを表示することもできる

pushの設定をmatching(手元にある全ての追跡ブランチの変更をリモートに反映する)に変更する

git config --global push.default matching

pushの設定をsimple(今選択されているブランチが追跡ブランチであれば、手元のブランチの変更内容をリモートに反映する)に変更する

git config --global push.default simple

git init

現在いるディレクトリに対応するリポジトリを作成する

git init

git status

現在のリポジトリの状態を表示する

git status

ワーキングツリーの状態を簡潔に表示する

git status -s

git add

ファイルに行った変更をステージに上げる

git add <file>

ディレクトリ内の全てのファイルに対して行った変更をステージに上げる

git add .

git rm

ファイルを削除し、そのファイルを削除したという変更をステージに上げる

git rm <file>

ファイルに対する変更をステージから下ろす

git rm --cached <file>

ディレクトに対する変更をステージから下ろす

git rm -r --cached <directory>

git commit

コミットする

git commit

メッセージと一緒にコミットする

git commit -m "メッセージ"

直前のコミットとまとめてコミットする (直前のコミットログは上書きされる)

git commit --amend

変更点を表示してコミットする

git commit -v

直前のcommitのAuthorを後から修正する

git commit --amend --reset-author

git log

履歴を見る

git log
git log --graph
git log --graph --pretty=format:'%s %d'

タグ情報を含めてログを表示する

git log --decorate

コミットメッセージの1行目のみを表示する

git log --pretty=short

指定したディレクトリ、ファイルのみのコミットログを表示する

git log <file or directory>

コミットログだけでなく、それぞれのコミットで生まれた差分も表示する

git log -p

特定のファイルに対するコミットログと、それぞれのコミットで生まれた差分を表示する

git log -p <file>

git checkout

fileに対する変更を無かったことにする

git checkout -- <file>

ディレクトリ内にある全てのファイルに対する変更を無かったことにする

git checkout -- .

ブランチを作ってそこへ移動する

git checkout -b <new branch>

リモートのブランチを元にローカルに新たなブランチを作成してチェックアウトする

git checkout -b <new local branch> <remote branch>

git reset

ファイルをunstageする

git reset HEAD <file>

現在のブランチを、特定のコミットまで戻す (一度pushなどを行って、ほかの開発者に公開されているコミットを取り消すことは絶対にしてはいけません)

git reset --hard <戻りたいコミットのid>

間違えてリセットしてしまった時に元に戻す(同上)

git reset --hard ORIG_HEAD

直前に行ったコミットそのものを取り消す(ワークディレクトリはそのまま)

git reset --soft HEAD~

git mv

ファイルをリネームし、その変更をステージに上げる

git mv <リネーム前のファイルの名前> <リネーム後のファイルの名前>

git branch

リポジトリに存在しているブランチの一覧を表示する

git branch

リモートリポジトリも含んだブランチ情報を表示する

git branch -a

新しいブランチを作る

git branch <new branch>

(master以外の)特定のブランチから新しくブランチを作る

git branch -b <新しく作るブランチの名前> <分岐元>

リモートブランチを追跡するブランチを作成する

git branch <手元のブランチの名前> <追跡したいリモートブランチの名前>

ブランチを削除する

git branch -d <branch to delete>

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

git branch -dr <remote tracking branch>
または
git branch -d -r <remote tracking branch>
または
git branch --delete -remotes <remote tracking branch>

手元のブランチをリモートのブランチに追跡させる

git branch --set-upstream-to=<追跡したいリモートブランチ> <手元のブランチ>

ローカルのブランチ名を変更する

git branch -m <old> <new>

// 現在使用しているブランチの名前を変更する場合は次のようにしてもok
git branch -m <new>

git merge

マージする

git merge <branch to merge>

マージを取り消す

git merge --abort

Fast-Forwardにならないようにブランチをマージする

git merge --no-ff <branch to merge>

ブランチ上の全てのコミットを一つにまとめて、今いるブランチ上にコミットする。 (マージするわけではないので注意)

git merge --squash <branch>

git rebase

リベース

git rebase master

リベースを無かったことにする

git rebase --abort

リベース時のコンフリクトを解消した後にもう一度リベースする

git rebase --continue

コンフリクトを解決した結果、一つ前のコミットとの違いがなくなってしまった場合にそのコミットをスキップする

git rebase --skip

過去のコミットをまとめる

git rebase -i <commit>

git clone

作業ディレクトリ付きリポジトリをクローンする

git clone 複製元 複製される先

リモジトリ内に含まれるサブモジュールも同時に複製する

git clone --recursive <repository> 

リポジトリのみクローンする

git clone --bare 複製元 複製される先

リポジトリをURLからクローンする

git clone <URL>

git remote

リモートリポジトリを表示する

git remote

リモートリポジトリを設定する

git remote add origin <リモートリポジトリの場所>

リモートリポジトリの追跡リポジトリを削除

git remote remove origin

git fetch

リモートリポジトリに存在するコミットやブランチを、ローカルのリポジトリに持ってくる

git fetch origin(リモートリポジトリの名前)

git push

リモートリポジトリに新しいブランチを複製する

git push origin <手元のブランチ>:<リモートに作りたいブランチ>

手元のブランチとリモート に作りたいブランチの名前が同じ場合は次のようにしてもよい

git push origin <ブランチの名前>

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

git push origin  :<消したいリモートブランチ>

または

git push --delete <消したいリモートブランチ>

リモートのmasterブランチを作る時に、直後に行うgit branch --set-upstream-to=origin/master masterもまとめて行う

git push -u origin master

リモートでタグを反映させる

git push origin <tag>

リモートのタグを削除する

git push --delete origin <tag>

タグを公開リポジトリに反映させる

git push --tags

git pull

fetchしてmergeする

git pull

特定のブランチに関してのみpullを行う

git pull origin <local branch>

git tag

現在のコミットに対して軽量タグをつける

git tag <tagname>

タグの一覧を表示する

git tag

注釈付きタグをつける

git tag -a <tagname>

注釈付きタグとコメントをつける

git tag -am "コメント" <tagname>

タグの一覧とコメントを表示する

git tag -n

タグを削除する

git tag -d <tagname>

git revert

特定のコミットを打ち消す

git revert <commit>

メッセージを編集してrevertする

git revert <commit> -e

または

git revert <commit> --edit

revert時にメッセージを編集しない

git revert <commit> --no-edit

git restore

ファイルの変更を無かったことにする

git restore <filename>

unstageする

git restore --staged <filename>

git diff

ワークツリーとステージ領域の差分を確認する

git diff

ワークツリーと最新コミットの差分を確認する

git diff HEAD

特定のファイルまたはディレクトリの差分を表示する

git diff <file or directory>

git cherry-pick

既に行った特定のコミットを再びそのブランチでコミットする

git cherry-pick <commit>

git show

HEADと作業ディレクトリの差分を表示する

git show

コミットの詳細を見る

git show <commit>

特定のtagを確認

git show <tag>

git ls-files

Gitでどれだけのファイルを管理しているのかを表示する

git ls-files

f:id:alberto_hojo:20200214223717p:plain

参考