您的位置:首页 > 编程语言

github上fork别人的代码之后,如何保持和原作者同步的更新

2014-10-28 11:22 746 查看
git clone 命令

-o <name>

Instead of using the remote name origin to keep track of the upstream repository, use <name>.

1.从自己fork之后的版本库clone

git clone -o chucklu https://github.com/chucklu/Hearthstone-Deck-Tracker.git
2.再将别人的版本库git remote add

git remote add epix37 https://github.com/Epix37/Hearthstone-Deck-Tracker.git
查看所有远端

$ git remote -v
chucklu https://github.com/chucklu/Hearthstone-Deck-Tracker.git (fetch)
chucklu https://github.com/chucklu/Hearthstone-Deck-Tracker.git (push)
epix37 https://github.com/Epix37/Hearthstone-Deck-Tracker.git (fetch)
epix37 https://github.com/Epix37/Hearthstone-Deck-Tracker.git (push)

如果之前用的是git clone命令的话,可以用rename来进行重命名远端

重新命名远端git remote rename oldname newname

如何将本地分支和远端分支进行映射

====================================

Administrator@LUJUNTAO /d/SourceCode/SuperSocket (master)
$ git remote -v
chucklu https://github.com/chucklu/SuperSocket.git (fetch)
chucklu https://github.com/chucklu/SuperSocket.git (push)
kerryjiang https://github.com/kerryjiang/SuperSocket.git (fetch)
kerryjiang https://github.com/kerryjiang/SuperSocket.git (push)

Administrator@LUJUNTAO /d/SourceCode/SuperSocket (master)
$ git branch -vv
chucklu_master 1c8e75f [chucklu/master] added websocket data encoders
chucklu_v1.6 5db9e34 [chucklu/v1.6] fixed the bug m_CurrentSourceCount was n
ot updated in SmartPool
demo 4e3ffeb [chucklu/demo] 1.MultipleCommandAssembly示例移动到2-Med
ium文件夹下,删除多余的
* master 1c8e75f [kerryjiang/master] added websocket data encoders
v1.6 5db9e34 [kerryjiang/v1.6] fixed the bug m_CurrentSourceCount wa
s not updated in SmartPool

3.本地建立个分支

chucklu_master分支用来对应自己远端的master分支

master分支用来对应原作者的master分支

$ git branch
chucklu_master
*master

(1)切换到master分支

git checkout master

首先确保目前处于master分支,上面的git branch就是查看本地分支的命令,master前面的*表示当前分支是master分支

(2)同步原作者的代码

git pull

(3)切换到chucklu_master分支

git checkout chucklu_master

(4)变基或者合并

git rebase master

git merge master

(5)推送代码到自己的版本库

git push chucklu HEAD:master或者

git push chucklu chucklu_master:master

假如自己fork版本库之后,已经在某个分支上进行了修改的话。

那么rebase就不适用,需要使用cherry-pick来处理。

为了确保cherry pick之后的代码,确实是自己所期望的,那么只需要对比一次,自己的分支的最后一次提交和原作者的分支的最后一次提交,看看差异,是否是自己额外修改导致的

使用tortoisegit-->diff with previous version



使用cherry-pick的注意事项,如果其中有某一个commit是合并导致的,那么这个commit就不需要进行cherry-pick

git pull

$ git pull
remote: Counting objects: 125, done.
remote: Compressing objects: 100% (49/49), done.
emote: Total 125 (delta 94), reused 106 (delta 76), pack-reused 0Receiving objec

Receiving objects: 100% (125/125), 30.93 KiB | 0 bytes/s, done.
Resolving deltas: 100% (94/94), completed with 40 local objects.
From https://github.com/Epix37/Hearthstone-Deck-Tracker 4079dfa..9153391 master -> epix37/master
* [new branch] FilePrinting-logReader -> epix37/FilePrinting-logReader
* [new tag] v0.10.18 -> v0.10.18
Updating 4079dfa..9153391

$ git pull
remote: Counting objects: 9, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 9 (delta 7), reused 7 (delta 5), pack-reused 0
Unpacking objects: 100% (9/9), done.
From https://github.com/Epix37/Hearthstone-Deck-Tracker 9153391..f3c7c0a master -> epix37/master
* [new tag] v0.10.19 -> v0.10.19

今天看到一篇文章,貌似cherry-pick不推荐使用

http://dan.bravender.net/2011/10/20/Why_cherry-picking_should_not_be_part_of_a_normal_git_workflow.html

====9月13日更新====

如果你仅仅是同步原作者的master分支,而不需要进行合并操作的话,本地仅有一个分支也够用了

不过,需要添加2个remote
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: