Gitのブランチを修正する方法が間違っ提出?

私は間違った枝は非常に良いの提出をした作られました。どのように私は最後の私のメインブランチにコミット元に戻すか、同じ変更を加えて、私のブランチにアップグレードしてください?


#1階

このトピックで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ローカルブランチではなく、リモートブランチ。


ハウス#2

あなたはクリーン(無修正)作業コピーを持っている場合

(心の中で次のステップに提出ハッシュを確実にするために)提出をロールバックするには:

git reset --hard HEAD^

プルは、別のブランチを提出します:

git checkout other-branch
git cherry-pick COMMIT-HASH

あなたは、変更またはトラックが変更されている場合

また、それは注意しgit reset --hardます殺すあなたが持つかもしれない任意の人跡未踏の変更および修正をあなたはこれらの持っているのであれば、変更を、あなたが好むことがあります。

git reset HEAD^
git checkout .

ハウス#3

あなたが変更にプッシュしていない場合は、ソフトリセットを実行することができます。

git reset --soft HEAD^

これは、提出され復元されますが、変更はインデックスに戻って提出されます。あなたの日付が互いにあるとし、gitのは、あなたが別のブランチを入力できるようになり、その後、あなたは簡単に提出することができます:

git checkout branch
git commit

欠点は、あなたが再入力メッセージをコミットする必要があるということです。


#4階

あなたが変更に貢献してきた場合は、リセットHEAD後の力でプッシュする必要があります。

git reset --hard HEAD^
git merge COMMIT_SHA1
git push --force

警告:ハードリセットは、作業コピー中にコミットされていない変更を元に戻しますと、強制リセットは、リモートのローカルブランチの現在の状態の状態ブランチを完全にカバーしています。

念のため、Windowsの(というよりも、バッシュ、Windowsのコマンドラインを使用して)上では、実際には4つである^^^^ことがあるので、代わりの1

git reset --hard HEAD^^^^

ハウス#5

だから、あなたの計画はあなたが約束していることがあれば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

loveからから:HTTP//haacked.com/archive/2015/06/29/git-migrate/


ハウス#6

あなたは既に(例えば、分岐が存在するブランチに変更を適用したい場合は、開発を)、従ってください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

また、該当する場合、遅延ストレージポップアップ(アプリケーション)、あなたはこれまでに、対象のブランチをマージするまで。


#7の構築

この問題が発生したが、Visual Studioを持っている場合は、次の操作を実行できます。

あなたのブランチを右クリックし、[選択View History

入力画像の説明

リターンを提出して右クリックします。そして、復元またはリセットする必要に応じインチ

入力画像の説明


#8の構築

私は最近、私は他のブランチにコミットする必要があるとき、私は誤って、所有者を変更し、同じことをしました。しかし、私は何もプッシュしていませんでした。

あなただけの間違った枝を提出した後、何も変更せず、レポへのプッシュがない場合は、次の操作を行うことができます。

// 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..."

注:上記の例では、私はgitのリセットHEAD〜1は、一度提出して書き換えてください。nをコミットをフォールバックしたい場合しかし、あなたは、n〜gitのリセットHEADを行うことができます。

あなたが最終的に間違ったブランチに提出し、最終的に間違った枝に提出したことを実現する前に、より多くのコードを書いた場合も、あなたは進行中の作業を保存するためにgitのスタッシュを使用することができます。

// 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

注:私はリファレンスとして、このサイトを使用https://www.clearvision-cm.com/blog/what-to-do-when-you-commit-to-the-wrong-git-branch/


ハウス#9

細部へのこの答えは、あなたが複数の提出は、たとえば、転送する必要がある場合developnew_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
オリジナルの記事は、0公表 ウォンの賞賛0 ビュー2235を

おすすめ

転載: blog.csdn.net/p15097962069/article/details/103890259