「.NET 開発基盤部会 Wiki」は、「Open棟梁Project」,「OSSコンソーシアム .NET開発基盤部会」によって運営されています。
目次 †
概要 †
Gitコマンドの覚書
用語 †
repository †
- ファイルやディレクトリの変更履歴を記録して管理する場所。
- 管理対象のファイルやディレクトリはrepositoryの下に置く。
http://www.backlog.jp/git-guide/intro/intro1_2.html
local / remote †
- remote repository
- サーバに配置して複数人で共有するためのrepository。
- local repository
- ユーザ一人が利用する、自分の手元のマシン上に配置するrepository。
- local repositoryの内容はremote repositoryを経由して公開・取得する。
http://www.backlog.jp/git-guide/intro/intro1_3.html
bare / non-bare †
- non-bare repository
ワーキングディレクトリを持つ。
- bare repository
ワーキングディレクトリを持たない。更新情報だけを持っている。
http://www.nekotricolor.com/entry/theory-of-bare-and-non-bare-repository-manage-wordpress-themes-with-git
branch †
http://www.backlog.jp/git-guide/stepup/stepup1_1.html
branch名 †
- (remote)/(branch)
remoteのrepositoryのbranch
- (origin)/(branch)
cloneしたremoteのrepositoryのbranch
HEAD †
現在使用しているbranchの先頭を表す名前
stash †
ファイルの変更内容を一時的に記録しておく領域。
commit †
ファイルやディレクトリの追加・変更を、Repositoryに記録する
branch †
branch †
branchする
- command
- 現在のbranchから<new-branch>をbranchする
git branch <new-branch>
checkout †
branchをcheckoutする(切り替える)。
- command
- 既存branch <existing-branch> に切り替える。
git checkout <existing-branch>
- 新規branch <new-branch> を作成して即時checkout
git checkout -b <new-branch>
- 既存branch <existing-branch>から新規branch <new-branch> を作成して即時checkout
git checkout -b <new-branch> <existing-branch>
merge †
branchをmergeする。
削除 †
branchを削除する。
repository †
clone †
git clone は既存の git repositoryのclone (コピー) を作成する
repository間 †
git remote †
git fetch †
remote repositoryからfetchする。
git pull †
remote repositoryからpull(fetch & merge)する。
git push †
remote repositoryへpushする。
ステージングの操作 †
git add †
ステージング・エリアにaddする。
- command
- <file> に加えられたすべての変更をステージして次回のコミットの対象とする。
git add <file>
- <directory> 内のすべての変更をステージして次回のコミットの対象とする。
git add <directory>
- インタラクティブなステージングセッションを開始する。
git add -p
git clean †
ステージング・エリアからcleanする。
- command
- git clean の「予行演習」を行うコマンドで削除されるファイルを表示する。
git clean -n
- 追跡対象外のファイルをカレントディレクトリから削除する。
ただし、.gitignore. で指定したファイルは削除しない。
git clean -f
- 追跡対象外のファイルを指定したパスから削除する。
git clean -f <path>
- 追跡対象外のファイルとディレクトリをカレントディレクトリから削除する。
git clean -df
- 追跡対象外ファイルと通常無視されるファイルをカレントディレクトリから削除する。
git clean -xf
git reset †
commitのreset
- command
- 作業ディレクトリに変更も加えずに、指定したファイルをステージングエリアから削除する
git reset <file>
- 作業ディレクトリに何の変更も加えることなくステージエリアをresetして直前のcommit時の状態と一致させる
git reset
- ステージエリアと作業ディレクトリをresetして直前のcommit時の状態と一致させる
git reset --hard
commitの操作 †
<commit> = commit の"SHA-1 id"
git commit †
変更のcommit
git revert †
commitのrevert
- command
- 特定のcommitのrevert
git revert <commit>
git reset †
commitのreset
- 現在のbranchの先端を <commit> の位置に戻した上でステージングエリアをその状態と一致するように元に戻すが、作業ディレクトリのみはそのままにしておく。
これにより、変更規模が小さく整理されたスナップショットを作成してlocal repositoryへ再commitできる。
git reset <commit>
確認 †
branch †
git branch †
branchの確認
- command
- local branchの確認
git branch
- remote追跡branchを表示
git branch -r
- local branchとremote追跡branchの両方のbranchを表示
git branch -a
- 名称の変更
git branch -m <branch>
commit †
git log †
logの確認
- command
- commit履歴全体をデフォルトの形式で表示
git log
- 表示するcommit数を <limit> に制限する。
git log -n <limit>
- commit履歴を概観する(各々のcommitの内容を1行に圧縮して表示)
git log --oneline
- 改変されたファイルおよびその中での追加行数と削除行数を増減数で表示
git log --stat
- 各々のcommitに対応するパッチ(commitの完全な差分情報)を表示します。
git log -p
ステージング †
git status †
statusの確認
- command
- ステージされたファイル、されていないファイル、追跡対象外のファイルを一覧表示。
git status
Rebase、Reset、Revert †
Rebase、Reset、Revert の違いと使い分け
Rebase †
「履歴を綺麗にしたい」場合に使う(ローカルでのみ行うべき)
概要 †
- ブランチの基点(親コミット)を変更することで、履歴を整理
- ブランチを最新の `main` ブランチに追従させる(の最新の状態に適用し直す。)
動作イメージ †
https://www.google.com/search?q=Rebase&udm=2
注意点 †
- 履歴を書き換えるため、リモートにプッシュしたコミットに対しては使わない。
- 既にリモートにpushしたコミットの履歴をrebaseで書き換えた後、push --forceすると結果として、チームの開発効率が低下する。
- 他の開発者のローカル履歴とリモートの履歴が不一致になり、何が起こったのか分からず、他の開発者が混乱する。
- リモートの意図しない履歴を書き換える可能性があり、また、他の開発者のコミットが消える可能性がある。
Reset †
「間違えてコミットした」場合に使う(ローカルでのみ行うべき)
概要 †
- 指定したコミットに対して、現在のブランチを強制的に戻すコマンド。
- 履歴を巻き戻すとともに、ワーキングツリーやインデックスの状態をどうするかによって、3つのモードがある。
モード | `git reset <commit>` の影響 |
`--soft` | コミット履歴だけ戻し、インデックスやワーキングツリーの変更はそのまま残る |
`--mixed`(デフォルト) | コミットとインデックスは戻るが、ワーキングツリーの変更は残る |
`--hard` | コミット、インデックス、ワーキングツリーすべてを戻す(変更が完全に消える) |
注意点 †
- `--hard` を使うと 変更が完全に消える ので、慎重に使うこと。
- リモートにプッシュ済みの履歴を変更すると他の開発者に影響を与える。
Revert †
「リモートに影響を与えずに変更を取り消したい」(安全なので推奨される)場合に使う
概要 †
- 履歴を維持しながら特定のコミットの変更を取り消すため、リモートリポジトリに影響を与えずに修正できる。
- 指定したコミットを打ち消す「新しいコミット」を作成するコマンド。
- 履歴を改変せずに変更を取り消すことができるため、安全な方法。
注意点 †
- 履歴が残るため、リセットのように「なかったこと」にはならない。
- 逆順にrevert(打ち消す「新しいコミット」)してもコンフリクトが発生する場合がある。
比較表 †
コマンド | 目的 | 履歴の改変 | 影響範囲 |
rebase | 履歴を整理・ブランチを最新にする | する | ローカルのみ(要 `push --force`) |
reset | コミットを取り消す・巻き戻す | する | ローカルのみ(特に `--hard` に注意) |
revert | 特定のコミットを打ち消す | しない | リモートでも安全 |