Git - 入门到熟悉_分支管理

Posted 小小工匠

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Git - 入门到熟悉_分支管理相关的知识,希望对你有一定的参考价值。

文章目录


Pre

Git - 入门到熟悉_Git基本概念与核心命令

接下来,我们聊一聊Git的分支管理

使用分支意味着可以从开发主线上分离开来,然后在不影响主线的同时继续工作。


常用命令

#查看当前分支
git branch [-avv]
#基于当前分支新建分支
git branch <branch name>
#基于提交新建分支
git branch <branch name> <commit id>
$ git branch -d dev
#切换分支
git checkout <branch name>
#合并分支
git merge <merge target>
#解决冲突,如果因冲突导致自动合并失败,此时 status 为mergeing 状态.
#需要手动修改后重新提交(commit) 


为什么需要分支


查看分支信息

大家先拆下,你本地的工程有几个分支 ?????


artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ pwd  
/d/opt/gitdemo/gitProject

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git branch  # 查看本地分支 
* master

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git branch -avv    # 查看所有分支 包含远程分支 ,可以看到 版本号一致 
* master                5e6d25b [origin/master] 第二次提交
  remotes/origin/master 5e6d25b 第二次提交

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ echo '第三次提交' >> README.MF  # 修改本地文件

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git branch -avv   # 查看所有分支 包含远程分支 ,因为修改未提交,可以看到 版本号还是一致 
* master                5e6d25b [origin/master] 第二次提交
  remotes/origin/master 5e6d25b 第二次提交

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   README.MF

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

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git commit -am '第三次提交'   # 提交本地修改 到  local repository 
warning: LF will be replaced by CRLF in README.MF.
The file will have its original line endings in your working directory
[master 2d23d8a] 绗笁娆℃彁浜? 1 file changed, 1 insertion(+)

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git branch -avv  # 查看所有分支 包含远程分支 本地已修改提交到本地仓库,所以和远端的仓库版本号出现了不一致 
* master                2d23d8a [origin/master: ahead 1] 第三次提交
  remotes/origin/master 5e6d25b 第二次提交

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$


artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git checkout  remotes/origin/master   # 切换到远程分支  
Note: switching to 'remotes/origin/master'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 5e6d25b 绗簩娆℃彁浜?
artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject ((5e6d25b...))
$ cat README.MF   # 查看远程分支的文件内容  ,可以看到和本地不一致 
artisan git demo
new modify content

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject ((5e6d25b...))
$

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject ((5e6d25b...))
$ git checkout  master  #  切换到本地master分支  
Previous HEAD position was 5e6d25b 绗簩娆℃彁浜?Switched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ cat README.MF  # 查看本地master分支的文件内容  ,可以看到和远端不一致 
artisan git demo
new modify content
第三次提交

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$



artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git push  # 提交到Git Remote 
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 319 bytes | 319.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To https://gitee.com/yangshangwei/gitProject.git
   5e6d25b..2d23d8a  master -> master

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git branch -avv  # 再此查看所有分支,发现经过push后又一致了。  
* master                2d23d8a [origin/master] 第三次提交
  remotes/origin/master 2d23d8a 第三次提交

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$
 


总结一下: -


artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ echo 'four ' >> README.MF   # 修改本地文件

 

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git commit -am '第四次提交'   # 提交到本地 local repository 
warning: LF will be replaced by CRLF in README.MF.
The file will have its original line endings in your working directory
[master 9f302ff] 绗洓娆℃彁浜? 1 file changed, 1 insertion(+)

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git branch  # 查看当前分支
* master

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git branch -av   # 查看所有分支 
* master                9f302ff [ahead 1] 第四次提交
  remotes/origin/master 2d23d8a 第三次提交

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git branch -avv   # 查看所有分支 详细信息    关联信息  master 关联origin/master
* master                9f302ff [origin/master: ahead 1] 第四次提交
  remotes/origin/master 2d23d8a 第三次提交

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$


创建分支

  1. 基于当前分支,创建一个新分支
  2. 基于远程分支,创建一个新分支
  3. 基于一个提交,创建一个新分支
  4. 基于Tag ,创建一个新分支

本质上所有的新分支的常见都是 基于提交点来创建的。

基于当前分支,创建一个新分支

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git  branch  
* master

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git branch dev master  # 根据master分支 创建dev 分支  

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git branch -av  # 查看所有分支 
  dev                   9f302ff 第四次提交
* master                9f302ff 第四次提交
  remotes/origin/master 9f302ff 第四次提交

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git branch
  dev
* master

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git branch -a
  dev
* master
  remotes/origin/master

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git branch -avv
  dev                   9f302ff 第四次提交
* master                9f302ff [origin/master] 第四次提交
  remotes/origin/master 9f302ff 第四次提交

 


基于远程分支,创建一个新分支

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git branch baseRemote remotes/origin/master   # 基于远程分支 remotes/origin/maste 创建一个分支 baseRemote 

Branch 'baseRemote' set up to track remote branch 'master' from 'origin'.

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git branch
  baseRemote
  dev
* master

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$



基于一个提交,创建一个新分支

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git log
commit 9f302ffcd77e703b02e9d185d085484f1e298c0a (HEAD -> master, origin/master, dev, baseRemote)
Author: yangshangwei <yswcomeon@gmail.com>
Date:   Mon Aug 15 00:34:03 2022 +0800

    第四次提交

commit 2d23d8a4a8b9f79f893ce7ef594624929b57d31c
Author: yangshangwei <yswcomeon@gmail.com>
Date:   Mon Aug 15 00:24:25 2022 +0800

    第三次提交

commit 5e6d25b9e0f1bae23ce98facecf79ad4e97df174
Author: yangshangwei <yswcomeon@gmail.com>
Date:   Sun Aug 14 23:55:34 2022 +0800

    第二次提交

commit 4ac59bd9186592662350d23f1cdfbe5c4f21f329
Author: yangshangwei <yswcomeon@gmail.com>
Date:   Sun Aug 14 23:02:58 2022 +0800

    init commit

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git branch baseOnCommit 4ac59bd9186592662350d23f1cdfbe5c4f21f329  # 基于第一次提交创建一个分支 baseOnCommit 

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git branch -av
  baseOnCommit          4ac59bd init commit
  baseRemote            9f302ff 第四次提交
  dev                   9f302ff 第四次提交
* master                9f302ff 第四次提交
  remotes/origin/master 9f302ff 第四次提交

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)

基于Tag ,创建一个新分支


删除分支 git branch -d


artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git branch
  baseOnCommit
  baseRemote
  dev
* master

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git branch -d baseOnCommit  # 删除1个 
Deleted branch baseOnCommit (was 4ac59bd).

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git branch
  baseRemote
  dev
* master

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git branch -d baseRemote   dev  # 删除多个 
Deleted branch baseRemote (was 9f302ff).
Deleted branch dev (was 9f302ff).

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git branch
* master

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$

切换分支 git checkout


artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git branch dev master

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git checkout dev
Switched to branch 'dev'

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (dev)
$ git branch
* dev
  master

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (dev)
$

注意看前面的 * 号

以上是关于Git - 入门到熟悉_分支管理的主要内容,如果未能解决你的问题,请参考以下文章

Git - 入门到熟悉_TAG管理

Git - 入门到熟悉_TAG管理

Git - 入门到熟悉_日志管理

Git - 入门到熟悉_日志管理

Git - 入门到熟悉_远程仓库管理

Git - 入门到熟悉_远程仓库管理