16

私はマスターにたくさんのコミットを行い、後でそれらがブランチにあるべきだったことに気付きました。

マスターのリベース、マージ、リセットについてさまざまなことを見てきました。しかし、操作の試みは、私がやろうとしていることのように見える歴史を生み出しませんでした.

私の試みは、マスターを時間内に戻すにはrebase --ontoとの組み合わせが必要であると私に信じさせます。reset --hardしかし、Git の分岐についての私の理解には、まだ不十分な点があります。これを行うことの一部は、それをどのように使用できるかを学ぶことです。

私が移動しようとしている変更はどれもプッシュされていないことに注意してください。

現時点の

  * remote/trunk
--o--a--b--c--d--e--f     <- master
  |
  o                       <- remote branch foo

望ましい結果

  * remote/trunk
--o                       <- master
  |
  o--a--b--c--d--e--f     <- remote branch foo
4

3 に答える 3

9

マーティンの答えのバリエーションで、必ずしもあなたの状況に当てはまるとは限りませんが、とにかく投稿したいと思います:)

commitoでブランチを作成するのを忘れたとすると、次のようになります。

x--y--z--o--a--b--c--d--e--f  master
         |
         +
   [forgot to make a branch here]

そして、あなたはあなたが本当に望んでいたのは:

x--y--z--o   master
         |
         +--a--b--c--d--e--f  topic

この場合にできることは、oハッシュを使用してブランチを作成することです。

git branch topic # creates new branch 'topic' - will be at commit `f`
git checkout o -b newmaster # creates new branch called newmaster pointing on commit `o` (please replace `o` with the actual hash)
git branch -M newmaster master # force rename newmaster to master (means master points on hash `o`)

マスターブランチ(commit o)にいるので、最後のステップとして次のことができます。

git checkout topic

もちろん、ハッシュは最初の5文字だけにすることができます。

編集

使用していることはそれほど重要ではありません。git-svn本当に重要なのは、その後のどの時点でもマスターブランチを公開していないことです。o

gitのブランチは、実際にはコミットへのポインターにすぎません。そのため、分岐は非常に安価です。ポインタを作成するだけで、分岐ができます。

リモートブランチの追跡についてはわかりませんが、ブランチの名前を変更/移動した後に設定する必要がある場合があります。

于 2010-03-04T02:25:41.170 に答える
6

ブランチの名前を変更することが正しい解決策であるかどうかはわかりません。

  * remote/trunk
--M--a--b--c--d--e--f     <- master
  |
  F                       <- remote branch foo

に:

--F                       <- master
  |
  M--a--b--c--d--e--f     <- remote branch foo
  * remote/trunk

(名前を変更した場合remote/foo、これはお勧めできません。最初に追跡してから名前を変更する必要がありますが、最終結果は必要なものとは異なります)

これはあなたが望む「望ましい結果」ではありません(fooはMではなくFから始める必要があります):

  * remote/trunk
--M                       <- master
  |
  F--a--b--c--d--e--f     <- remote branch foo

あなたはそれを介してのみそれを達成することができますrebase --onto

git checkout --track -b origin/foo  # create a local branch named after the remote one
git branch tmp                      # mark current foo HEAD to 'F'
git branch -f foo master            # put foo where it should b: at 'f'
git branch -f master tmp^           # reset master to M, parent of tmp
git checkout tmp                    # go to where we must replay the commits
git rebase --onto tmp master foo    # replay a to f on top of tmp
git svn dcommit                     # push the local foo in order to update remote/foo

あなたに与える:

  * remote/trunk
--M                             <- master
  |
  F--a'--b'--c'--d'--e'--f'     <- local foo and remote branch foo
于 2010-03-04T05:23:21.427 に答える
1

hasen j の提案はほぼ正しいですが、いくつかの小さな変更を加える必要がありました (そして、git-svn を使用しています)。

# create the branch with your commits
git branch performance
# fork the master to a new branch at the commit before your local, non pushed commits
git branch newmaster 2d0516dfe8252de87
# checkout your branch 
git checkout performance
# rename the newmaster
git branch -M newmaster master
# now checkout the master
git checkout master

現在のブランチの名前を変更することはできないため、コミットを移動したパフォーマンス ブランチをチェックアウトしました。

于 2011-12-01T15:17:52.057 に答える