Git開發實戰(五)之分支操做

1、分支的概念git

      1.圖解分支概念github

     

      經過上圖,咱們能夠看到每一條線能夠看做是一個分支,每個點能夠看做一次提交commit,上面的那條線就是主分支Master,下面分出另一條分支,最後又和主分支合併了!vim

2、分支的經常使用操做bash

      1.查看當前項目中的全部分支git branchfetch

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git branch
* master

      2.建立一個新的分支git branch 新分支名,能夠看到咱們成功建立了一個新的分支20171027-001url

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git branch 20171027-001

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git branch
  20171027-001
* master

      3.切換到新分支20171027-001git checkout 待切換到的分支名,咱們分別查看master分支和這個新分支的提交日誌,能夠看到都是同樣的;spa

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git checkout 20171027-001
Switched to branch '20171027-001'

      4.咱們在新的分支上,修改README文件,而後提交,而後咱們查看提交日誌,咱們能夠發現,主分支master上是沒有這個提交記錄的,也就是說咱們在新分支的修改,對master分支是沒有影響的;日誌

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (20171027-001)
$ vim README

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (20171027-001)
$ git status
On branch 20171027-001
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   README

no changes added to commit (use "git add" and/or "git commit -a")

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (20171027-001)
$ git add README
warning: LF will be replaced by CRLF in README.
The file will have its original line endings in your working directory.

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (20171027-001)
$ git commit -m '我在新分支上修改了README文件'
[20171027-001 d08ae20] 我在新分支上修改了README文件
 1 file changed, 1 insertion(+)

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (20171027-001)
$ git log
commit d08ae208a74a1f27e0833cb4bd206b7956e83846
Author: kevinShaw <aibinxiao@126.com>
Date:   Fri Oct 27 09:14:27 2017 +0800

    我在新分支上修改了README文件

commit 04c9dd8185c8e681ab03c67cc7e698a147204ab7
Author: kevinShaw <aibinxiao@126.com>
Date:   Thu Oct 26 10:08:25 2017 +0800

    忽略.yml的配置文件

commit d4d2af303b2f512b64336a8ed6568a64a48cf9cf
Author: kevinShaw <aibinxiao@126.com>
Date:   Thu Oct 26 09:48:40 2017 +0800

    刪除了message文件

commit 335ba6ca01329e27168b1f942b3bc2624e449691
Author: kevinShaw <aibinxiao@126.com>
Date:   Thu Oct 26 09:23:30 2017 +0800

    提交message

--------------------------------------------------------------

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (20171027-001)
$ git checkout master
Switched to branch 'master'

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git log
commit 04c9dd8185c8e681ab03c67cc7e698a147204ab7
Author: kevinShaw <aibinxiao@126.com>
Date:   Thu Oct 26 10:08:25 2017 +0800

    忽略.yml的配置文件

commit d4d2af303b2f512b64336a8ed6568a64a48cf9cf
Author: kevinShaw <aibinxiao@126.com>
Date:   Thu Oct 26 09:48:40 2017 +0800

    刪除了message文件

commit 335ba6ca01329e27168b1f942b3bc2624e449691
Author: kevinShaw <aibinxiao@126.com>
Date:   Thu Oct 26 09:23:30 2017 +0800

    提交message

commit 9cfc652c8d449fc6e5af9d1d5873a8018b457272
Author: kevinShaw <aibinxiao@126.com>
Date:   Mon Oct 23 16:48:41 2017 +0800

    刪除log

      5.分支的刪除,咱們在主分支上再次建立一個分支20171027-002,首先咱們先切換到主分支上,在建立一個新的分支,咱們經過查看刪除先後的分支,知道咱們使用git branch -d 待刪除的分支名 命令能夠刪除指定的分支;code

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git checkout master
Already on 'master'

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git branch 20171027-002

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git branch
  20171027-001
  20171027-002
* master

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git branch -d 20171027-002
Deleted branch 20171027-002 (was 04c9dd8).

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git branch
  20171027-001
* master

3、分支的高級操做orm

      1.本地代碼的分支合併,接着以前,咱們在新分支20171027-001上修改了README文件,可是主分支上沒有這個修改提交,那咱們要讓主分支也有這個提交,咱們要怎麼辦呢?咱們能夠將新分支20171027-001分支合併到主分支上,首先咱們要先切換到master分支上,而後咱們使用 git merge 待合併的分支名,最後查看提交日誌,咱們就看到新分支的的提交在主分支上也有了,而後提交的那個產生的隨機數也是同樣的;

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git checkout master
Already on 'master'

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git merge 20171027-001
Updating 04c9dd8..d08ae20
Fast-forward
 README | 1 +
 1 file changed, 1 insertion(+)


aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git log
commit d08ae208a74a1f27e0833cb4bd206b7956e83846
Author: kevinShaw <aibinxiao@126.com>
Date:   Fri Oct 27 09:14:27 2017 +0800

    我在新分支上修改了README文件

      2.建立遠程倉庫,而後將代碼推送到遠程倉庫中,好比咱們將這個test_git目錄推送到遠程倉庫中;以github爲例,其餘代碼託管平臺操做也是大同小異(碼雲,碼市);

      (1)建立遠程倉庫

     

      (2)將本地倉庫的代碼推送到遠程倉庫

     

      本地推送:

  • 爲本地倉庫綁定一個遠程倉庫地址,git remote add origin 遠程倉庫地址
  • 將本地倉庫代碼推送到遠程倉庫中git push -u origin master,(其實完整命令應該是git push origin master:master 通常使用前者)這個操做master表示我要將本地倉庫的master分支的代碼上傳推送到遠程倉庫中的master分支中,origin表示github或者你所託管的遠程倉庫平臺,能夠認爲origin是github的一個別名,有這個別名就不須要每次push使用那個很長的地址了;
  • git容許咱們同一個本地代碼倉庫建立多個遠程倉庫,但通常狀況下使用一個就夠了
aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git remote add origin https://github.com/xiaoaibin/test_git.git

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git push -u origin master
Counting objects: 18, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (15/15), done.
Writing objects: 100% (18/18), 1.86 KiB | 0 bytes/s, done.
Total 18 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), done.
To https://github.com/xiaoaibin/test_git.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

      查看配置:

  • 咱們能夠看到裏面配置了遠程倉庫,能夠看到[remote "origin"]中origin其實就是github遠程倉庫的別名;
  • 咱們還能夠再綁定一個碼雲遠程倉庫,好比說[remote  "backup"],咱們把別名設置爲backup
[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
        ignorecase = true
[user]
        name = kevinShaw
        email = xxx@xxx.com
[remote "origin"]
        url = https://github.com/xiaoaibin/test_git.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[remote "origin"]
        url = https://gitee.com/aibinxiao/test_git.git
        fetch = +refs/heads/*:refs/remotes/backup/*
[branch "master"]
        remote = origin
        merge = refs/heads/master

      (3)查看遠程倉庫,咱們能夠看到,咱們本地的代碼都推送到了遠程倉庫中,而後本地的全部提交也都能看到;

     

     

      3.第一次從遠程倉庫獲取代碼,使用git clone 遠程倉庫地址,好比 git clone https://github.com/xiaoaibin/test_git.git

 

本文爲原創文章,若是對你有一點點的幫助,別忘了點贊哦!比心!如需轉載,請註明出處,謝謝!

相關文章
相關標籤/搜索