2015年1月19日 星期一

git合併兩個現有repos的紀錄到同一個repos

假設要把mp3合併到mp2(主要)
下載兩個repos並進入其中主要的branch
$ git clone git@your.domain:bear/mp2.git
$ git clone git@your.domain:bear/mp3.git
$ cd mp2
把mp3加入mp2的remote
$ git remote add mp3 ../mp3
$ git fetch mp3
$ git branch mp3 mp3/master
$ git merge mp3
合併後的git log圖如下:
*   0f54c78 2015-01-19 | fix conflict merge [bear]
|\
| * dc09a26 2015-01-19 | add mp3.txt (mp3/master, mp3) [bear]
| * d6b0993 2015-01-19 | mp3 test commit 2 [bear]
| * 9d89949 2015-01-19 | mp3 first commit [bear]
*   4fea708 2014-11-23 | Merge branch 'master' of your.domain:bear/mp2 (origin/master, origin/HEAD) [b
|\
| * fdc6ced 2014-11-23 | gitlab [bear]
* 719d8ef 2014-11-23 | gitlab [bear]
* a66d115 2014-11-23 | Initial commit [kalecgos0616]
( ~/.bashrc:設定 alias glog='git log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short')
要重新回到mp2 被merge前的 commit( 4fea708 ) 並保留log,因為 0f54c78 中混合了4fea708 和 dc09a26 的code,有衝突的檔案要手動解決conflict,要回到最原本的4fea708
方法1. ( 會多3個revert的commit )
$ git revert dc09a26 d6b0993 9d89949
方法2. ( 只會多一個commit,並回到 4fea708 的狀態)
$ git revert -m 1 0f54c78

下面這方法試不出來,可能是我的0f54c78 是兩個repos merge的node
$ git revert HEAD~2..HEAD

ps. 把本地端某個branch推到gitlab上
$ git checkout -b branch_name remote_name/branch_name
$ git push -u origin branch_name

參考資料:
http://stackoverflow.com/questions/4114095/revert-to-a-previous-git-commit
http://blog.caplin.com/2013/09/18/merging-two-git-repositories/
http://stackoverflow.com/questions/2765421/push-a-new-local-branch-to-a-remote-git-repo-and-track-it-too

沒有留言:

張貼留言