Come si ripristina il ramo principale di git sul ramo a monte in un repository biforcuto?


103

Ho completamente incasinato il ramo principale del mio repository git biforcuto.

Voglio ripristinare completamente il ramo principale che è stato inserito nel mio fork con il contenuto del repository principale a monte. Non ho alcun interesse a conservare le modifiche o la cronologia del ramo principale.

L'approccio più semplice sarebbe stato quello di eliminare il mio repo biforcuto e refork dal progetto a monte. Tuttavia, ho del lavoro in altri rami spinti che non voglio perdere.

Quindi come resettare il mio ramo master pushed con il master upstream?


git clone https://myrepo.git
cd myrepo
git remote add upstream https://upstream.git
git fetch upstream

Dove devo andare da qui per ripristinare i miei rami master locale e remoto con il master a monte?

Risposte:


189

Puoi reimpostare il ramo master locale alla versione upstream e inviarlo al tuo repository.

Supponendo che "upstream" sia il repository originale e "origin" sia il tuo fork:

# ensures current branch is master
git checkout master

# pulls all new commits made to upstream/master
git pull upstream master

# this will delete all your local changes to master
git reset --hard upstream/master

# take care, this will delete all your changes on your forked master
git push origin master --force

(Puoi definire il repository originale come "upstream" con git remote add upstream /url/to/original/repo.)


1
Probabilmente dovrebbe essere git reset --hard upstream/masteranche il ripristino della directory di lavoro. Ma la tua risposta è comunque corretta.
j6t

Anche non usare "--hard" funziona mantenendo le modifiche locali. Vorrei fare l'hard reset in un passaggio separato per eventualmente ispezionare il diff.
Johannes Barop

1
Questo è stato molto utile.
Aleem S

4
git fetch upstreamPrima è necessario chiamare
Henry E

3
Come nota @HenryE, non riuscire a recuperare prima le modifiche a monte con in git fetch upstreamgenere produce il seguente errore non leggibile dall'uomo:"fatal: ambiguous argument 'upstream/master': unknown revision or path not in the working tree."
Cecil Curry

5

Ciò ripristinerebbe il tuo ramo principale con il master a monte e se il ramo è stato aggiornato da quando hai biforcato, attirerà anche quelle modifiche.

git checkout master 
git reset upstream/master
git pull --rebase upstream master
git push origin master --force

PS: Supponendo che Upstream sia il repository originale mentre origin è la tua copia.


Sembra che tu abbia creato il tuo ramo da un commit diverso. Il motivo principale per il ribasamento è mantenere una cronologia del progetto lineare. Detto questo, non dovresti mai ribase i commit una volta che sono stati pubblicati in un repository pubblico perché questo sostituisce i vecchi commit con quelli nuovi. Per i dettagli, consultare atlassian.com/git/tutorials/rewriting-history/git-rebase
user8128167

0

Ho provato il metodo in questo modo:

$REPO=<repo>
$ORIGIN=<user>/$REPO
$UPSTREAM=<upstream>/$REPO

$ git clone git@github.com:$ORIGIN.git
$ cd $REPO
$ git checkout master
$ git remote add upstream git@github.com:$UPSTREAM.git
$ git reset --hard upstream/master
$ git pull --rebase upstream master
$ git push origin master --force

l'output mostrerà un avviso:

fatal: ambiguous argument 'upstream/master': 
unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

Quindi il modo corretto è messo git pullprima git reset:

$ git clone git@github.com:$ORIGIN.git
$ cd $REPO
$ git checkout master
$ git remote add upstream git@github.com:$UPSTREAM.git
$ git pull --rebase upstream master
$ git reset --hard upstream/master
$ git push origin master --force

quindi l'output sarà come questo:

From github.com:<upstream>/<repo>
 * branch                master     -> FETCH_HEAD
 * [new branch]          master     -> upstream/master
HEAD is now at 7a94b1790 Merge pull request #4237 from <upstream>/...
Current branch master is up to date.
Everything up-to-date.
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.