Condivisione della tecnologia

[Note sullo studio di Git] Capitolo 3 Elementi di ramificazione, unione e configurazione (parte 1)

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Capitolo 3 Ramificazione, fusione e elementi di configurazione

argomento correlato:

  • Gestione filiale locale
  • filiale remota
  • Forza l'unione delle versioni
  • utilizzo git reuse recorded resolution (rerere) Unisci versioni Git in conflitto
  • Calcolare le differenze tra i rami
  • Ramo orfano (Orphan branches

3.1 Gestione delle filiali locali

Se git La biblioteca è locale e, anche se non è necessario condividere il codice da remoto, la filiale locale può essere gestita come un magazzino da condividere da remoto. Come mostrato in questo esempio, è necessario prima copiare un magazzino nell'area locale, il che significa che esiste una libreria remota.

# clone the jgit repository to match
$ git clone https://git.eclipse.org/r/jgit/jgit 
$ cd jgit 
# Whenever you start working on a bug fix or a new 
# feature in your project, you should create a branch
$ git branch newBugFix 
$ git branch 
* master 
 newBugFix
# The newBugFix branch points to the current HEAD
# to verify this:
$ git log -1 newBugFix --format=format:%H
25fe20b2dbb20cac8aa43c5ad64494ef8ea64ffc
# edit branch description
$ git branch --edit-description newBugFix
# Add description in a newly opened editor
Refactoring the Hydro controller 

The hydro controller code is currently horrible needs to be refactored.  
# Show the description
$ git config --get branch.newBugFix.description 
Refactoring the Hydro controller 

The hydro controller code is currently horrible and needs to be refactored.  
# Show the commit hash the new branch based on
$ cat .git/refs/heads/newBugFix 
25fe20b2dbb20cac8aa43c5ad64494ef8ea64ffc 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

Si può vedere che il ID con correnteHEAD DiID coerente.

Nota: dopo aver creato un nuovo ramo, è necessario passare a questo ramo per utilizzare:git checkout -b <newBranch>

Espandi: se non segui HEAD Crea un nuovo ramo, ma un determinato ID commit (979e346), in questo momento la presentazione basata su questa versione può essere scritta:

# new branch from commit ID
$ git branch anotherBugFix 979e346 
$ git log -1 anotherBugFix --format=format:%h 
979e346 
$ git log -1 anotherBugFix --format=format:%H 
979e3467112618cc787e161097986212eaaa4533 
# checkout new branch
$ git checkout -b lastBugFix 979e346
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

Visualizza le informazioni sulla filiale:

  1. git branch: solo nome della filiale
  2. git branch -v: Annotato e semplificato in base a 1 SHA-1
  3. git branch -vv: Visualizza i nomi dei rami di tracciamento remoto in base a 2
$ git branch -v 

  anotherBugFix 979e346 Interactive Rebase: Do actions if  
* lastBugFix    979e346 Interactive Rebase: Do actions if  
  master        25fe20b Add missing package import for jg 
  newBugFix     25fe20b Add missing package import for jg
$ git branch -vv 

  anotherBugFix 979e346 Interactive Rebase: Do actions if e 
* lastBugFix    979e346 Interactive Rebase: Do actions if e 
  master        25fe20b [origin/master] Add missing package  
  newBugFix     25fe20b Add missing package import for g 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

3.2 Filiale remota

A volte, il codice nativo può essere generato da un clone del codice base di qualcun altro.A questo punto, il codice locale ha un magazzino remoto, solitamente chiamatoorigin fonte.Per comprendere Git e il suo codice base remoto, puoi iniziare congit status Il comando inizia:

$ git checkout -b remoteBugFix --track origin/stable-3.2
Switched to a new branch 'remoteBugFix'
Branch 'remoteBugFix' set up to track remote branch 'stable-3.2' from 'origin'.
  • 1
  • 2
  • 3

visibile,remoteBugFix Il ramo di monitoraggio remoto della filiale èorigin/stable-3.2 .In questo momentogit status I risultati mostrano il localeHEAD con telecomandoHEAD Se il ramo locale può essere inoltrato rapidamente al ramo remoto. Gli esempi sono i seguenti:

# Find a commit on remote branch
$ git log -10 origin/stable-3.2 --oneline
# Checkout specific SHA-1
$ git reset --hard 2e0d178
HEAD is now at 2e0d17885 Add recursive variant of Config.getNames() methods
# Use 'git status' to see the free benefit of Git
$ git status
On branch remoteBugFix
Your branch is behind 'origin/stable-3.2' by 9 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

nothing to commit, working tree clean
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

Avviso:

  1. Ecco un concetto che ricorre spesso:fast-forward, significa che Git può fondersi HEAD Passare alla versione più recente;
  2. git pull Il comando è in realtà una combinazione di due comandi:
    1. git fetch
    2. git merge(Unisci il ramo di monitoraggio remoto al ramo locale)

Esegui quanto sopra git status il comando viene utilizzato per simulare l'esecuzione localegit fetch Successivamente, il nuovo contenuto sul ramo remoto è stato trasferito al ramo di tracciamento localeorigin/stable-3.2 stato. Continuando l'unione verrà effettivamente sincronizzata con il ramo locale:

$ git merge origin/stable-3.2
Updating 2e0d17885..f839d383e
Fast-forward
 .../org/eclipse/jgit/api/RebaseCommandTest.java    | 213 +++++++++++++++++----
 .../src/org/eclipse/jgit/api/RebaseCommand.java    |  31 +--
 .../jgit/errors/IllegalTodoFileModification.java   |  59 ++++++
 .../eclipse/jgit/lib/BaseRepositoryBuilder.java    |   2 +-
 .../src/org/eclipse/jgit/lib/Config.java           |   2 +
 .../src/org/eclipse/jgit/lib/RebaseTodoLine.java   |  16 +-
 6 files changed, 266 insertions(+), 57 deletions(-)
 create mode 100644 org.eclipse.jgit/src/org/eclipse/jgit/errors/IllegalTodoFileModification.java
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

Secondo i risultati dell'esecuzione, si può vedere che come git status Come indicato nel prompt, questa unione è un'unione con avanzamento rapido (fast-forward merge

Oltre a essere utilizzato durante la creazione di una filiale locale --track Specificare il ramo di monitoraggio remoto,git È inoltre disponibile il supporto per specificare rami remoti su rami esistenti. Se crei in anticipo una filiale locale e dimentichi di associare la filiale remota,git Questa funzionalità di associazione tardiva è utile:

# Checkout a new branch without relating to its remote tracking branch
$ git checkout -b remoteBugFix2 2e0d17 
Switched to a new branch 'remoteBugFix2' 
# Set the tracking branch manually by using -u or --set-upstream-to
$ git branch --set-upstream-to origin/stable-3.2 
Branch remoteBugFix2 set up to track remote branch stable-3.2 from origin. 
# Validate current status
$ git status 
On branch remoteBugFix2 
Your branch is behind 'origin/stable-3.2' by 9 commits, and can be fast-forwarded. 
  (use "git pull" to update your local branch) 
nothing to commit, working directory clean 
# Same as the previous demo as expected.
# Update the branch (also a fast-forward merge with origin/stable-3.2)
$ git pull 
Updating 2e0d17885..f839d383e
Fast-forward
 .../org/eclipse/jgit/api/RebaseCommandTest.java    | 213 +++++++++++++++++----
 .../src/org/eclipse/jgit/api/RebaseCommand.java    |  31 +--
 .../jgit/errors/IllegalTodoFileModification.java   |  59 ++++++
 .../eclipse/jgit/lib/BaseRepositoryBuilder.java    |   2 +-
 .../src/org/eclipse/jgit/lib/Config.java           |   2 +
 .../src/org/eclipse/jgit/lib/RebaseTodoLine.java   |  16 +-
 6 files changed, 266 insertions(+), 57 deletions(-)
 create mode 100644 org.eclipse.jgit/src/org/eclipse/jgit/errors/IllegalTodoFileModification.java
# The output is still the same as previous one as expected.
# Validate the current HEAD with origin/stable-3.2
$ git log -1 origin/stable-3.2  --format=format:%h
f839d383e
# Still the same.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

3.3 Forzare la generazione di commit di unione

Prima di leggere questo libro, potresti aver visto molti esempi di catene di distribuzione del software e modelli di ramificazione; potresti anche aver provato diverse strategie, solo per scoprire che una volta che uno strumento supporta un flusso di lavoro specifico, è difficile implementarlo completamente scenari applicativi desiderati. E Git supporta quasi tutti gli scenari di flusso di lavoro. Un requisito comune è essere in grado di generare un commit di unione quando si unisce una funzionalità (anche se le nuove funzionalità possono essere incorporate in modalità avanzamento rapido). Tale requisito viene spesso utilizzato per indicare che una determinata funzionalità è stata unita e si desidera che l'operazione di unione venga archiviata esplicitamente nel magazzino.

Suggerimenti

Mentre Git Fornisce un modo comodo e veloce per accedere a tutte le informazioni di invio,Git La libreria dovrebbe effettivamente essere utilizzata cometronco d'albero, non solo il codice sorgente backup

L'esempio seguente genererà un commit di unione forzata:

# Checkout branch stable-3.1 as remoteOldBugFix for use
$ git checkout -b remoteOldBugFix --track origin/stable-3.1
# force a merge commit
$ git merge origin/stable-3.2 --no-ff --edit --quiet
  • 1
  • 2
  • 3
  • 4

Nota, usa --edit Dopo aver impostato i parametri, è possibile modificare le informazioni predefinite sul commento del commit dell'unione nell'editor che si apre.nell'esempio--no-ff Indica la disabilitazione della modalità di avanzamento veloce;--quiet Utilizzato per semplificare il contenuto dell'output sullo schermo. L'effetto finale è mostrato in figura:

Figura 3-1

Per fare un confronto con il modo normale, elimina quando si esegue un'unione --no-ff Elementi dei parametri:

# Reset to initial status
$ git reset --hard  remotes/origin/stable-3.1
# Merge with fast forward by default
$ git merge origin/stable-3.2 --quiet
  • 1
  • 2
  • 3
  • 4

L'effetto è il seguente:

Figura 3-2

Unisci i contenuti a metà

Oltre a eseguire una fusione completa, Git consente anche agli utenti di intervenire nel contenuto unito e decidere autonomamente quale contenuto includere nella fusione.utilizzo--no-commit parametro,git Interromperà il processo prima del commit di unione, consentendo all'utente di modificare o aggiungere file prima del commit finale. Ad esempio, il numero di versione del progetto viene modificato nel ramo della funzionalità del progetto, ma il ramo principale non viene modificato. L'operazione di unione predefinita aggiornerà il numero di versione al numero di versione nel ramo della funzionalità, ma in realtà non desideri modificare il numero di versione.Usa questa volta--no-commit può essere evitato.

Nell'esempio seguente viene illustrata un'operazione di unione con l'intervento dell'utente. Durante l'unione, vuoi escludere manualmente il commit che ha aggiornato il file di licenza:

# Clone repo into demo
$ git clone https://github.com/PacktPublishing/Git-Version-Control-Cookbook-Second-Edition_hello_world_flow_model.git demo
$ cd demo
# Checkout remote branch
$ git checkout -b remotePartlyMerge --track origin/release/1.0
Branch remotePartlyMerge set up to track remote branch release/1.0 from origin. 
Switched to a new branch 'remotePartlyMerge'
# Force a merge commit with --no-commit flag
$ git merge origin/master  --no-ff --no-commit
Automatic merge went well; stopped before committing as requested
# Exclude LICENSE file
$ git reset LICENSE
# Resume merging
$ git commit -m "Merging without LICENSE"
[remotePartlyMerge 1202b0e] Merging without LICENSE
# Check status (only LICENSE file remain untracked)
$ git status
On branch remotePartlyMerge
Your branch is ahead of 'origin/release/1.0' by 6 commits.
  (use "git push" to publish your local commits)

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        LICENSE

nothing added to commit but untracked files present (use "git add" to track)
# check difference excluding LICENSE file (only on Linux)
$ git diff origin/master !(LICENSE)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

Considerando il problema della velocità della rete, il repository di esempio è stato Git-Version-Control-Cookbook-Second-Edition_hello_world_flow_model.git Aggiungi a questa libreria didattica (repos/ex3.3-no-commit-repo.rar