tingtinghsu
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee
  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- title: Git 7.6 git branch 分支 tags: Git, 5x, 5x --- 7.6 0425 Git branch 分支 === # Examle 1 直線進行的分支 ## 1. git branch 列出所有分支 ```bash /Users/tingtinghsu/git-examples/git-branch1 tingdeAir:git-branch1 tingtinghsu$ git branch * master ``` ## 2. git branch `cat` 建立新分支 ```bash tingdeAir:git-branch1 tingtinghsu$ git branch cat /* 貼貼紙 */ * master ``` ## 3. git checkout `cat` 切換到cat分支 `HEAD`: 上帝視角 1. HEAD移動到cat貼紙,並且跟著cat 2. 專案會變成cat貼紙指到的Commit狀態 3. 目前cat跟master所在位置一樣 ```bash tingdeAir:git-branch1 tingtinghsu$ git checkout cat Switched to branch 'cat' tingdeAir:git-branch1 tingtinghsu$ git branch * cat master ``` ### 3-1 在`cat` branch送commit 使用checkout之後送commit 1. cat貼紙與HEAD往前移動(如下圖,`cat`分支變為粗體字) 3. 其他分支留在原地 ![圖:cat貼紙與HEAD往前移動](https://i.imgur.com/auRG8fF.png) ```bash tingdeAir:git-branch1 tingtinghsu$ git branch * cat master tingdeAir:git-branch1 tingtinghsu$ touch .keep tingdeAir:git-branch1 tingtinghsu$ git status On branch cat Untracked files: (use "git add <file>..." to include in what will be committed) .keep tingdeAir:git-branch1 tingtinghsu$ git add . tingdeAir:git-branch1 tingtinghsu$ git commit -m "add cat branch" [cat 8fded81] add cat branch 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 .keep tingdeAir:git-branch1 tingtinghsu$ git log --oneline 8fded81 add cat branch 865f98a add hello-world file e12d8ef add database.yml in config folder 85e7e30 add hello 657fce7 add container abb4f43 update index page cef6e40 create index page cc797cd init commit ``` ### 3-2 git merge 合併分支 (先切回master) 跟master很像的分支,合併時會使用fast-forward快轉 (git圖示上master往前走,沒有小耳朵) 1. 切換回master分支 `git checkout master` - HEAD移動到master貼紙 - 專案會變成master貼紙指到的commit狀態 (如下圖,`master`分支變回粗體字) ![圖:專案會變成master貼紙指到的commit狀態](https://i.imgur.com/K1nTCbo.png) 2. 合併cat分支 `git merge cat` - master與HEAD移動到cat貼紙的所在地 - 快轉(Fast-forward)合併 => 合併分支可以看成在移動貼紙(如下圖,`master`貼紙移動到與`cat`貼紙相同的commit) ![](https://i.imgur.com/b9tmAjn.png) ```bash tingdeAir:git-branch1 tingtinghsu$ git branch * cat master tingdeAir:git-branch1 tingtinghsu$ git checkout master Switched to branch 'master' tingdeAir:git-branch1 tingtinghsu$ git merge cat Updating 865f98a..8fded81 Fast-forward .keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 .keep tingdeAir:git-branch1 tingtinghsu$ git log --oneline 8fded81 add cat branch 865f98a add hello-world file e12d8ef add database.yml in config folder 85e7e30 add hello 657fce7 add container abb4f43 update index page cef6e40 create index page cc797cd init commit ``` ## 4. `git branch -d`刪掉已合併的分支 1. 把cat貼紙撕起來 2. Commit 或是檔案不會因此受到影響 => **Git 的本體是commit, 分支只是浮雲** ```bash tingdeAir:git-branch1 tingtinghsu$ git branch -d cat Deleted branch cat (was 8fded81). ``` # Examle 2 複雜一點的分支 y型merge ## 1. git branch `dog` 建立新分支 在目前HEAD所在地貼dog貼紙 ```bash tingdeAir:git-branch1 tingtinghsu$ git branch dog tingdeAir:git-branch1 tingtinghsu$ git branch dog * master rabbit tingdeAir:git-branch1 tingtinghsu$ ``` ## 2. git checkout `dog` 切換到dog分支 1. HEAD移動到dog分支 2. 專案會變成dog貼紙指到的Commit狀態 3. 目前dog跟master所在位置一樣 ```bash tingdeAir:git-branch1 tingtinghsu$ git checkout dog Switched to branch 'dog' tingdeAir:git-branch1 tingtinghsu$ git branch * dog master rabbit ``` ### 2-1 在`dog` branch送commit 再進行一次存檔 1. dog貼紙與HEAD往前移動 2. 其他分支貼紙留在原地 ![dog貼紙與HEAD往前移動](https://i.imgur.com/c0XA4yv.png) HEAD像是令牌,只有領到令牌的分支才可以往前移動 ```bash tingdeAir:git-branch1 tingtinghsu$ git commit -m "add dog 1" [dog e560a0e] add dog 1 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 .keepdog ``` ### 2-2 在`dog` branch再送一次commit ```bash tingdeAir:git-branch1 tingtinghsu$ git add . tingdeAir:git-branch1 tingtinghsu$ git commit -m "add dog 2" [dog 3f699df] add dog 2 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 .keeptwodogs ``` 第二個分支長太大,這時候的合併就沒辦法fast forward了 ![分支長太大的合併就沒辦法fast forward](https://i.imgur.com/63w6piS.png) ### 2-3. git merge `rabbit` : 在dog分支合併rabbit分支 rabbit分支也commit過一次,若此時dog分支想要合併rabbit分支: 1.長出一個合併的commit (dog和rabbit分支的公證人) 2.dog貼紙和HEAD往新的commit移動 ![合併分支時產生小耳朵](https://i.imgur.com/fGz2BDC.png) ```bash tingdeAir:git-branch1 tingtinghsu$ git checkout rabbit Switched to branch 'rabbit' tingdeAir:git-branch1 tingtinghsu$ ls config hello.html index.html welcome.html tingdeAir:git-branch1 tingtinghsu$ touch rabbit tingdeAir:git-branch1 tingtinghsu$ git add . tingdeAir:git-branch1 tingtinghsu$ git commit -m "add rabbit" [rabbit 6001538] add rabbit 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 rabbit tingdeAir:git-branch1 tingtinghsu$ git checkout dog Switched to branch 'dog' tingdeAir:git-branch1 tingtinghsu$ git merge rabbit Merge made by the 'recursive' strategy. rabbit | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 rabbit ``` *在Sourcetree打勾Checkout New Branch,代表建立分支之後立即切換過去該branch。 # Example 3 讓合併的節點消失掉`git rebase` `git rebase`: 變換根基/嫁接 注意: 在git的世界裡,commit沒有辦法刪除,但透過`git rebase`可以將歷史commit記錄變成直線。 現在有`dog`與`bird`分支 ![dog分支與bird分支](https://i.imgur.com/qxDoNGf.png) 在HEAD是dog, `git rebase bird `: dog踩在bird分支的頭上 1. 複製一份dog的commit接到bird的commit後面,形成新的commit 2. HEAD移動到新的commit 3. dog貼紙移動到新的commit, HEAD指向bird貼紙 4. bird貼紙從舊的分支commit脫離(如下圖:bird分支已消失,歷史紀錄變成直線) ![bird分支的舊commit脫離](https://i.imgur.com/cPwVtqr.png) ```bash tingdeAir:git-branch1 tingtinghsu$ git rebase bird First, rewinding head to replay your work on top of it... Applying: add dog3 ``` # merge和rebase的使用時機 rebase 優點: 拿來整理過多節點的時候,把節點拉成同條 缺點: 看不太出來歷史軌跡

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully