Gitの使い方逆引き(随時更新)

必要になった時にその都度調べたもの追記していくので、gitのバージョンアップなどで使用方法や、より新しい手法が増えている可能性が有ることに留意する。

リポジトリの作成

リポジトリを作成する。

> git init

グループパーミッションを付けてベアリポジトリを作成する。

> git init --bare --shared

リポジトリに後からグループパーミッションを付ける。

> git config core.sharedRepository group
> chmod -R g+ws hooks
> chmod -R g+ws info
> chmod -R g+ws objects
> chmod -R g+ws refs

Gitの設定

基本設定を見る。

> git config --global -l

基本設定をエディタで変更する。

> git config --global -e

リポジトリごとの設定を見る(.git/config)

> git config --local -l

リポジトリごとの設定をエディタで変更する(.git/config)

> git config --local -e

既定、およびそのリポジトリでのコミッタ名を設定する。

> git config --global user.name [committer name]
> git config --local user.name [committer name]

既定、およびそのリポジトリでのコミッタのメールアドレスを設定する。

> git config --global user.email [e-mail address]
> git config --local user.email [e-mail address]

ファイルのコミット、移動、削除

コミット対象とするファイルをステージにセットする。

> git add [target file]

コミット対象を行単位でステージにセットする対話モードを起動する。差分ごとに表示されるので、ステージするならy、しないならn、行単位で選択するならe

> git add -p

リポジトリとワーキングコピーのファイルのパスを移動する。

> git mv [target files] [destination dir]

ワーキングコピーのファイルを削除し、リポジトリの管理対象から除外する。

> git rm [target file]

よく使うオプション

  • --cached: ワーキングコピーのファイルを残したまま、リポジトリの管理対象から除外する。

ステージ上のファイルをコミットする。

> git commit

よく使うオプション

  • -a: 既にリポジトリの管理対象になっているファイルで変更があったものをステージにセットする。
  • -m "[comment]": コメントを指定する。※指定しない場合は既定のエディタが開く

一つ前のコミットに追加でステージをコミットする。

> git commit --amend

一つ前のコミットの著者を変更する。

> git commit --amend --author="[committer name] <[e-mail address]>"
> git rebase --continue

一つ前のコミットのコミット日時を変更する。

> git commit --amend --date="[Date formats such as RFC3339]"
> git rebase --continue

コミット前の変更を無かった事にする。

> git checkout .

検索

HEADから文字列を検索する。

> git grep [search word]

全コミットから文字列を検索する。

> git grep [search word] $(git rev-list --all)

リモートを含めた全ブランチから文字列を検索する。

> git grep [search word] $(git branch -a --format='%(objectname) %(refname:short)' | sort | uniq -w 40 | cut -c 42-)

コミットログ

コミットログを表示する

> git log

よく使うオプション

  • --oneline: --pretty=oneline--abbrev-commitを一緒に指定した場合と同等。
    • --pretty=oneline: commit IDとコメント文を一行で表示する。
    • --abbrev-commit: commit IDを短縮する。
  • --graph: commit先のbranch treeと一緒に表示する。
  • --since=[yyyy-mm-dd] | --after=[yyyy-mm-dd]: 指定日以降のみ表示する。
  • --until=[yyyy-mm-dd] | --before=[yyyy-mm-dd]: 指定日以前のみ表示する。
  • --pretty=full: commiterを表示する。

タグ

最新のコミットにtagを付ける。

> git tag [tag name]

ブランチ

リポジトリに存在するブランチを表示する。

> git branch

よく使うオプション

  • -r | --remotes remoteに存在するブランチを表示する。
  • -a | --all localとremoteに存在するブランチを表示する。

リポジトリの管理

管理対象になっているファイルの一覧を表示する。

> git ls-files

特定リビジョンの管理対象ファイルの一覧を表示する。

> git ls-tree -r [commit ID]

特定リビジョンの差分を表示する。

> git show [commit ID]

リビジョン間で比較する。

> git diff [old commit ID] [new commit ID]

[new commit ID]を省略した場合は、ワーキングコピーとの比較になる。

よく使うオプション

  • --name-only 変更があったファイルのパスだけ表示する。

リポジトリ上のゴミを削除する。

> git gc --aggressive --prune=now

ワーキングコピーはそのままに、一つ前のコミットを無かった事にする。

> git reset --soft HEAD^

全てのコミットからファイルを削除する。

> git filter-branch --index-filter 'git rm --cached --ignore-unmatch [target file]' HEAD
> git reflog expire --expire=now --all
> git gc --aggressive --prune=now

全てのコミットの著者・コミッタを変更する。

> git filter-branch -f --env-filter "GIT_AUTHOR_NAME='[committer name]'; GIT_AUTHOR_EMAIL='[e-mail address]'; GIT_COMMITTER_NAME='[committer name]'; GIT_COMMITTER_EMAIL='[e-mail address]'"

リモートリポジトリ

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

> git remote -v

Githubなど別途生成したリモートリポジトリをoriginとして登録する。

> git remote add origin ssh://[committer name]@[host domain]:[repository path]

リモートリポジトリoriginmasterブランチにプッシュする。

> git push origin master

リモートリポジトリoriginmasterブランチから差分を取得する。

> git pull origin master