1、创建分支
git branch <newBranchName>
2、切换分支
git checkout <branchName>
分支切换会改变工作目录中的文件。Git会自动添加、删除、修改文件以确保此时你的工作目录和这个分支最后一次提交时的样子一模一样。
3、创建分支并切换
git checkout –b <newBranchName>
4、合并分支的步骤(例如合并到主分支)
(1)切换回主分支
git checkout master
(2)然后合并其他分支到主分支中
git merge <otherBranchName>
(3)最后删除被合并了的分支
git branch –d <otherBranchBame>
5、查看分支
(1)查看当前所有分支
git branch (-v)
(2)查看哪些分支已经合并到当前所在分支
git branch –merged
(3)查看哪些分支未合并到当前所在分支
git branch –no-merged
6、推送到远程分支
(1)推送本地分支1到远程仓库上的分支1
git push <remoteName> <branch1>
(2)推送本地分支1到远程仓库上的分支2
git push <remoteName> <branch1>:<branch2>
7、跟踪远程分支
(1)创建本地分支branch1并跟踪远程分支<branchName>
git checkout –b <branch1> <remoteName>/<branchName>
(2)设置已有的本地分支跟踪一个刚刚拉取的远程分支
git branch –u <remoteName>/<branchName>
8、拉取远程分支
(1)推荐使用fetch拉取一个远程分支,然后再将该分支合并到某个已经存在的本地分支中。
a)先创建一个本地分支
git checkout –b <localBranch>
b)拉取远程分支
git fetch <remote> <branch>
c)合并到本地分支
git merge <remote> <branch>
(2)使用pull拉取一个已经被跟踪的远程分支
a)先创建一个本地分支并跟踪到欲拉取的远程分支
git checkout –b <branch1> <remote>/<branch2>
b)再拉取远程分支
git pull <branch2>
9、跟踪远程分支
(1)当前分支跟踪一个远程的分支
git checkout --track <remoteName>/<branchName>
(2)设置已有的本地分支跟踪一个刚刚拉取下来的远程分支,或者想要修改正在跟踪的上游分支:
git branch -u/--set-upstream-to <remoteName>/<branchName>
10、删除远程分支
git push <remote> --delete <branch>