Git分支詳解

Git分支詳解

git branch命令

  • 查看分支 git branch
  • 新建分支 git branch branch_name
  • 切換分支 git checkout branch
  • 刪除分支 git branch -d branch_name
    (注意不能刪除當(dāng)前所在的分支)
  • 更改分支名稱 git branch -m old_name new_name
  • 創(chuàng)建分支并切換到新創(chuàng)建的分支 git branch -b branch_name
  • 合并分支 git merge branch_name
  • 查看工作區(qū)與暫存區(qū)差異 git diff
  • 查看提交歷史 git log
  • 封存當(dāng)前暫存區(qū)修改 git stash
  • 查看當(dāng)前保存內(nèi)容 git stash list
  • 提取暫存區(qū) git stash apply stash@{0}

git分支初步使用

  1. 首先查看當(dāng)前目錄下的git分支

    $ git branch
      admin
    * master
    

    當(dāng)前目錄有兩個(gè)分支,分別是master與admin分支,并且目前我們處于master分支上。
    當(dāng)前的項(xiàng)目文件的內(nèi)容相同,因?yàn)槲覀儧](méi)有做修改。

  2. 對(duì)文件進(jìn)行修改并合并
    在master分支下的文件里添加注釋
    #this is master branch
    在admin分支下的文件中添加注釋
    #this is admin branch
    使用git commit -am 'admin branch'提交更改
    回到master分支,使用git merge admin命令合并分支

    $ git merge admin
    Auto-merging dPro/urls.py
    CONFLICT (content): Merge conflict in dPro/urls.py
    Automatic merge failed; fix conflicts and then commit the result.
    

    可以看到,git提示我們合并失敗,因?yàn)樵谖募?code>dPro/url.py內(nèi)有沖突,他要求我們手動(dòng)合并并重新提交
    dPro/url.py內(nèi)顯示了沖突的內(nèi)容

    <<<<<<< HEAD
    #這是master分支
    =======
    #this is admin branch
    >>>>>>> admin
    

    我們對(duì)文件進(jìn)行修改,將admin分支的內(nèi)容合并到master分支上,修改結(jié)果如下

    #this is admin branch
    

    git提示我們合并成功

  3. 查看版本差異
    首先使用git log查看版本歷史

    commit f387cc59552544a031cbd424f3d516a2781ecbab (HEAD -> master)
    Merge: 167823b 8e554e9
    Author: Qian <2358979326@qq.com>
    Date:   Sun Jun 2 12:36:38 2019 +0800
    
        mergeadmin fix conflict 
    
    commit 8e554e9a334d31b5acce076d465fb398f6d9ac6c (admin)
    Author: Qian <2358979326@qq.com>
    Date:   Sun Jun 2 12:19:52 2019 +0800
    
        admin branch
    
    commit 167823b479d9c932414534bbaa6610674807f907
    Author: Qian <2358979326@qq.com>
    Date:   Sat Jun 1 23:39:58 2019 +0800
    
        test
    
    

    我們選擇兩個(gè)版本比較差異,使用git diff 版本號(hào)1 版本號(hào)2,一般選擇版本號(hào)前8位(git使用前7位)。

    $ git diff f387cc595 167823b479d
    diff --git a/dPro/urls.py b/dPro/urls.py
    index f12a399..7ca8aa1 100644
    --- a/dPro/urls.py
    +++ b/dPro/urls.py
    @@ -24,5 +24,4 @@ urlpatterns = [
        url(r'login/',views.login),
        url(r'index/',views.index),
    ]
    -#this is admin branch
    -
    +#這是master分支
    
  4. 封存暫存區(qū)
    我們對(duì)文件進(jìn)行修改,添加#對(duì)暫存區(qū)的修改,并保存
    接著我們切換到admin分支,使用git checkout admin命令,出現(xiàn)以下錯(cuò)誤

    error: Your local changes to the following files would be overwritten by checkout:
            dPro/urls.py
    Please commit your changes or stash them before you switch branches.
    Aborting
    

    git提示我們提交我們的修改,或者是在切換分支之前satsh他們。
    我們使用git stash命令暫存。
    此時(shí)我們的工作區(qū)已經(jīng)變成了修改之前的狀態(tài),即之前做的修改已經(jīng)撤銷掉了,可以安全的切換分支了。

  5. 提取暫存區(qū)
    當(dāng)我們將修改暫存并切換分支后,重新回到之前暫存內(nèi)容的分支時(shí),如何提取之前stash的內(nèi)容?
    首先使用git stash list查看當(dāng)前保存的內(nèi)容

    $ git stash list
    stash@{0}: WIP on master: f387cc5 mergeadmin fix conflict
    stash@{1}: WIP on master: f387cc5 mergeadmin fix conflict
    

    使用命令git stash apply stash@{0},打開(kāi)編輯器,可以看到之前做的修改已經(jīng)提取出來(lái)了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容