9. 远程分支与本地分支管理

前言

该文章只是记录了一些自己的见解,可能并不准确,只是为了学习时的一些记录,不喜勿喷,谢谢

本篇文章,主要介绍下三种分支,以及如何关联三种分支,以及分支的本质

1. 三种分支

git中分支分为三种,如下:

  1. 本地分支
  2. 本地的远程分支(也叫追踪分支)
  3. 远程分支

1.1 本地分支

各个用户在各自的工作空间中创建的分支叫做本地分支,各个用户的本地分支可以互不相同,各自处理,互补影响。

1.2 远程分支

这里的远程分支指的是远程服务器,例如github中的某个仓库的分支

1.3 本地的远程分支(追踪分支)

本地的远程分支是用于远程分支关联本地分支的桥梁,方便在远程服务器断网时,本地保留某个时刻的最新的远程版本库,用于异常时回滚,该分支原则上不能手动操作,是由git操作。

2. 本地分支操作命令

在之前的文章也介绍过如果创建本地分支,这里稍微在介绍下

2.1 创建本地分支

如果我们是刚初始化的本地仓库,此时是没有分支的,随后使用git commit 命令后,就会生成一个默认的master分支,如下图:

[root@huangzb git4]# git init
Initialized empty Git repository in /root/mygit/git4/.git/
[root@huangzb git4]#
[root@huangzb git4]# git branch
[root@huangzb git4]#

现在,我们随便写点东西,再看看

[root@huangzb git4]# echo 'hello 良超' > helloc.txt
[root@huangzb git4]#
[root@huangzb git4]# git add .
[root@huangzb git4]# git commit -m 'create helloc.txt'
[master (root-commit) 3411685] create helloc.txt
1 file changed, 1 insertion(+)
create mode 100644 helloc.txt
[root@huangzb git4]#
[root@huangzb git4]# git branch
* master
[root@huangzb git4]#

可以看到,git为我们自动生成了 master分支

如果我们想自己创建分支,可以使用命令 git branch 分支名,操作如下:

[root@huangzb git4]# git branch dev
[root@huangzb git4]#
[root@huangzb git4]# git branch
  dev
* master
[root@huangzb git4]#

2.2 重命名本地分支

使用命令 git branch -m 原分支名 新分支名,操作如下:

[root@huangzb git4]# git branch -m dev developer
[root@huangzb git4]# git branch
  developer
* master
[root@huangzb git4]#

2.3 删除本地分支

如果某个时刻,某个分支不需要了,可以使用命令 git branch -d 分支命,操作如下

[root@huangzb git4]# git branch -d developer
Deleted branch developer (was 3411685).
[root@huangzb git4]#
[root@huangzb git4]# git branch
* master
[root@huangzb git4]#

2.4 查看某个分支

我们想看某个分支最近的提交信息,可以使用命令 git show 分支名 ,操作如下:

[root@huangzb git4]# git show master
commit 3411685270a4f2a592732e6c4422132e29686946 (HEAD -> master)
Author: CC <CC@CC.com>
Date:   Fri Apr 10 15:59:53 2020 +0800
 
    create helloc.txt
 
diff --git a/helloc.txt b/helloc.txt
new file mode 100644
index 0000000..55eb968
--- /dev/null
+++ b/helloc.txt
@@ -0,0 +1 @@
+hello 良超
[root@huangzb git4]#
 

3. 远程分支操作命令

远程分支操作分为两种

  1. 在github上创建远程分支
  2. 将本地的分支推送到远程,就可以创建远程分支

这里只介绍使用命令,将本地的分支推送到远程

3.1 创建远程分支

前提是本地仓库需要添加远程地址

这里,我们使用 git clone 拉取远程版本库,然后将本地分支推送到远程

创建远程分支的方式有几种命令:

  1. git push origin 本地分支:远程分支
  2. git push origin -u 本地分支名
  3. git push origin --set-upstream 本地分支名

3.1.1 git push origin 本地分支名:待创建的远程分支名

完整命令:git push origin 本地分支名:待创建的远程分支名

,操作如下:

[root@huangzb study-git]# git branch -av
* master                8d09f82 update heloc.txt
  remotes/origin/HEAD   -> origin/master
  remotes/origin/master 8d09f82 update heloc.txt
[root@huangzb study-git]#
[root@huangzb study-git]# git branch dev
[root@huangzb study-git]#
[root@huangzb study-git]# git push origin dev:dev
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'dev' on GitHub by visiting:
remote:      https://github.com/duguxiaobiao/study-git/pull/new/dev
remote:
To https://github.com/duguxiaobiao/study-git.git
 * [new branch]      dev -> dev
[root@huangzb study-git]#

上图的操作就是:在本地新建了分支dev,推送到远程的 dev分支

3.1.2 git push origin -u 本地分支名

这种方式则是将本地的指定分支推送到远程分支,通过 -u参数关联,默认是使用规范,远程如果没有跟本地分支同名的分支,则会创建要给跟本地分支名同名的远程分支

操作如下:

[root@huangzb study-git]# git branch -av
  dev                   8d09f82 update heloc.txt
* master                8d09f82 update heloc.txt
  remotes/origin/HEAD   -> origin/master
  remotes/origin/dev    8d09f82 update heloc.txt
  remotes/origin/master 8d09f82 update heloc.txt
[root@huangzb study-git]#
[root@huangzb study-git]# git branch test
[root@huangzb study-git]#
[root@huangzb study-git]# git push origin -u test
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'test' on GitHub by visiting:
remote:      https://github.com/duguxiaobiao/study-git/pull/new/test
remote:
To https://github.com/duguxiaobiao/study-git.git
 * [new branch]      test -> test
Branch 'test' set up to track remote branch 'test' from 'origin'.
[root@huangzb study-git]#
[root@huangzb study-git]# git branch -av
  dev                   8d09f82 update heloc.txt
* master                8d09f82 update heloc.txt
  test                  8d09f82 update heloc.txt
  remotes/origin/HEAD   -> origin/master
  remotes/origin/dev    8d09f82 update heloc.txt
  remotes/origin/master 8d09f82 update heloc.txt
  remotes/origin/test   8d09f82 update heloc.txt
[root@huangzb study-git]#

此时去github中,可以看到已经创建了一个test分支

3.1.3 git push origin --set-upstream 本地分支名

该操作跟 3.1.2 操作等同,意义差不多,稍微演示下

操作如下:

[root@huangzb study-git]# git branch -av
  dev                   8d09f82 update heloc.txt
* master                8d09f82 update heloc.txt
  test                  8d09f82 update heloc.txt
  remotes/origin/HEAD   -> origin/master
  remotes/origin/dev    8d09f82 update heloc.txt
  remotes/origin/master 8d09f82 update heloc.txt
  remotes/origin/test   8d09f82 update heloc.txt
[root@huangzb study-git]#
[root@huangzb study-git]# git branch test2
[root@huangzb study-git]# git push origin --set-upstream test2
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'test2' on GitHub by visiting:
remote:      https://github.com/duguxiaobiao/study-git/pull/new/test2
remote:
To https://github.com/duguxiaobiao/study-git.git
 * [new branch]      test2 -> test2
Branch 'test2' set up to track remote branch 'test2' from 'origin'.
[root@huangzb study-git]# git branch -av
  dev                   8d09f82 update heloc.txt
* master                8d09f82 update heloc.txt
  test                  8d09f82 update heloc.txt
  test2                 8d09f82 update heloc.txt
  remotes/origin/HEAD   -> origin/master
  remotes/origin/dev    8d09f82 update heloc.txt
  remotes/origin/master 8d09f82 update heloc.txt
  remotes/origin/test   8d09f82 update heloc.txt
  remotes/origin/test2  8d09f82 update heloc.txt
[root@huangzb study-git]#

3.2 删除远程分支

删除远程分支有三种方法:

  1. 在github上通过按钮删除
  2. 使用命令 git push origin :待删除的远程分支名
  3. 使用命令 git push origin --delete 待删除的远程分支名

这里就不介绍第一种了

3.2.1 git push origin : 待删除的远程分支名

这种删除方式,属于投机取巧的方式,将一个 空 对应这远程分支,即变相的删除了远程分支,操作如下:

[root@huangzb study-git]# git branch -av
  dev                   8d09f82 update heloc.txt
* master                8d09f82 update heloc.txt
  remotes/origin/HEAD   -> origin/master
  remotes/origin/dev    8d09f82 update heloc.txt
  remotes/origin/master 8d09f82 update heloc.txt
[root@huangzb study-git]#
[root@huangzb study-git]# git push origin :dev
To https://github.com/duguxiaobiao/study-git.git
 - [deleted]         dev
[root@huangzb study-git]#

3.2.2 git push origin --delete 待删除的远程分支名

这种相比上一种来说更加规范,通过指定参数 --delete 来表明是一个删除操作,因为刚才把dev分支都删除了,现在先加上再删除,操作如下:

[root@huangzb study-git]# git branch -av
  dev                   8d09f82 update heloc.txt
* master                8d09f82 update heloc.txt
  remotes/origin/HEAD   -> origin/master
  remotes/origin/master 8d09f82 update heloc.txt
[root@huangzb study-git]#
[root@huangzb study-git]# git push origin dev:dev
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'dev' on GitHub by visiting:
remote:      https://github.com/duguxiaobiao/study-git/pull/new/dev
remote:
To https://github.com/duguxiaobiao/study-git.git
 * [new branch]      dev -> dev
[root@huangzb study-git]#
[root@huangzb study-git]# git push origin --delete dev
To https://github.com/duguxiaobiao/study-git.git
 - [deleted]         dev
[root@huangzb study-git]# git branch -av
  dev                   8d09f82 update heloc.txt
* master                8d09f82 update heloc.txt
  remotes/origin/HEAD   -> origin/master
  remotes/origin/master 8d09f82 update heloc.txt
[root@huangzb study-git]#
 

再上图中可以看到,我们将远程的dev分支删除掉了,但是在本地中依然存在dev分支,如果此时本地不需要dev分支了,可以使用 git branch -d dev 命令来删除掉。

3.3 将远程分支拉取到本地

之前介绍的是将本地的分支推送到远程,现在是假设有另一个用户,他想获取远程分支就可以使用下面的操作

将远程分支拉取到本地会分为两个步骤

  1. 拉取远程分支到本地,即生成了 本地的远程分支(追踪分支)
  2. 将本地分支与本地的远程分支(追踪分支)进行关联

3.3.1 远程---> 本地远程分支(追踪分支)

现在我们切换到另外一个用户来操作

使用命令 git pull 来拉取最新代码,而且还有远程分支信息,操作如下:

[root@huangzb git3]# git pull
From https://github.com/duguxiaobiao/study-git
 * [new branch]      dev        -> origin/dev
 * [new branch]      test       -> origin/test
 * [new branch]      test2      -> origin/test2
Already up to date.
[root@huangzb git3]#
[root@huangzb git3]# git branch -av
* master                8d09f82 update heloc.txt
  remotes/origin/HEAD   -> origin/master
  remotes/origin/dev    8d09f82 update heloc.txt
  remotes/origin/master 8d09f82 update heloc.txt
  remotes/origin/test   8d09f82 update heloc.txt
  remotes/origin/test2  8d09f82 update heloc.txt
[root@huangzb git3]#

由上图我们可以看到,使用git pull 命令将远程分支拉取到本地后,在本地创建了本地的远程分支(追踪分支),

但是我们可以看到,本地并没有生成 类似 dev,test等本地分支,此时我们自己创建并且关联。

3.3.2 本地分支关联---------> 本地远程分支(追踪分支)

本地分支关联本地的远程分支有多种方式,如下:

  1. 使用命令 git checkout -b 新分支名 本地的远程分支名
  2. 使用命令 git branch 新分支名 --track 本地的远程分支名
3.3.2.1 git checkout -b 待新建的分支名 本地的远程分支名

假设目前已经将远程的dev分支pull到了本地,现在需要创建一个dev本地分支,且与之关联,可以使用该命令,操作如下:

[root@huangzb git3]# git checkout -b dev origin/dev
Branch 'dev' set up to track remote branch 'dev' from 'origin'.
Switched to a new branch 'dev'
[root@huangzb git3]# git branch -av
* dev                   8d09f82 update heloc.txt
  master                8d09f82 update heloc.txt
  remotes/origin/HEAD   -> origin/master
  remotes/origin/dev    8d09f82 update heloc.txt
  remotes/origin/master 8d09f82 update heloc.txt
  remotes/origin/test   8d09f82 update heloc.txt
  remotes/origin/test2  8d09f82 update heloc.txt
3.3.2.2 git branch 待新建的分支命 --track 本地的远程分支名

可以使用 --track 参数,在新建一个分支的同时关联一个本地的远程分支,当然这里也可以使用 git checkout -b 待新建的分支名 --track 本地的远程分支名,两者的区别就多了一步切换分支的操作。操作如下:

[root@huangzb git3]# git branch test --track origin/test
Branch 'test' set up to track remote branch 'test' from 'origin'.
[root@huangzb git3]#
[root@huangzb git3]# git branch -av
* dev                   8d09f82 update heloc.txt
  master                8d09f82 update heloc.txt
  test                  8d09f82 update heloc.txt
  remotes/origin/HEAD   -> origin/master
  remotes/origin/dev    8d09f82 update heloc.txt
  remotes/origin/master 8d09f82 update heloc.txt
  remotes/origin/test   8d09f82 update heloc.txt
  remotes/origin/test2  8d09f82 update heloc.txt
[root@huangzb git3]#

3.4 清理无效分支

当我们使用git命令操作远程仓库时,会在本地生成一个本地的远程分支,此时我们如果直接在github中删除了某一个远程分支,但是本地对应的远程分支依然存在,但是此时的本地的远程分支已经失效了,因为对应的远程分支都删除了,所有需要清除掉这样的无效的本地远程分支,如何删除呢?

这里分为两个步骤

  1. 尝试模拟可以删除的本地远程分支----即不会真的删除掉,但是会返回需要删除的本地远程分支信息
  2. 真正的删除掉无效的本地远程分支

可以直接使用步骤2,但是最好的先使用步骤1查看一波

操作如下:

这里我已经在github中将test分支删除掉了

3.4.1 尝试删除

使用命令 git remote prune origin --dry-run, 操作如下:

[root@huangzb git3]# git branch -av
* dev                   8d09f82 update heloc.txt
  master                8d09f82 update heloc.txt
  test                  8d09f82 update heloc.txt
  remotes/origin/HEAD   -> origin/master
  remotes/origin/dev    8d09f82 update heloc.txt
  remotes/origin/master 8d09f82 update heloc.txt
  remotes/origin/test   8d09f82 update heloc.txt
  remotes/origin/test2  8d09f82 update heloc.txt
[root@huangzb git3]#
[root@huangzb git3]# git remote prune origin --dry-run
Pruning origin
URL: https://github.com/duguxiaobiao/study-git.git
* [would prune] origin/test
[root@huangzb git3]#

由上图中可以看出,通过该命令,识别出 origin/test 追踪分支属于无效的分支,下面可以执行删除操作的。

3.4.2 直接清楚无效分支

在 3.4.1 中查看了存在待删除的无效分支,那么使用命令 git remote prune origin 来直接删除,操作如下:

[root@huangzb git3]# git branch -av
* dev                   8d09f82 update heloc.txt
  master                8d09f82 update heloc.txt
  test                  8d09f82 update heloc.txt
  remotes/origin/HEAD   -> origin/master
  remotes/origin/dev    8d09f82 update heloc.txt
  remotes/origin/master 8d09f82 update heloc.txt
  remotes/origin/test   8d09f82 update heloc.txt
  remotes/origin/test2  8d09f82 update heloc.txt
[root@huangzb git3]#
[root@huangzb git3]# git remote prune origin
Pruning origin
URL: https://github.com/duguxiaobiao/study-git.git
* [pruned] origin/test
[root@huangzb git3]#
[root@huangzb git3]# git branch -av
* dev                   8d09f82 update heloc.txt
  master                8d09f82 update heloc.txt
  test                  8d09f82 [gone] update heloc.txt
  remotes/origin/HEAD   -> origin/master
  remotes/origin/dev    8d09f82 update heloc.txt
  remotes/origin/master 8d09f82 update heloc.txt
  remotes/origin/test2  8d09f82 update heloc.txt
[root@huangzb git3]#

由上图可以看到,已经将无效的本地远程分支 origin/test 删除掉了。

但是需要注意的是,一般清除掉远程的无效分支后,也会将没有用处的本地分支也需要清除一下,如下:

[root@huangzb git3]# git branch -d test
Deleted branch test (was 8d09f82).
[root@huangzb git3]#

3.5 查看本地分支与远程分支的关联信息

如果我们想看我们的本地分支与远程分支的对应关系,例如我想查看下本地的 dev分支对应远程的哪一个分支,可以命令 git remote show origin 来查看,操作如下:

[root@huangzb git3]# git remote show origin
* remote origin
  Fetch URL: https://github.com/duguxiaobiao/study-git.git
  Push  URL: https://github.com/duguxiaobiao/study-git.git
  HEAD branch: master
  Remote branches:
    dev    tracked
    master tracked
    test2  tracked
  Local branches configured for 'git pull':
    dev    merges with remote dev
    master merges with remote master
  Local refs configured for 'git push':
    dev    pushes to dev    (up to date)
    master pushes to master (up to date)
[root@huangzb git3]#
 

从上图中,我们可以清楚的看到如下数据

  1. origin对应的远程仓库地址-----即 push 和 fetch 操作的远程仓库地址
  2. 当前分支在master中
  3. 目前远程的分支有三个
  4. 本地有两个分支与远程的分支相关联,且清楚的直接本地的某个分支对应的远程的哪个分支
原文地址:https://www.cnblogs.com/duguxiaobiao/p/12691497.html