1、新建分支
本地分支 git branch <branch-name>
远程分支 git push origin <branch-name>:<new-branch-name>
2、删除分支
本地分支 git branch -d <branch-name> 或 git branch -D <branch-name>
远程分支 git push origin :<branch-name> 如果报没有关联(ref)远程分支然后再执行命令3清理远程分支
3、清理远程分支,把本地不存在的远程分支删除
git remote prune origin
4、本地分支与远程分支关联与取消关联
git branch --set-upstream-to=origin/<branch-name>
git branch --unset-upstream <branch-name>
5、提交本地分支到指定的远程分支(不一定是关联的远程分支,不一定有权限)
git push origin <branch-name>
6、克隆指定的远程分支
git clone -b <branch-name> http://xxx/xxx/xxx.git
默认是克隆远程master主分支(HEAD -> origin/master)
7、本地版本回退
查看本地分支日志:git reflog
本地分支回退到指定的结点:git reset —-hard {n}
8、查看和修改git服务器地址和所有本地及远程分支:git config —-list
或者 git remote get-url --all origin
修改本地git仓库关联的远程仓库地址:git remote set-url origin https://xxxx.git
9、把本地已有的Git仓库推送到远程空的仓库中的命令
git remote add origin https://xxx.git
git push -u origin master
10、快速查看本地分支和远程列表:git branch -a
11、Git中文文件名自动转Unicode问题:git config --global core.quotepath false
12、tag命令相关
(1)创建本地tag标签:git tag tag名称 [-m "备注信息"]
(2)把本地指定tag同步到远程分支:git push origin tag tag名称
将本地所有tag同步到远程仓库:git push origin --tags
(3)把远程对应分支上的所有tag同步到本地分支:git pull
把远程对应分支上的指定tag同步到本地分支: git fetch origin tag 远程指定的tag名称
(4)删除本地指定的tag:git tag -d tag名称
删除远程对应分支上的指定tag: git push origin -d 远程指定的tag名称
注意:创建本地tag后,在不修改git工程的情况下通过git push命令无法推送本地tag到远程;同样的删除本地tag后,通过git push命令也无法删除远程的tag。
问题场景如: 使用git pull提示refusing to merge unrelated histories 创建了一个origin,两个人分别clone
分别做完全不同的提交
第一个人git push成功
第二个人在执行git pull的时候,提示
fatal: refusing to merge unrelated histories 解决方法:
git pull --allow-unrelated-histories