git branch 相关操作总结 新建分支 删除分支 切换分支 查看分支

时间:2021-11-17 21:53:32
  1. 查看分支

    (1) 查看本地分支  git branch 列出本地已经存在的分支,并且在当前分支的前面加*号标记,例如:
    localhost:website admin$ git branch
    * branch_dev_2_1_0
      master

    (2) 查看远程分支  git branch -r 例如:
    localhost:website admin$ git branch -r
      origin/branch_dev_2_1_0
      origin/branch_dev_2_1_0_cover
      origin/branch_dev_2_1_0_metrics
      origin/branch_dev_2_1_0_php7
      origin/master

    (3) 查看所有分支  git branch -a 例如:
    localhost:website admin$ git branch -a
    * branch_dev_2_1_0
      master
      origin/branch_dev_2_1_0
      origin/branch_dev_2_1_0_cover
      origin/branch_dev_2_1_0_metrics
      origin/branch_dev_2_1_0_php7
      origin/master

  2. 新建分支

    (1)新建分支但不切换到该分支  git branch 分支名,例如:
    localhost:website admin$ git branch
    * branch_dev_2_1_0
      master
    localhost:website admin$ git branch new_branch
    localhost:website admin$ git branch
    * branch_dev_2_1_0
      master
      new_branch
    可以看出,虽然新建了本地分支 new_branch 但是当前分支并没有切换(带*号为当前分支)

    (2) 新建并切换到该分支  git checkout -b 分支名,例如:
    localhost:website admin$ git branch
    * branch_dev_2_1_0
      master
    localhost:website admin$ git checkout -b new_branch
    Switched to a new branch 'new_branch'
    localhost:website admin$ git branch
      branch_dev_2_1_0
      master
    * new_branch
    新建本地分支 new_branch 的同时将当前分支切换为 new_branch (带*号为当前分支)

  3. 分支切换

    git checkout 分支名,例如:
    localhost:website admin$ git branch
      branch_dev_2_1_0
      master
    * new_branch
    localhost:website admin$ git checkout master
    Switched to branch 'master'
    localhost:website admin$ git branch
      branch_dev_2_1_0
    * master
      new_branch
    可以看出,原来的分支为 new_branch ,执行完 git checkout master 后,将分支切换到 master(带*号为当前分支)

  4. 删除本地分支

    git branch -d 分支名,例如:
    localhost:website admin$ git branch
    * branch_dev_2_1_0
      master
      new_branch
    localhost:website admin$ git branch -d new_branch
    Deleted branch new_branch (was e6d6ae0).
    localhost:website admin$ git branch
    * branch_dev_2_1_0
      master
    可以看出,原来总共有3个分支,删除 new_branch后变成了两个分支