git reset

git

書き方

git reset [--soft|--mixed|--hard] <commit> | git reset <path>

ブランチが指すコミットを移します。--softは変更をステージに残し、--mixed(既定)はステージを外して修正はファイルに残し、--hardはその修正まで捨てて取り返せません。

よく使うオプション

オプション意味
--softmove the branch only; index and files keep everything, so the change stays staged
--mixeddefault: also clear the staging area, leaving edits in your files
--hardalso overwrite your files; uncommitted work is destroyed
<path>no commit given: unstage that path
--keepmove but refuse if it would overwrite a local change
--mergelike --hard but keeps changes that do not collide

git reset --soft HEAD~1

undoes the last commit and leaves everything staged

git reset HEAD~1

undoes the last commit; the changes sit in your files, unstaged

git reset src/app.ts

unstages one file without changing it

git reset --hard origin/main

throws 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