2015年3月11日 星期三

將別的repository的commit合併到svn上

情境:
有兩個同專案的程式,因網路關係,其中一個有一陣子被放生到一個新的git上(因為git沒有網路也能用),現在要把git(程式較新)的commit移回svn上(git分出去後無提交新的commit)
假設svn倉庫在 http://svn.domain/trunk/project/
git倉庫在 ssh://bear@192.168.0.115:/srv/http/git_repo
本機端git svn 操作在git_svn_repo
本機端保留的git本地倉庫在 /your/git_repo/path

解法:
You'll need to add the other repository as a remote, then fetch its changes. From there you see the commit and you can cherry-pick it.

使用git svn 拉回svn的程式
$ git svn clone http://svn.domain/trunk/project/ git_svn_repo
$ cd git_svn_repo
新增git的遠端倉庫,並命名git_repo
$ git remote add git_repo file:///your/git_repo/path #我在本地端另外目錄有pull git倉庫的東西,先用他的commit 去merge
抓回git_repo
$ git fetch git_repo
檢查branch
$ git branch -a
查git倉庫的log
$ git log remotes/git_repo/master
將git倉庫你要合併的commit cherry-pick到master(他將會推上svn)
$ git cherry-pick commit_id
上傳到svn
$ git svn dcommit


將git_svn_repo的更新推到git
$ git remote rm git_repo #因為前面用本機保留的git本地倉庫,所以刪掉重新把remote設成git遠端倉庫
$ git remote add origin ssh://bear@192.168.0.115:/srv/http/git_repo
直接推送會報錯:
$ git push origin master
lman@192.168.0.115's password:
To ssh://bear@192.168.0.115:/srv/http/git_repo
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'ssh://bear@192.168.0.115:/srv/http/git_repo'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
解法:
$ git pull origin master # 這會多一個merge的commit
(編輯修改後)
$ git push origin master
即可將git_svn_repo的修改內容推送到遠端git倉庫

不過上面那段是錯的,原因:這樣git_svn_repo的修改就送不到svn上了



參考資料:
http://stackoverflow.com/questions/5120038/is-it-possible-to-cherry-pick-a-commit-from-another-git-repository

沒有留言:

張貼留言