git如何配置主干master连接远程仓库地址[email protected]:/home/yr/git/test.git 分支new_master连接本地仓库地址/home/yrr/git/test.git ,再将对应的代码上传到相应的仓库
时间: 2024-11-12 19:40:43 浏览: 66
首先,你需要在本地设置两个Git仓库,一个为主分支(master)链接远程仓库,另一个为新的分支(new_master)链接本地仓库。以下是步骤:
1. **配置主干master连接远程仓库**:
打开终端,在`master`分支目录下,运行以下命令来添加远程仓库并跟踪远程分支:
```
cd /home/yr/git/test.git
git remote add origin [email protected]:/home/yr/git/test.git
```
确认添加成功后,可以拉取远程分支:
```
git fetch origin master
```
2. **创建并初始化新的分支new_master**:
回到你的本地目录 `/home/yrr/git/test.git`:
```
cd /home/yrr/git/test.git
git checkout -b new_master
```
如果需要从主分支切换到新分支,可以使用 `git switch` 或 `git checkout` 命令。
3. **在new_master上工作**:
在`new_master`上进行修改、添加文件等操作。假设你对`file.txt`进行了修改:
```
echo "This is new content." >> file.txt
```
4. **提交更改**:
```
git add .
git commit -m "Initial commit on new_master"
```
5. **推送新分支到本地仓库**:
```
git push -u origin new_master
```
这里 `-u` 指定了默认推送到远程仓库的新分支。
6. **将new_master分支连接到远程仓库**:
回到原来的新分支目录 `/home/yrr/git/test.git/new_master`:
```
git remote add new_repo [email protected]:/home/yrr/git/test.git
```
接着,你可以把本地的分支同步到新的远程仓库:
```
git push new_repo new_master
```
完成以上步骤后,你就已经成功地配置了主分支(master)连接远程仓库,并且新分支(new_master)连接了本地仓库,代码也已上传到了相应的位置。
阅读全文