2009-08-29 17 views
1

私は3 'git commit'を行いましたが、 'git push'を行っていません。gitで先のコミットのファイルの変更をロールバックする方法

1. commit 1 
2. commit 2 
    touches fileA 
    touches fileB 
    touches fileC 
3. commit 3 

ので、どのように私はファイルbになるバックI

  1. ロールの変更は2をコミットすることができますか? (私は 'git checkout-fileB'をもう実行できません。私は既にgit commitしています。どうすれば変更をロールバックできますか?
  2. ファイルCを変更してコミット2のようにしますか?二コミット行くと、今ファイルを変更して、正しい 'Gitは-i HEAD〜2リベース' を実行し

答えて

2

使用git rebase -i HEAD~2および編集することができます

5

これは動作するはずです:?。

1. git rebase -i HEAD~2 
2. in your editor, select the following: 

edit 9b86592 commit 2 
pick f3907cb commit 3 

3. at this point roll back the changes you made in fileB, for example with 
    `git checkout <version_you_want>` or by manually editing the file 
4. make the changes in fileC you want to be part of commit 2 
5. `git add fileB fileC` 
6. `git commit --amend` 
7. `git rebase --continue` 

gitがしようとすると競合が発生した場合、マージ問題を解決する必要があるかもしれませんapply commit 3.それらを解決したら、git rebase --continueを再度実行してください。

# checkout incorrect commit 
git checkout <sha1_of_commit2> 

# revert the changes to fileB by checking out the parent version 
git checkout HEAD^ -- fileB 

# make an amended commit 
git commit --amend 

# go back to master 
git checkout master 

# transplant the changes since the bad commit onto the amended commit 
git rebase --onto [email protected]{1} <sha1_of_commit2> 
0

FILEBのチェックアウト、古いバージョンと

git checkout HEAD~3 -- fileB 
git commit -m"fix fileB" fileB 

今古いコミット

git rebase -i HEAD~3 

であなたの修正にスカッシュにリベース、それをコミット今あることを「FILEBを修正する」コミットあなたの最も最近の移動コミット2の後に "pick"命令を "squash"に変更し、コミット変更ファイルBをコミットと結合してリセットする。

+0

'rebase -i'は簡単です、IMO。 – u0b34a0f6ae

+0

@ kaizer.se:他の重要なステップに加えて、 'rebase -i'コマンドと対話型のリベースコミットリストを編集する必要があるので、説明するのに時間がかかります。 「手動で」行う方法を記述することは簡単です。 'rebase -i'を示唆する答えの多くは、レシピよりも多くのステップを含んでいるか、またはより複雑なステップや明示的に記述されていないより暗黙のステップです。 –

0

これは私がそれを行うだろうかです:あなたが分岐マスター上にあり、きれいなツリーを持っていると仮定すると

関連する問題