有兩種方法
假設源repos: source_repos,目的repos: target_repos
- 使用format-patch
- Step 1: 進到源source_repos的git根目錄
使用git log
找出待拷貝的commit。
- Step 2: 導出commit內容
$ git format-patch <commit>~..<commit>
會在當前目錄下面生成一個0001_XXX.patch文件停忿。這是一個patch diff文件杠氢,可以打開看贤惯。
- Step 3: 修改0001_XXX.patch里面相關文件的路徑(可選)
如果源source_repos和target_repos中文件的路徑發(fā)生了變化老速,那么要手動修改.patch文件里面的相關文件路徑啊研。
舉例來說裙戏,如果文件 common.c文件在源source_repos的路徑是 src/common/common.c乘凸,而在目的target_repos的路徑變成了src/utility/common.c,那么:
前面一步生成的.patch文件會包含:
...
diff --git a/src/common/common.c b/src/common/common.c
index c503614..b237157 100755
--- a/src/common/common.c
+++ b/src/common/common.c
@@ -597,6 +597,10 @@
...
...
于是這個patch文件在目的target_repos上是打不了的挽懦,因為目的target_repos上沒有src/common/common.c這個文件翰意;所以此時就要手動修改這個文件內容,把文件路徑進行調整:
...
diff --git a/src/utility/common.c b/src/utility/common.c
index c503614..b237157 100755
--- a/src/utility/common.c
+++ b/src/utility/common.c
@@ -597,6 +597,10 @@
...
- Step 4: 在目的target_repos里面打上這個patch
顯示進到目的target_repos的git根目錄信柿,有兩個命令可以冀偶。
4.1: $ git am path/to/patch.patch
這個命令直接提交patch文件,即完成commit操作渔嚷,用戶只需要在push到server端就完成了进鸠。
如果命令失敗了,可以使用$ git am --abort
丟棄形病,重新來打客年。
4.2:$ git apply path/to/patch.patch
相比前面的命令霞幅,用這個命令不會提交,所有的文件改動保存在checkout狀態(tài)量瓜,需要用戶手動commit司恳,然后push。
- Step 5:驗證改動的內容
驗證commit的源source_repos上的commit內容一樣绍傲,用戶需要使用git命令查看改動是否是正確的扔傅,例如git show <commit>
。
- 使用cherry-pick
- Step 1: 進到目的target_repos的git根目錄
- Step 2: 增加源source_repos作為remote_repos
$ git remote add [-t <branch>] <source-repos-name> <url_of_source_repos>
例如$ git remote add myremoterepos https://github.com/path/to/oldrepo
注意:這里如果指定了-t <branch>烫饼,那么再后面第三步獲取源source_repos上的所有commit記錄的時候只會獲取此<branch>上的歷史記錄猎塞。
然后可以使用命令查看remote repository的信息:
$ git remote show
$ git remote show <myremoterepos>
- Step 3: 獲取源source_repos上的所有commit記錄
$ git fetch <myremoterepos>
or
$ git remote update [<myremoterepos>]
- Step 4: 列出源source_repos上的commit列表
$ git log <myremoterepos>/<source-branch-name>
- Step 5: pickup需要的commit
$ git cherry-pick <commit1>
$ git cherry-pick <commit2>
...
- Step 6: push到server端
$ git push -u origin <branch>
- Step 7: 刪除remote_repos
$ git remote remove <myremoterepos>