VDone Demo VDone Demo
Home
  • Articles

    • JavaScript
  • Study Notes

    • JavaScript Tutorial
    • Professional JavaScript
    • ES6 Tutorial
    • Vue
    • React
    • TypeScript: Build Axios from Scratch
    • Git
    • TypeScript
    • JS Design Patterns
  • HTML
  • CSS
  • Technical Docs
  • GitHub Tips
  • Node.js
  • Blog Setup
  • Learning
  • Interviews
  • Miscellaneous
  • Practical Tips
  • Friends
About
Bookmarks
  • Categories
  • Tags
  • Archives
GitHub (opens new window)

Nikolay Tuzov

Backend Developer
Home
  • Articles

    • JavaScript
  • Study Notes

    • JavaScript Tutorial
    • Professional JavaScript
    • ES6 Tutorial
    • Vue
    • React
    • TypeScript: Build Axios from Scratch
    • Git
    • TypeScript
    • JS Design Patterns
  • HTML
  • CSS
  • Technical Docs
  • GitHub Tips
  • Node.js
  • Blog Setup
  • Learning
  • Interviews
  • Miscellaneous
  • Practical Tips
  • Friends
About
Bookmarks
  • Categories
  • Tags
  • Archives
GitHub (opens new window)
  • 手册

  • 文档笔记

    • Git Basics and Commands
    • Git Branches - Branch Internals
    • Git Branch Creation and Merging - Branch Operations
      • Git Branch Management - Viewing Branches
      • Git Branching Workflows
      • Git Branches - Remote Branches
      • Git Branches - Rebasing
      • Git Tools - Revision Selection
      • Git Tools - Interactive Staging
      • Git Tools - Rewriting History
      • Git Tools - Reset Demystified
    • 《Git》学习笔记
    • 文档笔记
    xugaoyi
    2020-11-18
    Contents

    Git Branch Creation and Merging - Branch Operations

    # Git Branch Creation and Merging - Branch Operations

    Documentation: Git Branching - Basic Branching and Merging (opens new window)

    # Creating and Switching to a Branch

    Suppose you have a new requirement and need to work on a new branch iss53:

    $ git checkout -b iss53  # b stands for branch
    
    1

    This is shorthand for the following two commands:

    $ git branch iss53
    $ git checkout iss53
    
    1
    2

    # Switching Branches

    An urgent issue suddenly comes up, and you need to fix it on the original master branch:

    $ git checkout master
    
    1

    Before switching to master, make sure the iss53 branch is in a clean state (all changes have been committed).

    Note: Switching branches causes Git to reset your working directory.

    The word checkout literally means "to check out." checkout <branch> checks out a branch, i.e., checks out the code of the specified branch, which resets the working directory and switches the branch.

    Next, you need to fix the urgent issue. Create a hotfix branch and work on it until the issue is resolved:

    $ git checkout -b hotfix
    
    # In between, modify the code on hotfix and commit
    $ echo 'test' > ./hotfix.txt
    $ git add .
    $ git commit -m 'fixed'
    
    1
    2
    3
    4
    5
    6

    # Merging Branches

    $ git checkout master # First, switch back to the master branch
    $ git merge hotfix # Merge the hotfix branch in
    
    1
    2

    # Deleting a Branch

    $ git branch -d hotfix # d stands for delete
    
    # Then switch back to iss53 to continue working
    $ git checkout iss53
    
    1
    2
    3
    4

    Note that deleting a branch uses the branch command.

    # Merging Branches After Multiple Commits

    Suppose you have finished fixing issue #53 and want to merge it into the master branch:

    $ git checkout master
    $ git merga iss53
    
    1
    2

    This looks similar to the previous merge. But now your development history has diverged from an earlier point. Because the commit on master is not a direct ancestor of the commit on iss53, Git has to do some extra work. In this case, Git uses the snapshots pointed to by the tips of both branches and their common ancestor to perform a simple three-way merge.

    Unlike simply moving the branch pointer forward, Git creates a new snapshot from the result of this three-way merge and automatically creates a new commit pointing to it. This is called a merge commit, and its special feature is that it has more than one parent commit.

    # Branch Merging with Conflicts

    If you made different changes to the same part of the same file in two different branches, Git won't be able to merge them cleanly, resulting in a conflict.

    When CONFLICT appears during the merge, it indicates a conflict:

    $ git merge iss53
    Auto-merging index.html
    CONFLICT (content): Merge conflict in index.html
    Automatic merge failed; fix conflicts and then commit the result.
    
    1
    2
    3
    4

    Use git status to see the unmerged status.

    Any files with merge conflicts will be marked as unmerged. Git adds standard conflict-resolution markers to the conflicting files, so you can open them and manually resolve the conflicts. Files with conflicts will contain special sections that look like this:

    <<<<<<< HEAD:index.html
    <div id="footer">contact : email.support@github.com</div>
    =======
    <div id="footer">
     please contact us at support@github.com
    </div>
    >>>>>>> iss53:index.html
    
    1
    2
    3
    4
    5
    6
    7

    You need to manually resolve the conflicts. After resolving all conflicts in all files, use git add on each file to mark it as resolved. Once you stage the formerly conflicting files, Git considers them resolved.

    If you are satisfied with the result and have confirmed that all previously conflicting files have been staged, you can run git commit to finalize the merge commit.

    Edit (opens new window)
    #Git
    Last Updated: 2026/03/21, 12:14:36
    Git Branches - Branch Internals
    Git Branch Management - Viewing Branches

    ← Git Branches - Branch Internals Git Branch Management - Viewing Branches→

    Recent Updates
    01
    How I Discovered Disposable Email — A True Story
    06-12
    02
    Animations in Grid Layout
    09-15
    03
    Renaming a Git Branch
    08-11
    More Articles >
    Theme by VDone | Copyright © 2026-2026 Nikolay Tuzov | MIT License | Telegram
    • Auto
    • Light Mode
    • Dark Mode
    • Reading Mode