首页 > 其他 > 详细

Git remote 同步远程仓库,保持fork出来的项目和原有项目同步

时间:2017-02-20 00:02:48      阅读:420      评论:0      收藏:0      [点我收藏+]

我们在创建一个Git工程项目时, 至少可以看到一个名为 origin 的远程库, git 默认使用这个名字来标识你本地工程所对应的远程仓库。

一. 添加远程仓库

一个git工程除了默认的origin 仓库外,还可以为其添加一个新的远程仓库, 可以随意指定一个名字, 运行 git remote add [shortname] [url]

 

[html] view plain copy
 
  1. $ git remote add new_repository_name git://github.com/paulboone/ticgit.git  

这样就相当与为你本地的git 工程项目配置了两个远程仓库,当然你本地的git 工程默认是绑定到origin 仓库的。执行$ git remote 命令将会显示所有远程仓库名称:

 

[html] view plain copy
 
  1. $ git remote  
  2.    origin  
  3.    new_repository_name  

二. 问题描述: 当我们 在github上fork出一个项目后,如果原有的项目更新了,怎样保持我们fork出来的项目和原有项目保持同步呢,并提交我们的代码呢?

 

1. 首先添加你从github上fork的源仓库到本地的git工程

[html] view plain copy
 
  1. $ git remote add source_repository_name [url]  

2. 假设origin仓库和source_repository_name源仓库都有一个分支branch_name,你在该分支上进行开发,将本地修改commit后,在每次Push前做如下操作,即可实现和上游source_repository_name仓库同步:(需要注意的是在操作step2之前,一定要将checkout到branch_name所指定的branch)

 

 

[html] view plain copy
 
  1. (1)同步源仓库的信息到本地   
  2.    $ git remote update source_repository_name  
  3. (2)将源仓库的信息merge到本地分支:   
  4.    $ git checkout branch_name  
  5.    $ git rebase source_repository_name/branch_name  

3. $ git push 将最新同步的代码和修改,提交到你的origin仓库

4. Github上提出Push Request即可,将你的origin仓库的所有修改提交到source_repository_name仓库

 

GitHub常用的开发协同流程为:将别人的仓库fork成自己的origin仓库 → git clone origin仓库到本地 → 本地添加fork源仓库 → 工作前先git remote update下fork源保持代码较新 → coding → push回自己 → github上提出Push Request即可

Git 常见命令一览表:

 

说明/备注 命令 备注
保存更新 git add [-i] -i 逐个确认
检查更新 git status  
提交更新 git commit [-a] -m "<更新说明>" -a 包含增删
-m 说明信息
克隆到本地 git clone <git地址>  
远端抓取 git fetch  
与本地当前branch合并 git merge  
抓取并合并 git pull [<远端别名>] [<远端branch>]
相当于 git fetch + git merge
推送到远端 git push [-f] [<远端别名>] [<远端branch>]
-f 强制覆盖
设置一个远端别名 git remote add <别名> <git地址>  
列出远端 git remote -v -v 详细信息
查看远端信息 git remote show <远端别名>  
重命名远端 git remote rename <远端别名> <新远端别名>
删除远端 git remote rm <远端别名>  
更新branch列表 git remote update [<远端别名>]  
列出branch git branch [-r] [-a] -r 远端
-a 全部
新建branch git branch <branch名>  
切换branch git checkout <branch名>  
创建本地branch对应远端branch git checkout -b <本地branch> -t <远端别名>/<远端branch>
-b 新建branch;-t 绑定远端branch
设置HTTP代理 git config --global http.proxy "<HTTP代理>"
恢复默认
Linux系统编辑 ~/.gitconfig 文件
设置电子邮件 git config --global user.email "<电子邮件>"
设置用户名 git config --global user.name "<用户名>"
查看标签(tag) git tag [--list]
打包、快照(snapshot) git archive [--prefix=<前缀路径>/] -o <文件名及格式> <branch或标签>
--prefix 指定前缀路径;格式可以是 zip, tar
     

Git remote 同步远程仓库,保持fork出来的项目和原有项目同步

原文:http://www.cnblogs.com/xmlee/p/6417759.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!