git rebase --interactive
必要なコマンドです。
例:
現在のステータスはこれです:
bernt@le3180:~/src/stackoverflow/reordering_of_commits
$ git status
On branch master
nothing to commit, working tree clean
bernt@le3180:~/src/stackoverflow/reordering_of_commits
$ git log --oneline
a6e3c6a (HEAD -> master) Fourth commit
9a24b81 Third commit
7bdfb68 Second commit
186d1e0 First commit
コミット9a24b81
(3番目のコミット)と7bdfb68
(2 番目のコミット)を並べ替えたい。これを行うには、変更する最初のコミットの前に、まずコミットを見つけます。これはコミット186d1e0
(最初のコミット)です。
実行するコマンドはgit rebase --interactive COMMIT-BEFORE-FIRST-COMMIT-WE-WANT-TO-CHANGE
、この場合はです。
bernt@le3180:~/src/stackoverflow/reordering_of_commits
$ git rebase --interactive 186d1e0
これにより、デフォルトのエディターで次の内容のファイルが開きます。
pick 7bdfb68 Second commit
pick 9a24b81 Third commit
pick a6e3c6a Fourth commit
# Rebase 186d1e0..a6e3c6a onto 186d1e0 (3 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
ファイル内のコミットの順序は、git logで使用される順序とは逆であることに注意してください。gitログでは、最新のコミットが一番上にあります。このファイルでは、最新のコミットが一番下にあります。
ファイルの下部にあるコメントで説明されているように、コミットのスカッシュ、ドロップ、並べ替えなど、さまざまなことができます。コミットを並べ替えるには、ファイルを次のように編集します(下部のコメントは表示されません)。
pick 9a24b81 Third commit
pick 7bdfb68 Second commit
pick a6e3c6a Fourth commit
pick
各行の先頭のコマンドは「このコミットを使用(つまり、含める)」を意味し、リベースコマンドで一時ファイルを保存してエディターを終了すると、gitがコマンドを実行してリポジトリと作業ディレクトリを更新します。
$ git rebase --interactive 186d1e0
Successfully rebased and updated refs/heads/master.
bernt@le3180:~/src/stackoverflow/reordering_of_commits
$ git log --oneline
ba48fc9 (HEAD -> master) Fourth commit
119639c Second commit
9716581 Third commit
186d1e0 First commit
書き換えられたコミット履歴に注意してください。
リンク: