git reset
git
書き方
git reset [--soft|--mixed|--hard] <commit> | git reset <path>ブランチが指すコミットを移します。--softは変更をステージに残し、--mixed(既定)はステージを外して修正はファイルに残し、--hardはその修正まで捨てて取り返せません。
よく使うオプション
| オプション | 意味 |
|---|---|
| --soft | move the branch only; index and files keep everything, so the change stays staged |
| --mixed | default: also clear the staging area, leaving edits in your files |
| --hard | also overwrite your files; uncommitted work is destroyed |
| <path> | no commit given: unstage that path |
| --keep | move but refuse if it would overwrite a local change |
| --merge | like --hard but keeps changes that do not collide |
例
git reset --soft HEAD~1undoes the last commit and leaves everything staged
git reset HEAD~1undoes the last commit; the changes sit in your files, unstaged
git reset src/app.tsunstages one file without changing it
git reset --hard origin/mainthrows away local commits AND uncommitted edits
取り消しに何通りもあるのが混乱のもとです — ブランチの指す先を動かすのと、打ち消すコミットを積むのは別です。
読み方
- 角括弧 [ ] は省いてよい部分です。
- 三点 … は複数並べられるという意味です。
- オプションは大文字小文字を区別します — -r と -R が別物のコマンドもあります。
よくある質問
Q. git reset は何をしますか。
ブランチが指すコミットを移します。--softは変更をステージに残し、--mixed(既定)はステージを外して修正はファイルに残し、--hardはその修正まで捨てて取り返せません。
Q. どう打ちますか。
git reset [--soft|--mixed|--hard] <commit> | git reset <path> — 角括弧は省いてよい部分です。
Q. よく使うオプションはいくつですか。
ここにまとめたのは6個です。全部は man git にあります。このコマンドはgitの仲間です。
関連するコマンド
man ページ: man git-reset