如何使用git branch命令

来自百问网嵌入式Linux wiki


前言

Git 的宗旨就是提供一个即快速又简单的的分支功能,Git 的分支概念在这个领域中的确是独一无二的,特别是在易用性和效率方面。
在一个项目中,虽然我们的工作都一样--不断添加项目代码,最终完成项目。而现在这个项目我们托管在了 git 仓库中,这样一个项目我们可以多人协作展开。


分支为我们每个开发者提供了一个相对独立完整的工作环境,当你生成并切换到自己创建的分支上的时候,与其他开发者就是独立平行的关系,你在自己的分支上干活,想提交就提交,你现在的更改完全不会影响其他人的进展。当你编写好全部代码,合并分支后彼此之间才又回到了源分支上。这样既安全,又不影响别人工作。

使用 git branch

那么我们该如何使用 git 中的分支功能呢?
git 为我们提供了专门的 git branch 命令,让我们可以对分支进行管理:
在一个新的仓库中,虽然我们没有在上面使用过分支,但是我们默认是处于 master 分支上了,使用 git status 命令即可查看当前的仓库状态:
book@www.100ask.org:~/tmp/Embedded-Linux-ADCMSE$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
book@www.100ask.org:~/tmp/Embedded-Linux-ADCMSE$
在使用 git branch 命令之前,先查看 git branch 的帮助信息:
book@www.100ask.org:~$ git branch -h
usage: git branch [<options>] [-r | -a] [--merged | --no-merged]
   or: git branch [<options>] [-l] [-f] <branch-name> [<start-point>]
   or: git branch [<options>] [-r] (-d | -D) <branch-name>...
   or: git branch [<options>] (-m | -M) [<old-branch>] <new-branch>
   or: git branch [<options>] [-r | -a] [--points-at]

Generic options
    -v, --verbose         show hash and subject, give twice for upstream branch
    -q, --quiet           suppress informational messages
    -t, --track           set up tracking mode (see git-pull(1))
    --set-upstream        change upstream info
    -u, --set-upstream-to <upstream>
                          change the upstream info
    --unset-upstream      Unset the upstream info
    --color[=<when>]      use colored output
    -r, --remotes         act on remote-tracking branches
    --contains <commit>   print only branches that contain the commit
    --abbrev[=<n>]        use <n> digits to display SHA-1s

Specific git-branch actions:
    -a, --all             list both remote-tracking and local branches
    -d, --delete          delete fully merged branch
    -D                    delete branch (even if not merged)
    -m, --move            move/rename a branch and its reflog
    -M                    move/rename a branch, even if target exists
    --list                list branch names
    -l, --create-reflog   create the branch's reflog
    --edit-description    edit the description for the branch
    -f, --force           force creation, move/rename, deletion
    --merged <commit>     print only branches that are merged
    --no-merged <commit>  print only branches that are not merged
    --column[=<style>]    list branches in columns
    --sort <key>          field name to sort on
    --points-at <object>  print only branches of the object

book@www.100ask.org:~$
根据帮助信息我们可以大致了解 git branch 的各个操作选项及其作用。
下面根据帮助信息中列出的用法,对 git branch 的方法给出一些参考示例。

查看分支

   git branch [<options>] [-r | -a] [--merged | --no-merged]
查看本地所有分支
   git branch [<options>]
book@www.100ask.org:~/tmp/Embedded-Linux-ADCMSE$ git branch
* master
book@www.100ask.org:~/tmp/Embedded-Linux-ADCMSE$
查看远程所有分支 (-r 即 remote)
   git branch [<options>] [-r | -a] [--merged | --no-merged]
book@www.100ask.org:~/tmp/Embedded-Linux-ADCMSE$ git branch -r
  origin/HEAD -> origin/master
  origin/master
book@www.100ask.org:~/tmp/Embedded-Linux-ADCMSE$
看本地和远程所有分支
   git branch [<options>] [-a] [--merged | --no-merged]
book@www.100ask.org:~/tmp/Embedded-Linux-ADCMSE$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
book@www.100ask.org:~/tmp/Embedded-Linux-ADCMSE$
可见,一般当前本地分支前带有“*”号且为绿色,远程分支为红色

新建分支

   git branch [<options>] [-l] [-f] <branch-name> [<start-point>]
仅仅是新建分支:
book@www.100ask.org:~/tmp/Embedded-Linux-ADCMSE$ git branch test1
book@www.100ask.org:~/tmp/Embedded-Linux-ADCMSE$ git branch
* master
  test1
book@www.100ask.org:~/tmp/Embedded-Linux-ADCMSE$
可见,虽然新建了test分支,但是目前依旧在dev5.7分支上。
新建分支的同时并切换到新建的分支:
book@www.100ask.org:~/tmp/Embedded-Linux-ADCMSE$ git checkout  -b test2
Switched to a new branch 'test2'
book@www.100ask.org:~/tmp/Embedded-Linux-ADCMSE$ git branch
  master
  test1
* test2
book@www.100ask.org:~/tmp/Embedded-Linux-ADCMSE$

删除分支

 git branch [<options>] (-d | -D) <branch-name>...
删除本地分支
   git branch -d <local_branchname>

注意:我们删除当前所处于的分支,如果需要删除当前所处于的分支,要先切换到其他分支,再删除该分支。

book@www.100ask.org:~/tmp/Embedded-Linux-ADCMSE$ git branch
  master
  test1
* test2
book@www.100ask.org:~/tmp/Embedded-Linux-ADCMSE$ git branch -d test1
Deleted branch test1 (was e0ad5d6).
book@www.100ask.org:~/tmp/Embedded-Linux-ADCMSE$ git branch
  master
* test2
book@www.100ask.org:~/tmp/Embedded-Linux-ADCMSE$
删除远端分支

git branch [<options>] [-r] (-d | -D) <local-branch-name>...

book@www.100ask.org:~/tmp/Embedded-Linux-ADCMSE$ git branch -a
  master
* test2
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
book@www.100ask.org:~/tmp/Embedded-Linux-ADCMSE$ git branch -r -d origin/master
Deleted remote-tracking branch origin/master (was e0ad5d6).
book@www.100ask.org:~/tmp/Embedded-Linux-ADCMSE$ git branch -a
warning: ignoring broken ref refs/remotes/origin/HEAD
  master
* test2
book@www.100ask.org:~/tmp/Embedded-Linux-ADCMSE$
目前为止都没有用到 -D 参数, -D 和 -d 的作用一样,不同的是 -D 表示强制删除,相当于 --delete --force (delete fully merged branch force creation, move/rename, deletion)

重名名分支

   git branch [<options>] (-m | -M) [<old-branch>] <new-branch>
根据命令可知,使用命令我们可将 old-branch 命名为 new-branch 。使用 -M 选项表示强制重命名。
将本地分支重命名
book@www.100ask.org:~/tmp/Embedded-Linux-ADCMSE$ git branch -a
* master
  test1
  test2
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
book@www.100ask.org:~/tmp/Embedded-Linux-ADCMSE$ git branch -m test1 test0
book@www.100ask.org:~/tmp/Embedded-Linux-ADCMSE$ git branch -a
* master
  test0
  test2
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
book@www.100ask.org:~/tmp/Embedded-Linux-ADCMSE$
删除远端分支
  • 先删除远端待重命名的分支
  • 然后 push 本地新分支名到远端仓库


针对一些选项的解释

-d --delete:删除
-D --delete --force 的快捷键
-f --force:强制
-m --move:移动或重命名
-M --move --force 的快捷键
-r --remote:远程
-a --all:所有