ここでの他の回答からgit rebase -i
、コミットを削除するためにどのように使用できるかについて少し混乱していたので、ここにテストケースを書き留めても問題ないことを願っています(OPと非常に似ています)。
フォルダーbash
にテスト リポジトリを作成するために貼り付けることができるスクリプトを次に示します。/tmp
set -x
rm -rf /tmp/myrepo*
cd /tmp
mkdir myrepo_git
cd myrepo_git
git init
git config user.name me
git config user.email me@myself.com
mkdir folder
echo aaaa >> folder/file.txt
git add folder/file.txt
git commit -m "1st git commit"
echo bbbb >> folder/file.txt
git add folder/file.txt
git commit -m "2nd git commit"
echo cccc >> folder/file.txt
git add folder/file.txt
git commit -m "3rd git commit"
echo dddd >> folder/file.txt
git add folder/file.txt
git commit -m "4th git commit"
echo eeee >> folder/file.txt
git add folder/file.txt
git commit -m "5th git commit"
この時点で、file.txt
次の内容の があります。
aaaa
bbbb
cccc
dddd
eeee
この時点で、HEAD は 5 番目のコミット、HEAD~1 は 4 番目のコミット、HEAD~4 は 1 番目のコミットになります (つまり、HEAD~5 は存在しません)。3 番目のコミットを削除したいとします。myrepo_git
ディレクトリで次のコマンドを発行できます。
git rebase -i HEAD~4
( 「致命的: 単一のリビジョンが必要です; 上流の HEAD~5 が無効です」という結果になることに注意してくださいgit rebase -i HEAD~5
。 ) テキスト エディター ( @Dennis の回答のスクリーンショットを参照) が次の内容で開きます。
pick 5978582 2nd git commit
pick 448c212 3rd git commit
pick b50213c 4th git commit
pick a9c8fa1 5th git commit
# Rebase b916e7f..a9c8fa1 onto b916e7f
# ...
そのため、リクエストした HEAD~4以降(ただしは含まない) のすべてのコミットを取得します。行を削除しpick 448c212 3rd git commit
てファイルを保存します。からこの応答を取得しますgit rebase
:
error: could not apply b50213c... 4th git commit
When you have resolved this problem run "git rebase --continue".
If you would prefer to skip this patch, instead run "git rebase --skip".
To check out the original branch and stop rebasing run "git rebase --abort".
Could not apply b50213c... 4th git commit
この時点folder/file.txt
で、テキスト エディターで myrepo_git/ を開きます。変更されていることがわかります。
aaaa
bbbb
<<<<<<< HEAD
=======
cccc
dddd
>>>>>>> b50213c... 4th git commit
基本的に、git
HEAD が 2 回目のコミットに到達したときに、aaaa
+ bbbb
;の内容があったことがわかります。そして、追加されたcccc
+のパッチがdddd
あり、既存のコンテンツに追加する方法がわかりません。
したがって、ここでgit
決定することはできません-決定を下さなければならないのはあなたです.3番目のコミットを削除することにより、それによって導入された変更(ここでは行cccc
)を保持するか、保持しないかのいずれかです. そうでない場合は、テキスト エディタを使用して - を含む余分な行を削除するだけで、cccc
次folder/file.txt
のようになります。
aaaa
bbbb
dddd
...そして保存しfolder/file.txt
ます。myrepo_git
これで、ディレクトリで次のコマンドを発行できます。
$ nano folder/file.txt # text editor - edit, save
$ git rebase --continue
folder/file.txt: needs merge
You must edit all merge conflicts and then
mark them as resolved using git add
ああ、競合を解決したことを示すには、実行する前に , を実行する必要 git add
があります。folder/file.txt
git rebase --continue
$ git add folder/file.txt
$ git rebase --continue
ここで、テキスト エディターが再び開き、行が表示されます。4th git commit
ここで、コミット メッセージを変更する機会があります (この場合、意味のある変更4th (and removed 3rd) commit
または同様の変更を行うことができます)。保存したくない場合は、保存せずにテキスト エディターを終了してください。これを行うと、次のようになります。
$ git rebase --continue
[detached HEAD b8275fc] 4th git commit
1 file changed, 1 insertion(+)
Successfully rebased and updated refs/heads/master.
この時点で、(元のコミットのタイムスタンプは変更されていないようですgitk .
) の内容の次のような履歴が得られます (これは、say または他のツールで調べることもできます):folder/file.txt
1st git commit | +aaaa
----------------------------------------------
2nd git commit | aaaa
| +bbbb
----------------------------------------------
4th git commit | aaaa
| bbbb
| +dddd
----------------------------------------------
5th git commit | aaaa
| bbbb
| dddd
| +eeee
以前に、行cccc
(削除した 3 番目の git commit の内容)を保持することにした場合、次のようになります。
1st git commit | +aaaa
----------------------------------------------
2nd git commit | aaaa
| +bbbb
----------------------------------------------
4th git commit | aaaa
| bbbb
| +cccc
| +dddd
----------------------------------------------
5th git commit | aaaa
| bbbb
| cccc
| dddd
| +eeee
git rebase
これは、コミット/リビジョンの削除に関してどのように機能するかを理解し始めるために、私が見つけたかった種類の読書でした。他の人にも役立つことを願っています...