209

私は3つのgitコミットを行いましたが、プッシュされていません。最新のものではない古いもの(ddc6859af44)と(47175e84c)を修正するにはどうすればよいですか?

$git log
commit f4074f289b8a49250b15a4f25ca4b46017454781
Date:   Tue Jan 10 10:57:27 2012 -0800

commit ddc6859af448b8fd2e86dd0437c47b6014380a7f
Date:   Mon Jan 9 16:29:30 2012 -0800

commit 47175e84c2cb7e47520f7dde824718eae3624550
Date:   Mon Jan 9 13:13:22 2012 -0800
4

6 に答える 6

258
git rebase -i HEAD^^^

修正したいものをeditまたはe(置換pick) でマークします。保存して終了します。

変更を加えてから、

git add .
git rebase --continue

追加の削除を追加する場合は、commit コマンドからオプションを削除します。メッセージを調整したい場合は、--no-editオプションだけを省略してください。

于 2012-01-11T19:06:23.007 に答える
160

古いもので修正したいというコミットを準備しましたが、そのリベースを見て驚きました-私はコミットされていない変更があると不平を言いました。しかし、古いコミットの編集オプションを指定して、再度変更を加えたくありませんでした。したがって、ソリューションは非常に簡単で簡単でした。

  1. 古いコミットへの更新を準備し、追加してコミットします
  2. git rebase -i <commit you want to amend>^-^テキストエディターで上記のコミットが表示されることに注意してください
  3. 次のようなものが得られます。

    pick 8c83e24 use substitution instead of separate subsystems file to avoid jgroups.xml and jgroups-e2.xml going out of sync
    pick 799ce28 generate ec2 configuration out of subsystems-ha.xml and subsystems-full-ha.xml to avoid discrepancies
    pick e23d23a fix indentation of jgroups.xml
    
  4. e23d23a と 8c83e24 を組み合わせるには、行の順序を変更して、次のようにスカッシュを使用できます。

    pick 8c83e24 use substitution instead of separate subsystems file to avoid jgroups.xml and jgroups-e2.xml going out of sync    
    squash e23d23a fix indentation of jgroups.xml
    pick 799ce28 generate ec2 configuration out of subsystems-ha.xml and subsystems-full-ha.xml to avoid discrepancies
    
  5. ファイルを書き込んで終了すると、コミット メッセージをマージするためのエディタが表示されます。そうして、テキストドキュメントを保存/終了します

  6. 完了です。コミットが修正されました

クレジットは次のとおりです: http://git-scm.com/book/en/Git-Tools-Rewriting-History 他にも便利な実証済みの git マジックがあります。

于 2013-08-09T15:42:35.150 に答える
16

私は別の方法を数回使用しました。実際、これはマニュアルgit rebase -iであり、いくつかのコミットを押しつぶしたり分割したりするなど、いくつかのコミットを再配置する場合に役立ちます。主な利点は、すべてのコミットの運命を一度に決定する必要がないことです。また、リベース中とは異なり、プロセス中はすべての Git 機能を利用できます。たとえば、元の履歴と書き換えられた履歴の両方のログをいつでも表示したり、別のリベースを実行したりすることもできます!

簡単に読めるように、次の方法でコミットを参照します。

C # good commit after a bad one
B # bad commit
A # good commit before a bad one

最初の履歴は次のようになります。

x - A - B - C
|           |
|           master
|
origin/master

次のように再作成します。

x - A - B*- C'
|           |
|           master
|
origin/master

手順

git checkout B       # get working-tree to the state of commit B
git reset --soft A   # tell Git that we are working before commit B
git checkout -b rewrite-history   # switch to a new branch for alternative history

git add(git add -iなど)を使用して古いコミットを改善しますgit stash。古いコミットを 2 つ以上に分割することもできます。

git commit           # recreate commit B (result = B*)
git cherry-pick C    # copy C to our new branch (result = C')

中間結果:

x - A - B - C 
|    \      |
|     \     master
|      \
|       B*- C'
|           |
|           rewrite-history
|
origin/master

終わりましょう:

git checkout master
git reset --hard rewrite-history  # make this branch master

または、コマンドを 1 つだけ使用します。

git branch -f master  # make this place the new tip of the master branch

それだけです、あなたはpush今あなたの進歩を遂げることができます。

最後のタスクは、一時ブランチを削除することです。

git branch -d rewrite-history
于 2013-08-19T07:24:25.777 に答える
16

git rebaseコミット履歴を書き換えるために使用できます。これは変更を破壊する可能性があるため、注意して使用してください。

まず、「修正」の変更を通常のコミットとしてコミットします。次に、最も古いコミットの親から開始するインタラクティブなリベースを実行します

git rebase -i 47175e84c2cb7e47520f7dde824718eae3624550^

これにより、すべてのコミットでエディターが起動します。それらを並べ替えて、「修正」コミットが修正したいコミットの下になるようにします。次に、行の最初の単語を「修正」コミットに置き換えます。これにより、以前のコミットsと結合 ( s quash) されます。エディターを保存して終了し、指示に従います。

于 2012-01-11T18:55:22.767 に答える
11

修正したいコミットでコマンドを使用git rebase --interactiveして、 を使用できます。edit

于 2012-01-11T19:01:30.420 に答える
2

OPが指定された2つのコミットを1つにスカッシュしたい場合、リベースせずにそれを行う別の方法があります

git checkout HEAD^               # go to the first commit you want squashed
git reset --soft HEAD^           # go to the second one but keep the tree and index the same
git commit --amend -C HEAD@{1}   # use the message from first commit (omit this to change)
git checkout HEAD@{3} -- .       # get the tree from the commit you did not want to touch
git add -A                       # add everything
git commit -C HEAD@{3}           # commit again using the message from that commit

構文は知っておくと便利です。@{N)これにより、参照があった場所の履歴を参照できるようになります。この場合、現在のコミットを表すのは HEAD です。

于 2012-01-11T20:52:29.577 に答える