git: fork仓库后的同步操作

2023-12-26 15:07:03

git的fork仓库使用

1.从主仓库派生/fork一个仓库
2. clone 自己的fork仓库到本地
git clone http://lyn@xxxxx/xxxx/xxxx.git
3. 查看clone的仓库remote
get remote -v      	// 此命令用于查看本地仓库的数据源,如果你没有添加任何别的仓库,之间简单的clone了自己的仓库,那这里面应该只有一个名为origin的源,地址为你自己的远程仓库地址

origin  http://lyn@xxxxx/xxxx/xxxx.git(fetch)
origin  http://lyn@xxxxx/xxxx/xxxx.git(push)
4. 添加上游仓库(fork的主仓库)
git remote add upstream 上游仓库地址
// git remote add <name> <url>  //此命令用于为本地仓库添加一个地址为url名为name的远程仓库数据源
5.查看上游仓库是否成功关联到本地仓库
get remote -v      	// 此命令用于查看本地仓库的数据源,如果你没有添加任何别的仓库,之间简单的clone了自己的仓库,那这里面应该只有一个名为origin的源,地址为你自己的远程仓库地址

origin  http://lyn@xxxxx/xxxx/xxxx.git(fetch)
origin  http://lyn@xxxxx/xxxx/xxxx.git(push)
upstream  http://lyn@xxxxx/xxxx/xxxx.git(fetch)
upstream  http://lyn@xxxxx/xxxx/xxxx.git(push)
6.把上游分支同步到本地
git fetch upstream
git fetch upstream master  // 只拉取上游master分支
7.查看当前所有分支
git branch -a   // 查看本地所有分支
8.切换分支
 git checkout GuangDong
 
 出现下面情况,提示错误,因为是有2个仓库都是叫GuangDong,所以不知道是要切换到自己仓库的GuangDong还是主仓库的GuangDong
	hint: If you meant to check out a remote tracking branch on, e.g. 'origin',
	hint: you can do so by fully qualifying the name with the --track option:
	hint:
	hint:     git checkout --track origin/<name>
	hint:
	hint: If you'd like to always have checkouts of an ambiguous <name> prefer
	hint: one remote, e.g. the 'origin' remote, consider setting
	hint: checkout.defaultRemote=origin in your config.
	fatal: 'GuangDong' matched multiple (2) remote tracking branches
	
解决:
git checkout --track upstream/GuangDong
这样就可以切换成功 
8.同步分支
git merge upstream/master    // 同步主仓库中的master 分支到本地仓库的这一分支上
9.推送代码到主仓库的分支
git commit -m 描述  //提交暂存区到本地仓库中
git push <远程主机名> <本地分支名>:<远程分支名>  //本地仓库代码提交到远程仓库
git push upstream master    // 推送代码到主仓库的分支

文章来源:https://blog.csdn.net/weixin_43959024/article/details/135215593
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。