mirror of
https://github.com/ZJDoc/GitGuide.git
synced 2026-07-29 15:20:25 +08:00
44 lines
935 B
Markdown
44 lines
935 B
Markdown
|
|
# 远程分支替换本地分支
|
|
|
|
## 问题描述
|
|
|
|
本地分支有`master`,想要使用远程仓库的`master`分支,如何替换?
|
|
|
|
## 问题解决
|
|
|
|
有两种解决方案
|
|
|
|
### 方案一
|
|
|
|
拉取远程分支后覆盖本地分支。首先拉取远程分支到本地
|
|
|
|
```
|
|
# 删除已有远程仓库
|
|
$ git remote remove origin
|
|
# 设置远程仓库链接
|
|
$ git remote add origin git@xxx.com:xx.git
|
|
$ 拉取远程分支
|
|
$ git fetch origin master
|
|
```
|
|
|
|
然后使用远程分支重置本地分支
|
|
|
|
```
|
|
$ git reset --hard origin/master
|
|
```
|
|
|
|
### 方案二
|
|
|
|
```
|
|
删除您的本地分支: git branch -d master
|
|
获取最新的远程分支: git fetch origin master
|
|
重建基于远程的本地分支: git checkout -b master origin/master
|
|
```
|
|
|
|
## 相关阅读
|
|
|
|
* [拉取指定远程分支到本地](./拉取指定远程分支到本地.md)
|
|
|
|
* [如何完全替换本地分支与远程分支?](https://cloud.tencent.com/developer/ask/26508)
|