1.查看本地分支
git branch
2.查看所有分支
git branch -a
3.新建分支
git branch branch_name
或
git checkout -b branch_name
区别:git branch branch_name新建分支后不会自动切换到新建的分之,git checkout -b branch_name则会自动切换到新建分支
4.切换分之
git checkout branch_name
5.推送新建的本地分支到远程仓库
git push origin zxd_test2
6.删除本地分之
git branch -d branch_name //删除本地分支
git branch -D branch_name //强制删除本地分支
提示:删除本地分之前先切换到别的分支,再执行删除分支操作
7.删除远程分之
git push origin --delete dev_name
8.查看远程仓库信息
git branch -r
或
git remote -v
9.查看本地分支与远程分支的追踪
显示每个(本地)分支当前指向的提交记录的哈希值,以及和其上游分支的相对位置(如果有的话)-v
与-verbose
是一个效果-vv
会显示上游分支的名字
举例
git branch -v
git branch -vv
$ git branch -vv
cyr_branch 689c970 [origin/cyr_branch: behind 2] default
* master f1e2810 finally
test-mysql e2fa7b9 add .gitignore
test-network 0d426ce add .gitignore
xu_branch e55fcf1 [origin/xu_branch: ahead 1] debug
zfc_branch e4a40a5 [origin/zfc_branch] merge
cyr_branch
的上游分支为origin/cyr_branch
,前者落后后者2xu_branch
的上游分支为origin/xu_branch
,前者领先后者1zfc_branch
的上游分支为origin/zfc_branch
,两者同步test-mysql
和test-network
没有对应的上游分支master
的上游分支为origin/master
,但是没有显示,我猜测是因为这个对应关系是固定的,所以无需显示
10.跟踪远程分支
最近使用git pull的时候多次碰见下面的情况:
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> release
其实,输出的提示信息说的还是比较明白的。
使用git在本地新建一个分支后,需要做远程分支关联。如果没有关联,git会在下面的操作中提示你显示的添加关联。
关联目的是在执行git pull, git push操作时就不需要指定对应的远程分支,你只要没有显示指定,git pull的时候,就会提示你。
解决方法就是按照提示添加一下呗:
git branch --set-upstream-to=origin/remote_branch your_branch
其中,origin/remote_branch是你本地分支对应的远程分支;your_branch是你当前的本地分支。
- 当前所在分支跟踪远程分支:
git branch --set-upstream-to=origin/<branch> your_branch
- 指定本地分支关联远程分支:
git branch --set-upstream 本地新建分支名 origin/远程分支名
例如,把本地dev分支和远程dev分支相关联:
git branch --set-upstream dev origin/dev
注:本地新建分支, push到远程服务器上之后,使用git pull或者git pull 拉取或提交数据时会报错,必须使用命令:git pull origin dev(指定远程分支);如果想直接使用git pull或git push拉去提交数据就必须创建本地分支与远程分支的关联
11.推送本地仓库到远端仓库并建立与远程仓库的跟踪关系
git push --set-upstream origin repo //repo为新创建的远端分支
12.删除远程文件 本地保留
git rm --cached filename
git commit -m "delete remote file filename "
git push oringin “分支名字”
13.删除远程文件夹 本地保留
git rm -r --cached filename
git commit -m "delete remote directory directoryname"
git push oringin “分支名字
14.提交代码到本地缓存区
git add .
15.添加提交的代码备注
git commit -m "备注内容"
16.推送提交的代码到远程新建的分支上面
git push origin feature-xx
17.更新远程分支列表
有时会遇到git branch -a时总是不出现新的分支或者远程已经没有的分支在本地还有,这时就需要更新下本地的git分支保持和远程分支一致,使用下面命令即可:
git remote update origin --prune
git remote update origin -p
18.清理远程已删除本地还存在的分支
git fetch --prune origin
或者 git fetch -p
或者 git pull -p
19.git 更新单个文件
git checkout origin/master file
20.查看代码提交者
git blame config/vehicle/vehicle_name_vv6-00/vehicle_info.pb.txt 查看文件中每行的提交信息
git blame -L 2,4 config/vehicle/vehicle_name_vv6-00/vehicle_info.pb.txt 查看第2至4行提交信息
git show commitID 查看本次commitID提交情况