Come far avanzare rapidamente un ramo verso la testa?


249

Sono passato al master dopo aver sviluppato un ramo per molto tempo. Il registro mostra:

Il tuo ramo è dietro 'origine / master' di 167 commit e può essere fatto avanzare rapidamente.

Provai:

git checkout HEAD

Non ha alcun effetto. Questo perché ho verificato un commit intermedio sul master.

Come far rimanere in testa il maestro?


6
git checkout HEADnon fa mai niente. HEADindica già il commit del check-out.
Emre Tapcı,

Risposte:


244

fare:

git checkout master
git pull origin

recupererà e unirà il origin/masterramo (si può semplicemente dire git pullcome l'origine è l'impostazione predefinita).


52
Penso che la risposta di Rob sia migliore. Di solito mi imbatto in questa situazione in cui ho appena finito di tirare e poi passo a un altro ramo, che deve essere anticipato. Mi dà fastidio se devo fare un altro pull (no-op) e aspettare che si completi; fare un'operazione solo locale è più veloce ed è quello che voglio comunque.
Barone Schwartz,

353

Prova git merge origin/master. Se vuoi essere sicuro che fa solo un avanzamento veloce, puoi dire git merge --ff-only origin/master.


4
Questo è bello da usare quando il telecomando ha alcuni cerchi di autenticazione da saltare. Quando inserisco un ramo, devo autenticarmi. Quindi, quando passo a un altro ramo (ad esempio, per selezionare le mie modifiche), preferisco usare questo mergecomando in modo da non dover ripetere l'autenticazione.
RustyTheBoyRobot

30
--ff-onlyè estremamente utile.
Luca

4
Non so se la origin/masterparte è necessaria o se è predefinita in modo sensato, ma ho trovato utile creare un alias per l'avanzamento veloce, quindi volevo assicurarmi che il ramo a monte fosse usato invece di codificarlo a origin/master: ff = merge --ff-only @{u}( @{u}è a monte) .
Thor84no,

2
è meglio della risposta suggerita se sei offline
Jacek Pietal,

1
Potresti spiegare perché un semplice pull non fa la stessa cosa? Inoltre, pull è ancora necessario se lo facciamo?
Zuzu Corneliu,

40

Nella tua situazione, git rebasefarebbe anche il trucco. Dal momento che non hai cambiamenti che il master non ha, git avanzerà rapidamente. Se stai lavorando con un flusso di lavoro rebase, questo potrebbe essere più consigliabile, dato che non finiresti con un commit di unione se sbagli.

username@workstation:~/work$ git status
# On branch master
# Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
#   (use "git pull" to update your local branch)
#
nothing to commit, working directory clean
username@workstation:~/work$ git rebase
First, rewinding head to replay your work on top of it...
Fast-forwarded master to refs/remotes/origin/master.
# On branch master
nothing to commit, working directory clean

1
E molto utile per me dato che non dovremmo usare git pull!
Stefan,

Anche se hai qualche cambiamento in sospeso, puoi sempre riporre e rifare, non so se questo è il modo "corretto" ma fa miracoli.
fn.

28
git checkout master
git pull

dovrebbe fare il lavoro.

Riceverai il messaggio "Il tuo ramo è dietro" ogni volta che quando lavori su un ramo diverso dal master , qualcuno fa modifiche al master e tu vai a tirare.

(branch) $ //hack hack hack, while someone push the changes to origin/master
(branch) $ git pull   

ora viene estratto il riferimento origine / master, ma il master non viene unito ad esso

(branch) $ git checkout master
(master) $ 

ora master è dietro origin / master e può essere fatto avanzare rapidamente

this will pull and merge (so merge also newer commits to origin/master)
(master) $ git pull 

this will just merge what you have already pulled
(master) $ git merge origin/master

ora il tuo master e origin / master sono sincronizzati


13

Se ti trovi su un ramo diverso e vuoi provare la versione più recente di master, puoi anche farlo

git checkout -B master origin/master


9

A chiunque desideri avanzare rapidamente non si trova su un altro ramo remoto (incluso se stesso) senza aver verificato quel ramo, puoi fare:

git fetch origin master:other

Questo fondamentalmente veloce in avanti l'indice di otherper origin/masterse non si è in otherramo. Puoi avanzare rapidamente più rami in questo modo.

Se stai lavorando su un altro ramo da qualche tempo e volevi aggiornare i rami stantii da remoto alla loro rispettiva testa:

git fetch origin master:master other:other etc:etc

2

Nessuna complessità richiesta basta stare in piedi al tuo ramo e fare un tiro git ha funzionato per me

Oppure, come secondo, prova git pull origin master solo nel caso in cui tu sia sfortunato con il primo comando


0

Per rifare il ramo del local tracker corrente spostando le modifiche locali sopra l'ultimo stato remoto:

$ git fetch && git rebase

Più in generale, per avanzare rapidamente e eliminare le modifiche locali ( hard reset ) *:

$ git fetch && git checkout ${the_branch_name} && git reset --hard origin/${the_branch_name}

per avanzare rapidamente e mantenere le modifiche locali ( rebase ):

$ git fetch && git checkout ${the_branch_name} && git rebase origin/${the_branch_name}

* - per annullare innanzitutto la modifica causata da un hard reset involontario git reflog, che visualizza lo stato di HEAD in ordine inverso, trova l'hash a cui puntava HEAD prima dell'operazione di reset (di solito ovvio) e ripristina il ramo a tale hash.


0

Nel tuo caso, per avanzare rapidamente, esegui:

$ git merge --ff-only origin/master

Questo utilizza l' --ff-onlyopzione di git merge, poiché la domanda richiede specificamente "avanzamento rapido".

Ecco un estratto git-merge(1)che mostra più opzioni di avanzamento rapido:

--ff, --no-ff, --ff-only
    Specifies how a merge is handled when the merged-in history is already a descendant of the current history.  --ff is the default unless merging an annotated
    (and possibly signed) tag that is not stored in its natural place in the refs/tags/ hierarchy, in which case --no-ff is assumed.

    With --ff, when possible resolve the merge as a fast-forward (only update the branch pointer to match the merged branch; do not create a merge commit). When
    not possible (when the merged-in history is not a descendant of the current history), create a merge commit.

    With --no-ff, create a merge commit in all cases, even when the merge could instead be resolved as a fast-forward.

    With --ff-only, resolve the merge as a fast-forward when possible. When not possible, refuse to merge and exit with a non-zero status.

Avanzamento veloce abbastanza spesso da giustificare un alias:

$ git config --global alias.ff 'merge --ff-only @{upstream}'

Ora posso eseguire questo per avanzare rapidamente:

$ git ff

-2

Sposta il puntatore del ramo su HEAD:

git branch -f master

Il tuo ramo masteresiste già, quindi git non ti permetterà di sovrascriverlo, a meno che tu non usi ... -f(questo argomento sta per --force)

Oppure puoi usare rebase:

git rebase HEAD master

Fallo a tuo rischio;)


1
Non tentare questo. se si verifica la seguente situazione, accadranno cose brutte: C0 --- C1 --- C2 --- C3 --- C4 (master). C0 --- C1 --- C2 --- B1 --- B2 --- B3 (dev) Se la tua testa è su B3 (dev) e fai git branch -f master, finirai con C0 - - C1 --- C2 --- B1 --- B2 --- B3 (dev) (master). C3 --- C4 non sono raggiungibili da nessun ramo e alla fine verranno raccolti. Se ti trovi in ​​questa situazione, guarda reflog e checkout C4 commit con l'opzione -b <branch> per creare una nuova filiale.
A_P,
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.