技術共有

【Git学習記】第3章 分岐・結合・設定項目(前編)

2024-07-12

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

第3章 分岐・結合・設定項目

関連トピック:

  • 地方支店の管理
  • リモートブランチ
  • 強制的にバージョンをマージする
  • 使用 git reuse recorded resolution (rerere) 競合する Git バージョンをマージする
  • ブランチ間の差異を計算する
  • 孤立したブランチ (Orphan branches

3.1 地方支店の管理

もし git ライブラリはローカルであり、コードをリモートで共有する必要がない場合でも、ローカル ブランチはリモートで共有する必要がある倉庫のように管理できます。この例に示すように、最初に倉庫をローカル エリアにコピーする必要があります。これは、リモート ライブラリが存在することを意味します。

# 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

ことがわかります。 ID 電流ありHEADID 一貫性のある。

注: 新しいブランチを作成した後、次のものを使用するには、このブランチに切り替える必要があります。git checkout -b <newBranch>

展開: フォローしない場合 HEAD 新しいブランチを作成しますが、特定のコミット ID (979e346)、現時点では、このバージョンに基づいた送信を次のように書くことができます。

# 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

ブランチ情報の表示:

  1. git branch:支店名のみ
  2. git branch -v:1をベースに注釈を付けて簡略化 SHA-1
  3. git branch -vv: 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 リモートブランチ

場合によっては、ネイティブ コードが他人のコード ベースのクローンから生成されることがあります。この時点で、ローカル コードには、通常は次のように呼ばれるリモート ウェアハウスがあります。 origin ソース。Git とそのリモート コード ベースを理解するには、以下から始めることができます。git status コマンドは次のように開始されます。

$ 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

見える、remoteBugFix ブランチのリモート追跡ブランチは、origin/stable-3.2 。現時点ではgit status 結果は地元の状況を示していますHEAD リモコン付きHEAD ローカル ブランチをリモート ブランチに早送りできるかどうか。例は次のとおりです。

# 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

知らせ:

  1. よく出てくる概念は次のとおりです。fast-forward、Git がマージできることを意味します HEAD 最新バージョンに移行します。
  2. git pull このコマンドは実際には 2 つのコマンドを組み合わせたものです。
    1. git fetch
    2. git merge(リモート追跡ブランチをローカルブランチにマージ)

上記を実行してください git status コマンドはローカル実行をシミュレートするために使用されますgit fetch その後、リモート ブランチの新しいコンテンツがローカルの追跡ブランチにプルされます。origin/stable-3.2 状態。マージを続行すると、実際にはローカル ブランチに同期されます。

$ 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

実行結果によれば、次のようになります。 git status プロンプトに記載されているように、このマージは早送りマージです (fast-forward merge

ローカルブランチを作成するときに使用するほかに --track リモート追跡ブランチを指定します。git 既存のブランチ上でリモート ブランチを指定することもサポートされています。事前にローカル ブランチを作成し、リモート ブランチを関連付けるのを忘れた場合、git この遅延バインディング機能は便利です。

# 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 マージコミットの強制生成

この本を読む前に、ソフトウェア配信チェーンと分岐モデルの多くの例を見たことがあるかもしれません。また、さまざまな戦略を試してみて、一度ツールが特定のワークフローをサポートすると、それを完全に実装するのは難しいことがわかったかもしれません。必要なアプリケーションシナリオ。また、Git はほぼすべてのワークフロー シナリオをサポートしています。 一般的な要件は、機能をマージするときにマージ コミットを生成できることです (新しい機能を早送りモードで組み込むことができる場合でも)。 このような要件は、特定の機能がマージされており、このマージ操作をウェアハウスに明示的に保存することを示すためによく使用されます。

チップ

一方 Git すべての提出情報に簡単にアクセスできる便利な方法を提供します。Git ライブラリは実際には次のように使用する必要がありますログソースコードだけでなく、 バックアップ

次の例では、強制マージ コミットが生成されます。

# 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

注意、使用 --edit パラメータを設定した後、開いたエディタでデフォルトのマージ コミット コメント情報を変更できます。例では--no-ff 早送りモードを無効にすることを示します。--quiet 画面出力内容を簡略化するために使用されます。最終的な効果を図に示します。

図 3-1

通常の方法との比較のため、マージ時に削除してください --no-ff パラメータ項目:

# 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

効果は次のとおりです。

図 3-2

コンテンツを途中で結合する

Git では、完全なマージを実行するだけでなく、ユーザーがマージされたコンテンツに介入して、どのコンテンツをマージに含めるかを独自に決定することもできます。使用--no-commit パラメータ、git マージコミットの前にプロセスを中断し、ユーザーが最終コミットの前にファイルを変更または追加できるようにします。たとえば、プロジェクトのバージョン番号はプロジェクトのフィーチャー ブランチで変更されますが、メイン ブランチは変更されません。デフォルトのマージ操作では、バージョン番号が機能ブランチのバージョン番号に更新されますが、実際にはバージョン番号を変更する必要はありません。この時間を利用してください--no-commit 回避できる。

次の例は、ユーザー介入によるマージ操作を示しています。マージするときに、ライセンス ファイルを更新したコミットを手動で除外したいとします。

# 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

ネットワーク速度の問題を考慮して、サンプル リポジトリは Git-Version-Control-Cookbook-Second-Edition_hello_world_flow_model.git この学習ライブラリに追加 (repos/ex3.3-no-commit-repo.rar