吃透GIT系列之【git的分支操作(查看分支/切换分支/新建分支/删除分支)】

一、查看分支

1、查看全部分支

git branch -a

2、查看本地分支

git branch -l

3、查看远程分支

git branch -r

二、切换分支

假设本地当前处于master分支下,远端有一个test1分支和一个test2分支,想要切换到test1这个分支,怎么办?

git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/test1
  remotes/origin/test2
  remotes/origin/master

可以使用以下两个命令中的任何一个:

git checkout test1
git checkout -b test1 remotes/origin/test1

请注意:checkout命令如果使用了-b的语法,则一定要携带一个远端分支作为参数,否则,该命令的执行效果就变成了:在本地新建一个分支,基于master分支,但是与远端的test1分支并无任何关系。

以下是上述各个命令的执行效果:

git checkout test1
Branch test1 set up to track remote branch test1 from origin.
Switched to a new branch 'test1'

git checkout -b test1 remotes/origin/test1
Branch test1 set up to track remote branch test1 from origin.
Switched to a new branch 'test1'

git checkout -b test1
Switched to a new branch 'test1'

请自行仔细对比上述3个命令之间的差异!!!

三、新建分支

1、创建分支

git branch name

2、切换分支

git checkout name

3、创建+切换分支

git checkout -b name

四、删除分支

1、删除本地分支

git branch -d branch_name
git branch -D branch_name

2、删除远程分支

git push origin :branch_name

请注意:origin后面有一个空格

五、重命名分支

git branch (-m | -M) <oldbranch> <newbranch>

重命名oldbranch为newbranch,使用-M则表示强制重命名。

如果你需要重命名远程分支,推荐的做法是:
1、删除远程待修改分支
2、push本地新分支名到远程

六、待添加

猜你喜欢

转载自blog.csdn.net/LEON1741/article/details/72649106