ここの他の回答から、私は 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次の内容のa があります。
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
基本的に、gitHEADが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 addfolder/file.txtgit 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でした。コミットやリビジョンの削除という点でどのように機能するかを理解するためです。他の人にも役立つことを願っています...