変更を失うことなく、プッシュされていない最後のgitコミットをアンコミットする方法 質問する

変更を失うことなく、プッシュされていない最後のgitコミットをアンコミットする方法 質問する

コミットを元に戻す方法はありますか。そのコミットで行われた変更はローカル コピーに保持されますが、作業コピーではコミットされていない変更になります。コミットをロールバックすると、前のコミットに戻ります。行われた変更を保持したいのですが、間違ったブランチにコミットしてしまいました。

これはプッシュされたわけではなく、コミットされただけです。

ベストアンサー1

それにはさまざまな方法があります。たとえば、次のようになります。

まだコミットを公開していない場合:

git reset HEAD~1 --soft   

これで完了です。コミットの変更は作業ディレクトリに残り、最後のコミットは現在のブランチから削除されます。git リセット 男


公開的にプッシュした場合(「master」というブランチに):

git checkout -b MyCommit //save your commit in a separate branch just in case (so you don't have to dig it from reflog in case you screw up :) )

通常通りコミットを元に戻してプッシュする

git checkout master
git revert a8172f36 #hash of the commit you want to destroy
# this introduces a new commit (say, it's hash is 86b48ba) which removes changes, introduced in the commit in question (but those changes are still visible in the history)
git push origin master

ここで、これらの変更を作業コピーのローカル変更として保存したい場合 (「ローカル コピーがそのコミットで行われた変更を保持するように」)、次の--no-commitオプションを使用してコミットを元に戻すだけです。

git revert --no-commit 86b48ba (hash of the revert commit).

小さな例を作成しました:https://github.com/Isantipov/git-revert/commits/master

おすすめ記事