git rebase

git

書き方

git rebase [-i] [--onto <base>] <upstream>

自分のコミットを別の土台の上に付け替えます。ハッシュが変わるので、他の人がすでに取得したブランチにはrebaseせずmergeを使います。

よく使うオプション

オプション意味
-iinteractive: reorder, squash, reword, drop commits
--onto <base>move the range onto a different base
--continuecarry on after fixing a conflict
--abortput the branch back exactly as it was
--skipdrop the commit that will not apply
--autostashstash uncommitted work and put it back afterwards
--autosquashapply fixup!/squash! commits automatically

git rebase main

replays your commits on top of main, giving them new hashes

git rebase -i HEAD~3

squash or reword the last three commits

git rebase --abort

cancel a rebase and return to the original branch

取り消しに何通りもあるのが混乱のもとです — ブランチの指す先を動かすのと、打ち消すコミットを積むのは別です。

読み方

  • 角括弧 [ ] は省いてよい部分です。
  • 三点 … は複数並べられるという意味です。
  • オプションは大文字小文字を区別します — -r と -R が別物のコマンドもあります。

よくある質問

Q. git rebase は何をしますか。

自分のコミットを別の土台の上に付け替えます。ハッシュが変わるので、他の人がすでに取得したブランチにはrebaseせずmergeを使います。

Q. どう打ちますか。

git rebase [-i] [--onto <base>] <upstream> — 角括弧は省いてよい部分です。

Q. よく使うオプションはいくつですか。

ここにまとめたのは7個です。全部は man git にあります。このコマンドはgitの仲間です。

関連するコマンド

man ページ: man git-rebase