あなたが公開したなら、あなたはあなたがの歴史を書き直したくないということは正しいですmaster
。D
他のユーザーが自分の作業を簡単にマージまたはリベースできるように、現在の履歴を保持しながらマスターへのコミットを公開して、現在の状態に戻すことが必要です。
将来のある時点でマージtopic
することを計画してmaster
いる場合、おそらくまた実行したいのは、との間に新しい共通ベースを作成することmaster
ですtopic
。これにより、後でマージするときに、topic
で元に戻されたコミットが失われることはありませんmaster
。これを行う最も簡単な方法は、元の状態にリセットされる「undo」コミットの上に「redo」コミットを作成し、その上master
に新しいtopic
ブランチを作成することです。
# checkout master branch (currently at G)
git checkout master
# Reset the index to how we want master to look like
git reset D
# Move the branch pointer back to where it should be, leaving the index
# looking like D
git reset --soft HEAD@{1}
# Make a commit (D') for the head of the master branch
git commit -m "Temporarily revert E, F and G"
# Create the new topic branch based on master.
# We're going to make it on top of master and the 'undo'
# commit to ensure that subsequent merges of master->topic
# or topic->master don't merge in the undo.
git checkout -b topic
# Revert the undo commit, making a redo commit (G').
git revert HEAD
別の方法として、コミットE'、F'、G'を個別にやり直すこともできますが、E、F、Gはすでに公開された履歴に含まれているため、'undo'コミットを参照して、次のように言うと、おそらくより理解しやすくなります。コミットは取り消されています。とにかく、これは何をするかgit revert
です。
基本的にあなたが知っているのはこれです。
D -- E -- F -- G -- D' <-- master
\
\
G' <-- topic
重要なことは、履歴を書き直しておらず、トピックがマスターに基づいているため、マージによって誤って「元に戻す」コミットが適用されないことです。これで、とリモートリポジトリの両方master
を安全にプッシュできます。topic