git-tips
git-tipsが本家です。
English | 中文 | Русский | 한국어 | Tiếng Việt
ツール:
git-tip -以下に紹介するtipsを実際に試すことができるCLIツールです。 (Here in Docker container)
tipsを追加したい場合はcontributing.mdを確認してください。
P.S: git version 2.7.4 (Apple Git-66)で動作検証をしています。
- GIT公式の基本的なコマンドのサンプルを表示する
- GIT公式のヘルプを表示する
- コミットメッセージからコミットを検索する
- リモートブランチと同期してローカルの変更を上書きする
- 特定のコミット時点で管理されている全ファイルを表示する
- ファーストコミットを取り消す
- 全てのコンフリクトしているファイルを表示する
- 特定のコミットで変更されたファイルを表示する
- 最終コミットからステージングされていない変更を表示する
- コミット対象のステージングされている変更を表示する
- ステージングされている変更とされていない変更の両方を表示する
- masterにマージ済みのブランチを表示する
- 前にいたブランチに素早く移動する
- masterにマージ済みのブランチを削除する
- 全てのブランチとその親ブランチを最新のコミットと共に表示する
- どのリモートブランチを追跡するか選択する
- ローカルブランチを削除する
- リモートブランチを削除する
- ローカルタグを削除する
- リモートタグを削除する
- ローカルで変更したファイルをHEADまで戻す
- 正反対のコミットを作ることで、元のコミットを取り消す
- コミットを破棄する。ローカルブランチのみで行うことを推奨
- 直前のコミットのコメントを変更する
- 現在のブランチのコミット履歴を表示する
- 直前のコミットの実行者を変更する
- 直前のコミットの実行者をデフォルト値にリセットする
- リモートブランチのURLを変更する
- 現在設定されている全てのリモートブランチを表示する
- ローカルとリモートの全部ランチを表示する
- リモートのブランチを表示する
- ファイル全体ではなく、ファイル中の一部変更点だけをステージングする
- GITの補完機能をインストールする
- 直近2週間の変更点を表示する
- masterからforkした時点よりあとのコミット履歴を表示
- コミットを別のブランチにcherry-pickする
- 特定のコミットが含まれているブランチを表示する
- エイリアスを作成する
- 前回コミットからの変更を一時保存する
- 前回コミットからの変更のうち、ステージングされていない変更を一時保存する
- 前回コミットからの変更をGit管理されていないファイルの変更も含めて一時保存する
- 前回コミットからの変更を名前をつけて一時保存する
- 前回コミットからの変更を全て一時保存する(ignoredしている、Git管理されていない、Git管理されている)
- 現在保存されているstashを表示する
- stashリストから削除せずに変更を適用する
- 最後のstashを適用し、適用したstashは削除する
- stashを全て削除する
- stashから1ファイルだけ取り出す
- Git管理しているファイルを全て表示する
- Git管理していないファイルを全て表示する
- Gitignoreで無視しているファイルを全て表示する
- リポジトリーからワーキングツリーを作成する(Git2.5)
- HEADからワーキングツリーを作成する
- ファイルをGit管理下から外す。ファイルは削除しない
- Git管理していないファイル/ディレクトリを削除する前に、ドライランで削除される対象を表示する
- Git管理していないファイルを強制削除する
- Git管理していないディレクトリーを強制削除する
- 全てのサブモジュールをアップデートする
- 現在のブランチでmasterにマージされていないコミットを表示する
- ブランチ名を変更する
- featureブランチをmasterにrebaseして、その後masterにマージする
- masterブランチをアーカイブする
- 前回のコミットをメッセージを除いて変更する
- リモートブランチで削除されているブランチへの参照を取り除く
- 一番最初のコミットのハッシュを取得する
- バージョンツリーを描画する
- reflogsから参照されているコミットを含めてバージョンツリーを描画する
- 指定したサブツリーだけをoriginにpushする
- サブツリーを現在のリポジトリに追加する
- 指定したサブツリーだけをpullする
- 指定したブランチをファイルに出力する
- バンドルからインポートする
- 現在のブランチ名を表示する
- コミット時に特定の1ファイルだけ無視する (e.g. Changelog)
- rebaseする前にstashする
- プルリクエストをID指定でローカルブランチにfetchする
- 現在のブランチの一番直近のタグを表示する
- 単語単位のdiffを表示する
- 一般的なdiffツールで変更を表示する
- Git管理しているファイルの変更を無視する
- assume-unchangedを無効にする
- .gitignoreに記載があるファイルを削除する
- 削除したファイルを元に戻す
- 特定のコミットハッシュまでファイルを戻す
- pullしたときにmergeではなくrebaseするよう変更する
- 全てのコンフィグとエイリアスの一覧を表示する
- ファイル名の大文字小文字変更を検知するように変更する
- カスタムエディターを追加する
- 誤字を自動修正する
- 指定のハッシュが、どのブランチにいるかを表示する
- ドライラン(ドライランをサポートしているコマンドに対して実行できる)
- コミットを前回のコミットの修正としてマークする
- rebase時に上記のコミット順を隣に並べ替えてわかりやすくする
- 指定したファイルだけコミットする
- インタラクティブにステージングする
- Gitが無視しているファイルを表示する
- Gitが無視しているファイルのステータスを表示する
- Branch1には入っていて、Branch2に入っていないコミットを表示する
- 直近n個のコミットログを表示する
- コンフリクトの解決法を記録してお、今後同じ理由でのコンフリクトが見つかったら同じ方法で自動的に解決する
- コンフリクトしたファイルをエディタで開く
- 現在のプロジェクトが使用しているオブジェクト数とディスク使用量を表示する
- 到達不能オブジェクトを削除する
- 作業リポジトリを gitwebで一時的にブラウジングする
- コミットログで署名も表示する
- グローバル設定を削除する
- 履歴を持たない新しい空ブランチを作成する
- 別ブランチのファイルを表示する
- ルートとマージコミットのみ表示する
- 直前の2コミットをインタラクティブにrebaseする
- 作業中のブランチを表示する
- 二分探索でバグの発生元を発見する
- pre-commitフックが仕掛けられているときに、チェックをスルーしてコミットする
- 特定のファイルの変更履歴を表示する (リネームされていても)
- 特定のブランチをクローンする
- 新しいブランチを作って移動する
- コミット時にファイルモードの変更を無視する
- ターミナルの出力に色を付けない
- ターミナルの出力の色設定を個別に変更する
- ローカルブランチを新しい順に表示する
- 文字列か正規表現でGit管理下のファイルの行を検索する
- 最新のリポジトリだけcloneする
- 指定の文字で全てのブランチのコミットログを検索する
- masterから指定のブランチに向けての最初のコミットを表示する
- ステージングしたファイルをステージングする前に戻す
- リモートリポジトリに強制的にpushする
- リモートリポジトリを追加する
- ファイルの全行に対して、最終更新日と更新者を表示する
- ユーザ毎のコミット回数とメッセージを一覧表示する
- 誰かの作業を上書きしない場合、リモートに強制的にpushする
- ユーザが貢献した行数を表示する
- 全てのマージをリバートする
- ブランチのコミット回数を表示する
- エイリアス git undo
- オブジェクトノートを追加する
- 全てのオブジェクトノートを表示する
- 別のリポジトリからコミットを適用する
- 特定のリモートブランチからfetchする
- 2つのブランチから共通の祖先を見付ける
- pushしてないコミットを表示する
- 空白以外の変更をapplyする
- ローカル/グローバルのgitコンフィグを編集する
- 指定範囲内をblameする
- Git変数を表示する
- 整形済みのパッチを作成する
- リポジトリのルート絶対パスを取得する
- 特定期間のログを表示する
- 指定者以外のログを表示する
- ペンディング中の変更のまとめを作成
- リモートリポジトリのブランチを表示する
- Git管理していないファイルをバックアップする
- エイリアスを一覧表示する
- git statusを簡潔に表示する
- 昨日のコミットをチェックアウトする
- ローカルブランチをリモートリポジトリにpushして追跡する
- ブランチのベースを変更する
- HTTPsではなくSSHをリモートへの接続に使用する
Git公式の基本的なコマンドのサンプルを表示する
git help everydayGit公式のヘルプを表示する
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 HEADmasterにマージ済みのブランチを表示する
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 -pgitの補完機能をインストールする
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-indexgit stash save --keep-index前回コミットからの変更をgit管理されていないファイルの変更も含めて一時保存する
git stash -uその他の方法:
git stash save -ugit stash save --include-untracked前回コミットからの変更を名前をつけて一時保存する
git stash save <message>前回コミットからの変更を全て一時保存する(ignoredしている、git管理されていない、git管理されている)
git stash -aその他の方法:
git stash --allgit stash save --all現在保存されているstashを表示する
git stash liststashリストから削除せずに変更を適用する
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 -tGit管理していないファイルを全て表示する
git ls-files --othersgitignoreで無視しているファイルを全て表示する
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 -nGit管理していないファイルを強制削除する
git clean -fGit管理していないディレクトリーを強制削除する
git clean -f -d全てのサブモジュールをアップデートする
git submodule foreach git pullその他の方法:
git submodule update --init --recursivegit 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 HEADgit log --pretty=oneline | tail -1 | cut -c 1-40git log --pretty=oneline --reverse | head -1 | cut -c 1-40バージョンツリーを描画する
git log --pretty=oneline --graph --decorate --allその他の方法:
gitk --allgit 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 Changelogrebaseする前にstashする
git rebase --autostashrebaseする前に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 -iGitが無視しているファイルを表示する
git check-ignore *Gitが無視しているファイルのステータスを表示する
git status --ignoredBranch1には入っていて、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 -3 -k特定のリモートブランチからfetchする
git fetch origin master:refs/remotes/origin/mymaster2つのブランチから共通の祖先を見付ける
diff -u <(git rev-list --first-parent BranchA) <(git rev-list --first-parent BranchB) | sed -ne 's/^ //p' | head -1pushしてないコミットを表示する
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.gitGit管理していないファイルをバックアップする
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 2git 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/'
Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
