在本地
git init
一个新的仓库,在github或者gitee上创建新的仓库时如何建立连接
git remote -v
git remote add origin https://gitee.com/zhengzy/tzwy.git
git remote -v
git add *
git commit -am init
git pull origin master --allow-unrelated-histories
git status
git add *
git commit -am init
git push -u origin master
收到别人的pull request,首先需要pull别人的代码,例如njustwh2014
将Pull Request发出者的派生版本库添加为一个新的源
>git remote add njustwh2014 https://github.com/njustwh2014/spring-boot-redis-guava-caffeine-cache.git
此时版本库中有两个源,一个克隆时自动建立的origin,另外一个就是新增加的njustwh2014。
>git remote -v
njustwh2014 https://github.com/njustwh2014/spring-boot-redis-guava-caffeine-cache.git (fetch)
njustwh2014 https://github.com/njustwh2014/spring-boot-redis-guava-caffeine-cache.git (push)
origin https://github.com/zheng-zy/spring-boot-redis-guava-caffeine-cache.git (fetch)
origin https://github.com/zheng-zy/spring-boot-redis-guava-caffeine-cache.git (push)
获取远程版本库njustwh2014的分支和提交。
>git fetch njustwh2014
remote: Enumerating objects: 33, done.
remote: Counting objects: 100% (33/33), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 17 (delta 6), reused 17 (delta 6), pack-reused 0
Unpacking objects: 100% (17/17), done.
From https://github.com/njustwh2014/spring-boot-redis-guava-caffeine-cache
* [new branch] master -> njustwh2014/master
现在除了本地分支master外,还有若干远程分支,如下:
>git branch -a
* master
remotes/njustwh2014/master
remotes/origin/HEAD -> origin/master
remotes/origin/master
此时可以切换到别人pull上来的代码分支,运行或者查看相关代码
>git checkout -b njumaster remotes/njustwh2014/master
Switched to a new branch 'njumaster'
Branch 'njumaster' set up to track remote branch 'master' from 'njustwh2014'.
后面步骤可以上idea上完成更方便
如果pull代码没有问题,可以merge到当前主分支上,提交之后push到自己远程库上,完成本次代码合并
删除本地分支
>git branch -D ngumaster
Deleted branch ngumaster (was 86b127b).
删除添加的远程分支
>git remote remove njustwh2014
参考:http://www.worldhello.net/gotgithub/04-work-with-others/010-fork-and-pull.html#merge-by-hands