Git开发实战(五)之分支操作

一、分支的概念

      1.图解分支概念

     

      通过上图,我们可以看到每一条线可以看作是一个分支,每一个点可以看作一次提交commit,上面的那条线就是主分支Master,下面分出另外一条分支,最后又和主分支合并了!

二、分支的常用操作

      1.查看当前项目中的所有分支git branch

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

      2.创建一个新的分支git branch 新分支名,可以看到我们成功创建了一个新的分支20171027-001

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分支和这个新分支的提交日志,可以看到都是一样的;

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 待删除的分支名 命令可以删除指定的分支;

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

三、分支的高级操作

      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

 

本文为原创文章,如果对你有一点点的帮助,别忘了点赞哦!比心!如需转载,请注明出处,谢谢!

 

转载于:https://my.oschina.net/aibinxiao/blog/1557024

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值