GIT-Fork代码的更新以及提交到原分支

需求:fork 了代码后,修改了fork后的内容,须要将改动的代码同步到原分支。git

clone fork的代码

$ git clone http://xxx.git  
Cloning into 'xxxxxx'...

给fork加一个remote

查看远程仓库fetch

$ git remote  
origin

吧远程仓库加入remote.net

$ git remote add upstream http://原地址.git

再次查看code

$ git remote  
origin  
upstream

这里出现了“upstream”,说明添加远程地址成功blog

同步fork代码至最新

将fork的代码和原仓库代码进行同步,旨在保持当前fork代码最新
若是不一样步直接发起merge的话,原仓库会显示冲突致使没法同步rem

$ git fetch upstream  
remote: Counting objects: 7, done.  
remote: Compressing objects: 100% (6/6), done.  
remote: Total 7 (delta 4), reused 0 (delta 0)  
Unpacking objects: 100% (7/7), done.  
From http://XXXX 
 * [new branch]      DEV        -> upstream/DEV  
 * [new branch]      master     -> upstream/master

若是是使用Idea ,git fetch 的界面操做是:
项目右键-Git-Repository-Fetch
而后在导航栏VCS-Update Project后,右下角能够看到upstream的分支get

切换本地分支

$ git checkout DEV  
Already on 'DEV'  
Your branch is up-to-date with 'origin/DEV'.

若是在Idea中切换能够参考:http://www.javashuo.com/article/p-rkjfuwgt-nw.html同步

合并最新代码

将原仓库内容和本地分支合并,GIt命令:it

$ git merge upstream/DEV  
Updating adcbf99..6de327b  
Fast-forward  
 xxx/src/main/resources/banner.txt | 2 +-  
 1 file changed, 1 insertion(+), 1 deletion(-)

将原仓库内容和本地分支合并,Idea操做:
右键-Git-Repository-Merge changes 选择upstream分支合并到本地io

解决冲突

若是是Idea中merge的时候会直接提示,并有可视化操做界面

提交Fork

将Fork的代码提交至远程仓库

同步至原仓库

向原地址发起merge request便可

其余操做

当时在添加remote的时候添加错误,致使一直fetch 不到数据
查看remote地址

$ git remote -v
origin  http://lu.xu@git.bqjr.club/lu.xu/OAM_OAM.git (fetch)
origin  http://lu.xu@git.bqjr.club/lu.xu/OAM_OAM.git (push)
upstream        http://lu.xu@git.bqjr.club/lu.xu/OAM_OAM.git (fetch)
upstream        http://lu.xu@git.bqjr.club/lu.xu/OAM_OAM.git (push)

这里看到我吧本身的git地址添加到了upstream,致使没法获取原地址内容

删除不正确的

git remote remove upstream

再次查看

$ git remote -v
origin  http://lu.xu@git.bqjr.club/lu.xu/OAM_OAM.git (fetch)
origin  http://lu.xu@git.bqjr.club/lu.xu/OAM_OAM.git (push)

删除remote 成功, 重复以上步骤便可

相关文章
相关标签/搜索