Quale di queste righe è corretta?
git checkout 'another_branch'
O
git checkout origin 'another_branch'
O
git checkout origin/'another_branch'
git checkout 'another_branch'
O
git checkout origin 'another_branch'
O
git checkout origin/'another_branch'
Risposte:
Se another_branchesiste già localmente e non sei su questo ramo, git checkout another_branchpassa al ramo.
Se another_branchnon esiste ma origin/another_branchesiste, allora git checkout another_branchequivale a git checkout -b another_branch origin/another_branch; git branch -u origin/another_branch. Quella di creare another_branchda origin/another_branche insieme origin/another_branchcome il monte dianother_branch .
Se nessuno dei due esiste, git checkout another_branchrestituisce errore.
git checkout origin another_branchrestituisce errore nella maggior parte dei casi. Se originè una revisione ed another_branchè un file, verifica il file di quella revisione ma molto probabilmente non è quello che ti aspetti. originviene utilizzato principalmente in git fetch, git pulle git pushcome telecomando, un alias dell'URL nel repository remoto.
git checkout origin/another_branch riesce se origin/another_branch esiste. Porta ad essere nello stato HEAD distaccato, non su alcun ramo. Se si effettuano nuovi commit, i nuovi commit non sono raggiungibili da nessun ramo esistente e nessuno dei rami verrà aggiornato.
AGGIORNARE :
Poiché 2.23.0 è stato rilasciato, con esso possiamo anche usare git switchper creare e cambiare rami.
Se fooesiste, prova a passare a foo:
git switch foo
Se foonon esiste ed origin/fooesiste, prova a creare fooda origin/fooe quindi passa a foo:
git switch -c foo origin/foo
# or simply
git switch foo
Più in generale, se foonon esiste, prova a creare fooda un ref o commit noto e passa a foo:
git switch -c foo <ref>
git switch -c foo <commit>
Se manteniamo un repository in Gitlab e Github contemporaneamente, il repository locale potrebbe avere due telecomandi, ad esempio, originper Gitlab e githubGithub. In questo caso il repository ha origin/fooe github/foo. git switch foosi lamenterà fatal: invalid reference: foo, perché non sa da quale ref, origin/fooo github/foo, creare foo. Dobbiamo specificarlo con git switch -c foo origin/fooo in git switch -c foo github/foobase alle necessità. Se vogliamo creare rami da entrambi i rami remoti, è meglio usare nomi distintivi per i nuovi rami:
git switch -c gitlab_foo origin/foo
git switch -c github_foo github/foo
Se fooesiste, prova a ricreare / forzare-creare fooda (o reimpostare foosu) un ref o commit noto e quindi passare a foo:
git switch -C foo <ref>
git switch -C foo <commit>
che equivalgono a:
git switch foo
git reset [<ref>|<commit>] --hard
Prova a passare a una HEAD distaccata di un ref o commit noto:
git switch -d <ref>
git switch -d <commit>
Se vuoi solo creare un ramo ma non passare ad esso, usa git branchinvece. Prova a creare un ramo da un ref o commit noto:
git branch foo <ref>
git branch foo <commit>
git checkoutcomando fa troppe cose, secondo me. Ecco perché ci sono così tante modalità operative qui. Se l'unica cosa che git checkoutfacesse fosse cambiare ramo, la risposta sarebbe semplice, ma può anche creare rami e persino estrarre file da specifici commit senza cambiare ramo.
git switchper passare a un ramo.
git checkoutinvece per le vecchie versioni, che funziona anche nelle versioni moderne.
Passare a un altro ramo in git. Risposta semplice,
git-checkout - Cambia i rami o ripristina i file dell'albero di lavoro
git fetch origin <----this will fetch the branch
git checkout branch_name <--- Switching the branch
Prima di cambiare il ramo assicurati di non avere alcun file modificato, in tal caso, puoi eseguire il commit delle modifiche o riporlo.
[git checkout "branch_name" ]
è un altro modo di dire:
[git checkout -b branch_name origin/branch_name ]
nel caso in cui "branch_name" esiste solo remoto.
[git checkout -b branch_name origin/branch_name ] è utile in caso di più telecomandi.
Per quanto riguarda [ git checkout origin 'another_branch'] non sono sicuro che sia possibile, AFAK puoi farlo usando il comando "fetch" - [ git fetch origin 'another_branch']
Con Git 2.23 in poi, si può usare git switch <branch name>per cambiare ramo.
Ciò che ha funzionato per me è il seguente:
Passa al ramo necessario:
git checkout -b BranchName
E poi ho tirato il "maestro" di:
git pull origin master
Comandi utili per lavorare nella vita quotidiana:
git checkout -b "branchname" -> creates new branch
git branch -> lists all branches
git checkout "branchname" -> switches to your branch
git push origin "branchname" -> Pushes to your branch
git add */filename -> Stages *(All files) or by given file name
git commit -m "commit message" -> Commits staged files
git push -> Pushes to your current branch
Se vuoi che il ramo tenga traccia del ramo remoto, il che è molto importante se hai intenzione di eseguire il commit delle modifiche al ramo e di estrarre le modifiche, ecc., Devi utilizzare aggiungi a -t per il checkout effettivo, ad esempio:
git checkout -t branchname
Dai un'occhiata : git branch -a
Se stai ricevendo solo un ramo. Quindi procedere di seguito.
git config --listgit config --unset remote.origin.fetch git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/*depthparametro) in precedenza e ora ti chiedi perché non puoi recuperare altri rami remoti ottenendo error: pathspec 'another_branch' did not match any file(s) known to giti comandi suggeriti sopra. Sicuramente non è la questione originale, ma può aiutare gli altri a grattarsi la testa qui.
Sto usando questo per passare da un ramo ad un altro chiunque tu possa usare, funziona per me come un incantesimo.
git switch [branchName] OPPURE checkout [branchName]
es: git switch sviluppa O
git checkout sviluppa
git checkout [branch]per la maggior parte degli utenti che arrivano a questa domanda