git branch分支管理

                                              作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.git分支的查看与创建

1.查看分支和当前所在分支

[root@prometheus-server31 devops]# git branch
* master
[root@prometheus-server31 devops]# 

2.创建分支

[root@prometheus-server31 devops]# git branch k8s
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# git branch
  k8s
* master
[root@prometheus-server31 devops]# 

二.切换分支

1.切换到已经存在的分支

[root@prometheus-server31 devops]# git branch
  k8s
* master
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# git checkout k8s
Switched to branch 'k8s'
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# git branch
* k8s
  master
[root@prometheus-server31 devops]# 

2.如果分支不存在,可以创建分支

[root@prometheus-server31 devops]# git checkout -b docker
Switched to a new branch 'docker'
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# git branch
* docker
  k8s
  master
[root@prometheus-server31 devops]# 

三.主分支合并其他分支

1.docker分支创建文件

[root@prometheus-server31 devops]# ll
total 16
drwxr-xr-x 3 root root 4096 Nov 17 20:16 ./
drwxr-xr-x 7 root root 4096 Nov 17 19:43 ../
drwxr-xr-x 8 root root 4096 Nov 17 22:05 .git/
-rw-r--r-- 1 root root   23 Nov 17 19:59 go.mod
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# git branch
* docker
  k8s
  master
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# echo docs.docker.com > office.txt
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# ll
total 20
drwxr-xr-x 3 root root 4096 Nov 17 22:07 ./
drwxr-xr-x 7 root root 4096 Nov 17 19:43 ../
drwxr-xr-x 8 root root 4096 Nov 17 22:05 .git/
-rw-r--r-- 1 root root   23 Nov 17 19:59 go.mod
-rw-r--r-- 1 root root   16 Nov 17 22:07 office.txt
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# git add office.txt ; git commit -m 'add docker info'
[docker 5b1e29f] add docker info
 1 file changed, 1 insertion(+)
 create mode 100644 office.txt
[root@prometheus-server31 devops]# 

2.k8s分支创建文件

[root@prometheus-server31 devops]# git branch
* docker
  k8s
  master
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# git checkout k8s
Switched to branch 'k8s'
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# git branch
  docker
* k8s
  master
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# ll
total 16
drwxr-xr-x 3 root root 4096 Nov 17 22:08 ./
drwxr-xr-x 7 root root 4096 Nov 17 19:43 ../
drwxr-xr-x 8 root root 4096 Nov 17 22:08 .git/
-rw-r--r-- 1 root root   23 Nov 17 19:59 go.mod
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# echo https://www.kubernetes.io > k8s.log
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# git add k8s.log ; git commit -m 'add k8s info'
[k8s 828e023] add k8s info
 1 file changed, 1 insertion(+)
 create mode 100644 k8s.log
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# ll
total 20
drwxr-xr-x 3 root root 4096 Nov 17 22:09 ./
drwxr-xr-x 7 root root 4096 Nov 17 19:43 ../
drwxr-xr-x 8 root root 4096 Nov 17 22:09 .git/
-rw-r--r-- 1 root root   23 Nov 17 19:59 go.mod
-rw-r--r-- 1 root root   26 Nov 17 22:09 k8s.log
[root@prometheus-server31 devops]# 

3.合并分支

[root@prometheus-server31 devops]# git branch
  docker
* k8s
  master
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# git checkout master   # 先切换到master分支
Switched to branch 'master'
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# git branch 
  docker
  k8s
* master
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# ll
total 16
drwxr-xr-x 3 root root 4096 Nov 17 22:10 ./
drwxr-xr-x 7 root root 4096 Nov 17 19:43 ../
drwxr-xr-x 8 root root 4096 Nov 17 22:10 .git/
-rw-r--r-- 1 root root   23 Nov 17 19:59 go.mod
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# git merge k8s  # 先合并K8S分支
Updating 005b95d..828e023
Fast-forward
 k8s.log | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 k8s.log
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# ll
total 20
drwxr-xr-x 3 root root 4096 Nov 17 22:11 ./
drwxr-xr-x 7 root root 4096 Nov 17 19:43 ../
drwxr-xr-x 8 root root 4096 Nov 17 22:11 .git/
-rw-r--r-- 1 root root   23 Nov 17 19:59 go.mod
-rw-r--r-- 1 root root   26 Nov 17 22:11 k8s.log  # 不难发现,该分支的数据已经到主分支了。
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# git merge docker  # 接下来合并docker分支
Merge made by the 'ort' strategy.
 office.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 office.txt
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# ll
total 24
drwxr-xr-x 3 root root 4096 Nov 17 22:11 ./
drwxr-xr-x 7 root root 4096 Nov 17 19:43 ../
drwxr-xr-x 8 root root 4096 Nov 17 22:12 .git/
-rw-r--r-- 1 root root   23 Nov 17 19:59 go.mod
-rw-r--r-- 1 root root   26 Nov 17 22:11 k8s.log
-rw-r--r-- 1 root root   16 Nov 17 22:11 office.txt  # 不难发现,docker分支的内容也来到主分支啦~
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# cat k8s.log 
https://www.kubernetes.io
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# cat office.txt 
docs.docker.com
[root@prometheus-server31 devops]# 

四.删除分支

[root@prometheus-server31 devops]# git branch
  docker
  k8s
* master
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# git branch -d k8s  #  删除k8s分支
Deleted branch k8s (was 828e023).
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# git branch
  docker
* master
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# git branch -d docker  # 删除docker分支
Deleted branch docker (was 5b1e29f).
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# git branch
* master
[root@prometheus-server31 devops]# 
[root@prometheus-server31 devops]# ll
total 24
drwxr-xr-x 3 root root 4096 Nov 17 22:11 ./
drwxr-xr-x 7 root root 4096 Nov 17 19:43 ../
drwxr-xr-x 8 root root 4096 Nov 17 22:17 .git/
-rw-r--r-- 1 root root   23 Nov 17 19:59 go.mod
-rw-r--r-- 1 root root   26 Nov 17 22:11 k8s.log
-rw-r--r-- 1 root root   16 Nov 17 22:11 office.txt
[root@prometheus-server31 devops]# 
posted @ 2021-01-09 22:22  尹正杰  阅读(268)  评论(0编辑  收藏  举报