31

私は最近、自分の git リポジトリを台無しにしてしまったので、解決策があるかどうかを知りたいと思っています。

私のセットアップはこれです:

Central repo on github.
Personal repo on github (which is a fork of Central)
   +Central is setup as remote (upstream/master)
   +Master branch (origin/master)
   +Feature branch (origin/feature)

私のワークフローは次のようなものでした:

Need to fix something in Central:
   1. checkout Master
   2. Make changes
   3. Pull from upstream/master and merge
   3. Commit, push to upstream/master

Need to work on a New Feature:
   1. Checkout/Create Feature branch
   2. Work work work
   3. Pull from upstream/master and merge
   4. Commit, push to upstream/master

このようにして、Master ブランチの Central を常に元の状態に保つことができました。

今私がしたことは、代わりにマスターブランチで作業を開始したことです。そのため、マスターに変更を加えたので、そこから分岐して Central のコピーを取得できなくなりました。いくつかの修正を行って Central にプッシュする必要があるときはいつでも、Central を別のディレクトリに複製して、そこから作業する必要があります。

私の質問: マスターで行ったすべての変更を別のブランチ (フィーチャーなど) に移動しながら、マスターをセントラルの同一のコピーに「戻す」方法はありますか?

紛らわしいことは承知しています。助けていただければ幸いです。不明な点があれば補足します。

4

3 に答える 3

46

パット・ノッツとボンベにほのめかされた、解決策は非常に単純でした。

#Make sure we're on the master branch
$ git checkout master

# Make a new branch to hold the work I've done
$ git branch old_master

# Save this branch on my remote repo (for backup)
$ git checkout old_master
$ git push origin old_master

# Reset my local master back to match the commit just before I started 
# working on my new feature
$ git checkout master
$ git reset --hard 2aa93842342342

# Get it to be the same as my Central
$ git pull upstream master

# Now DELETE my master on my remote repo
$ git push origin :master

# And recreate it
$ git push origin master

# Branch created!
#* [new branch]      master -> master

#

これで、master と old_master という 2 つの素敵なブランチができました。master は私の Central のコピーであり、プロダクションへの修正用であり、old_master は以前に行ったすべての作業を保持しています!

ありがとう!

于 2009-04-21T17:38:23.940 に答える
16
# Make sure we're on the master branch
$ git checkout master

# Make a new branch to hold the work I've done
$ git branch old_master

# Reset my local master back to match origin/master
$ git reset --hard origin/master

これで、ブランチold_masterと同じようにチェックアウトして使用できますfeature

于 2009-04-21T15:20:27.017 に答える
3

正確にはどういう意味ですか

マスターを台無しにして、そこから分岐できなくなりました。

リポジトリ内の任意のコミットからいつでも新しいブランチを作成できます。それがどんなに「めちゃくちゃ」であっても、Git には概念がありません。

基本的に、Git はオブジェクトを明示的に削除せず、参照されていない (ぶら下がっている) オブジェクトのみをガベージ コレクションするため、リポジトリを以前の状態に戻すことができます。したがって、リポジトリがどのように見えるかを知る必要があります。gitkまたはgit logそこであなたを助けることができます。

ローカル リポジトリを希望の状態に復元したら、それを中央のパブリック リポジトリにプッシュして戻すことができます。その結果、早送り以外のプッシュが発生する場合は、プッシュ--force時にフラグを指定する必要がある場合があります。

于 2009-04-21T14:59:25.133 に答える