Technology Sharing

[Git Learning Notes] Chapter 3 Branching, Merging, and Configuration Items (Part 1)

2024-07-12

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

Chapter 3 Branches, Merges, and Configuration Items

related topic:

  • Local branch management
  • Remote branches
  • Force version merge
  • use git reuse recorded resolution (rerere) Merge conflicting Git versions
  • Compute differences between branches
  • Isolated branches (Orphan branches

3.1 Local branch management

if git The library is local, and even if there is no need to share code remotely, local branches can be managed like repositories that need to be shared remotely. As shown in this example, you need to copy a repository to the local first, which means there is a remote library.

# 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

It can be seen from this that the ID With the currentHEAD ofID Consistent.

Note: After creating a new branch, you need to switch to it before you can use it:git checkout -b <newBranch>

Expansion: If not from HEAD Create a new branch, but a commit ID (979e346), at this time the submission based on this version can be written as:

# 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

View branch information:

  1. git branch: Branch name only
  2. git branch -v: Based on 1 with annotations and simplification SHA-1
  3. git branch -vv: Display remote tracking branch names based on 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 Remote branches

Sometimes, local code is likely to be generated by cloning someone else's code base. In this case, the local code has a remote repository, usually called origin Source. Get to know Git and its remote code base, which can be obtained fromgit status The command starts:

$ 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

visible,remoteBugFix The remote tracking branch for the branch isorigin/stable-3.2At this time git status The results show the localHEAD With remoteHEAD Whether the local can fast-forward to the remote branch. The example is as follows:

# 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

Notice:

  1. Here is a concept that comes up often:fast-forward, which means that Git can merge HEAD Move to the latest version;
  2. git pull The command is actually a combination of two commands:
    1. git fetch
    2. git merge(Merge remote tracking branches into local branches)

Execute the above git status Command is used to simulate local executiongit fetch After that, the new content on the remote branch has been pulled to the local tracking branchorigin/stable-3.2 Continue merging to actually synchronize to the local branch:

$ 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

According to the execution results, as git status As the tip says, this merger is a fast-forward merger (fast-forward merge

In addition to creating a local branch with --track Specify a remote tracking branch.git It also supports specifying remote branches on existing branches. If you create a local branch in advance and forget to associate the remote branch,git This late binding feature comes in handy:

# 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 Force a merge commit

Before reading this book, you may have seen many cases of software delivery chains and branch models; you may have been trying different strategies, but in the end you found that once a tool supports a specific workflow, it is difficult to fully meet your other application scenarios. Git supports almost all workflow scenarios. A common requirement is to generate a merge commit when merging a feature (even if the new feature can be merged in fast-forward mode). Such requirements are often used to indicate that a feature has been merged and you want to explicitly store this merge operation in the repository.

Tips

Given that Git Provides a convenient and quick way to access all submission information.Git The library should actually be used aslog, not just the source code Backup

The following example will generate a force merge commit:

# 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

Note that using --edit After the parameter is set, you can modify the default merge commit comment information in the editor that opens.--no-ff Indicates disabling fast forward mode;--quiet Used to simplify screen output content. The final effect is shown in the figure:

Figure 3-1

To contrast with the normal way, remove --no-ff Parameters:

# 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

The effect is as follows:

Figure 3-2

Intervention and merging of content

In addition to performing a complete merge, Git also allows users to intervene in the merge content and decide independently what content is involved in the merge. --no-commit parameter,git It will interrupt the process before the merge commit, allowing the user to modify or add files before the final commit. For example, the project version number is modified in the feature branch of the project, but the main branch is not modified. The default merge operation will update the version number to the version number on the feature branch, but in fact you do not want to change the version number. In this case, use--no-commit It can be avoided.

The following example demonstrates the merge operation under user intervention. When merging, you want to manually remove the commit that updates the license file:

# 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

Considering the network speed problem, the sample warehouse has been Git-Version-Control-Cookbook-Second-Edition_hello_world_flow_model.git Add to this learning library (repos/ex3.3-no-commit-repo.rar