私は間違ったブランチに完全に良いコミットをしました。masterブランチの最後のコミットを元に戻し、同じ変更を加えてアップグレードブランチに取り込むにはどうすればよいですか?
私は間違ったブランチに完全に良いコミットをしました。masterブランチの最後のコミットを元に戻し、同じ変更を加えてアップグレードブランチに取り込むにはどうすればよいですか?
回答:
まだ変更をプッシュしていない場合は、ソフトリセットを実行することもできます。
git reset --soft HEAD^
これはコミットを元に戻しますが、コミットされた変更をインデックスに戻します。ブランチがお互いに関して比較的最新であると仮定すると、gitは他のブランチへのチェックアウトを可能にし、その後すぐにコミットすることができます:
git checkout branch
git commit
欠点は、コミットメッセージを再入力する必要があることです。
git reset --soft HEAD\^
このトピックについては4年遅れていますが、これは誰かに役立つかもしれません。
コミットする前に新しいブランチを作成するのを忘れて、マスターですべてをコミットした場合は、コミットの数に関係なく、次のアプローチの方が簡単です。
git stash # skip if all changes are committed
git branch my_feature
git reset --hard origin/master
git checkout my_feature
git stash pop # skip if all changes were committed
これで、マスターブランチがに等しくなりorigin/master、すべての新しいコミットがオンになりmy_featureます。my_featureはローカルブランチであり、リモートブランチではないことに注意してください。
master、その後リセットし、masterへorigin/master。
origin/masterです。リセットしたいコミットがすでにコミットされているので、多くのコミットは必要ありません。:先端のためのクレジットは、しかし、このページでgithub.com/blog/...
1つのコミットをロールバックするには(次のステップのコミットのハッシュに注意してください):
git reset --hard HEAD^
そのコミットを別のブランチにプルするには:
git checkout other-branch
git cherry-pick COMMIT-HASH
また、これにより、追跡されていない変更や変更が強制終了git reset --hardされるため、変更がある場合は次のようにしてください。
git reset HEAD^
git checkout .
git rev-parse BRANCH_NAMEシャを得るために。
git reflog show <branch>!
git stash、リセットの前に、git stash pop後で使用してそれらを復元することができるため、--hardパーツを恐れる必要はありません
すでに変更をプッシュしている場合は、HEADをリセットした後、次のプッシュを強制する必要があります。
git reset --hard HEAD^
git merge COMMIT_SHA1
git push --force
警告:ハードリセットは作業コピーのコミットされていない変更を元に戻しますが、強制プッシュはリモートブランチの状態をローカルブランチの現在の状態で完全に上書きします。
念のため、Windowsでは(BashではなくWindowsコマンドラインを使用)、実際^^^^には1つではなく4つなので、
git reset --hard HEAD^^^^
git reset --hard COMMIT_HASH git push --force
私は最近同じことをしましたが、誤ってマスターに変更をコミットしましたが、他のブランチにコミットするべきでした。しかし、私は何も押しませんでした。
間違ったブランチにコミットしただけで、その後何も変更しておらず、リポジトリにプッシュしていない場合は、次のようにすることができます。
// rewind master to point to the commit just before your most recent commit.
// this takes all changes in your most recent commit, and turns them into unstaged changes.
git reset HEAD~1
// temporarily save your unstaged changes as a commit that's not attached to any branch using git stash
// all temporary commits created with git stash are put into a stack of temporary commits.
git stash
// create other-branch (if the other branch doesn't already exist)
git branch other-branch
// checkout the other branch you should have committed to.
git checkout other-branch
// take the temporary commit you created, and apply all of those changes to the new branch.
//This also deletes the temporary commit from the stack of temp commits.
git stash pop
// add the changes you want with git add...
// re-commit your changes onto other-branch
git commit -m "some message..."
注:上記の例では、1つのコミットをgit reset HEAD〜1で巻き戻していました。ただし、n個のコミットを巻き戻したい場合は、git reset HEAD〜nを実行できます。
また、間違ったブランチにコミットしてしまい、間違ったブランチにコミットしたことに気づく前にコードを書き終えた場合は、git stashを使用して進行中の作業を保存できます。
// save the not-ready-to-commit work you're in the middle of
git stash
// rewind n commits
git reset HEAD~n
// stash the committed changes as a single temp commit onto the stack.
git stash
// create other-branch (if it doesn't already exist)
git branch other-branch
// checkout the other branch you should have committed to.
git checkout other-branch
// apply all the committed changes to the new branch
git stash pop
// add the changes you want with git add...
// re-commit your changes onto the new branch as a single commit.
git commit -m "some message..."
// pop the changes you were in the middle of and continue coding
git stash pop
注:このWebサイトを参照として使用しました https://www.clearvision-cm.com/blog/what-to-do-when-you-commit-to-the-wrong-git-branch/
git checkout -b new_branchありました。もう一度コミットする必要があります。
したがって、masterコミットしたがコミットするつもりanother-branchである(まだ存在していない場合もある)シナリオで、まだプッシュしていない場合、これは簡単に修正できます。
// if your branch doesn't exist, then add the -b argument
git checkout -b another-branch
git branch --force master origin/master
これで、すべてのコミットmasterがオンになりますanother-branch。
another-branchすでに存在しています。この場合、それは私がマスターするために行ったコミットを無効にしただけで、それらをオンにしませんでしたanother-branch。
この回答について詳しく説明するために、たとえば次のdevelopように移動するコミットが複数ある場合new_branch:
git checkout develop # You're probably there already
git reflog # Find LAST_GOOD, FIRST_NEW, LAST_NEW hashes
git checkout new_branch
git cherry-pick FIRST_NEW^..LAST_NEW # ^.. includes FIRST_NEW
git reflog # Confirm that your commits are safely home in their new branch!
git checkout develop
git reset --hard LAST_GOOD # develop is now back where it started
もしあなたのために、それがちょうど1つのコミットであるならば、利用可能な他の多くのより簡単なリセットソリューションがたくさんあります。私の場合、誤って約10回のコミットが行われたため、それをmaster呼び出しましょうbranch_xyz。コミット履歴を失いたくありませんでした。
あなたが何ができるか、そして私を救ったのは、この回答を参照として使用して、4ステップのプロセスを使用していたことです。
master branch_xyz master 上記の手順の詳細は次のとおりです-
master(誤って多くの変更をコミットした) から新しいブランチを作成します
git checkout -b temp_branch_xyz
注:-bフラグは、新しいブランチを作成するために使用されます。
これが正しいかどうかを確認するためgit branchに、temp_branch_xyzブランチにいるgit logことを確認し、コミットが正しいかどうかを確認します。
一時的なブランチを、もともとコミット用に意図されたブランチ、つまりにマージしbranch_xyzます。
まず、元のブランチに切り替えbranch_xyzます(そうでない場合はgit fetch、必要になる可能性があります)。
git checkout branch_xyz
注:-bフラグを使用しない
ここで、一時的なブランチを現在チェックアウトしているブランチにマージしましょうbranch_xyz
git merge temp_branch_xyz
競合がある場合は、ここでいくつかの競合に注意する必要があります。マージが成功したら、プッシュする(そうする)か、次のステップに進むことができます。
この回答を参照としてmaster使用する際の誤ったコミットを元に戻し、最初にmaster
git checkout master
その後、元に戻してリモートに一致させる(または必要に応じて特定のコミットに戻す)
git reset --hard origin/master
繰り返しgit logますが、意図した変更が反映されるようにするために、私は前後に実行します。
証拠を消去すると、一時的なブランチが削除されます。そのためには、まずtempがマージされたブランチをチェックアウトする必要がありbranch_xyzます(つまり、このままでmaster以下のコマンドを実行すると、が表示される可能性がありますerror: The branch 'temp_branch_xyz' is not fully merged)。
git checkout branch_xyz
そして、この事故の証拠を削除します
git branch -d temp_branch_xyz
どうぞ。
変更を適用したいブランチがすでに存在する場合(ブランチ開発など)、以下のfotanusによって提供された指示に従ってください。
git checkout develop
git rebase develop my_feature # applies changes to correct branch
git checkout develop # 'cuz rebasing will leave you on my_feature
git merge develop my_feature # will be a fast-forward
git branch -d my_feature
そして、明らかにあなたが使用することができtempbranchの代わりに、または任意の他の支店名をmy_featureあなたが望んでいた場合。
また、該当する場合は、ターゲットブランチでマージするまで、スタッシュポップ(適用)を遅らせます。