コミットをトランクからGitの枝に移動するにはどうすればよいですか?
-
24-09-2019 - |
質問
私はマスターにたくさんのコミットを行い、彼らが支部にいるべきだったという事実の後に実現しました。
私は、マスターを再脱出させ、マージし、リセットすることについてさまざまなことを見ました。しかし、操作の試みは、私がやろうとしていることのように見える歴史をもたらしませんでした。
私の試みは、それがいくらかの組み合わせを必要とすると信じさせてくれます 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
解決
枝を名前に変更することが正しい解決策であるかどうかはわかりません。
* 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
他のヒント
必ずしもあなたの状況に適用できるわけではないマーティンの答えのバリエーションですが、とにかく投稿したいです:)
コミット時にブランチを作成するのを忘れたとします o
, 、だからあなたが持っている:
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`)
あなたはマスターブランチになります(コミット o
)、最後のステップとして:
git checkout topic
もちろんハッシュは最初の5文字にすぎません。
編集
あなたが使用していることはそれほど重要ではないはずです git-svn
, 、本当に重要なのは、あなたがマスターブランチをいつでも公開していないことです o
Gitのブランチは、本当にコミットへのポインターに他なりません。そのため、分岐はとても安いです。ポインターを作成するだけで、枝があります。
しかし、リモートブランチの追跡については知りません。ブランチの名前を変更/移動した後、それをセットアップする必要があるかもしれません。
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
あなたがいるブランチの名前を変更することはできないので、コミットを移動したパフォーマンスブランチをチェックアウトしました。