Files
GitGuide/docs/git/advanced/远程分支操作.md
2021-03-18 20:08:24 +08:00

104 lines
2.2 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 远程分支操作
学习本地分支与远程分支的交互
1. 分支显示
2. 分支同步
3. 分支推送
4. 分支跟踪
5. 分支删除
使用命令
1. [git branch](https://git-scm.com/docs/git-branch)
2. [git fetch](https://git-scm.com/docs/git-fetch)
3. [git push](https://git-scm.com/docs/git-push)
4. [git checkout](https://git-scm.com/docs/git-checkout)
5. [git merge](https://git-scm.com/docs/git-merge)
## 分支显示
显示本地分支和远程分支的关系
```
# 使用参数-vv列出本地分支是否有跟踪远程分支并且本地分支是否领先或者落后远程分支
$ git branch -vv
```
## 分支同步
获取远程分支数据
```
$ git fetch 远程仓库 # origin是默认的远程仓库
```
使用`git fetch`不会合并远程分支,需要再显式使用`git merge`命令
使用`git pull`可实现拉取远程分支数据并合并
```
$ git pull 远程仓库 本地分支名
```
## 分支推送
推送本地分支内容到远程分支
```
$ git push 远程仓库 本地分支名/远程分支名
```
如果需要本地分支和远程分支名一致,实现如下:
```
$ git push 远程仓库 本地分支名
```
使用参数`-u`设置要推送的远程分支为本地待跟踪,方便后续拉取代码操作
```
$ git push -u 远程仓库 本地分支
```
## 分支跟踪
设置本地分支想要跟踪的远程分支
```
$ git checkout -b [branch] [remotename]/[branch]
```
如果远程分支和本地分支一致,那么使用简易方式
```
$ git checkout --track [remotename]/[branch]
```
修改要跟踪的远程分支,使用参数`-u``--set-upstream-to`
```
$ git branch -u [branch] [remotename]/[branch]
# 同样的分支名
$ git branch -u [remotename]/[branch]
```
## 分支删除
删除远程分支
```
$ git push 远程仓库 --delete 远程分支名
```
如果远程分支已在本地分支之后,可以不删除远程分支而使用强制方式推送到远程分支,参考[[Git高级教程(二)] 远程仓库版本回退方法](https://blog.csdn.net/fuchaosz/article/details/52170105)
```
$ git push -f
```
## 相关阅读
* [3.5 Git 分支 - 远程分支](https://git-scm.com/book/zh/v2/Git-%E5%88%86%E6%94%AF-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF)