有两种方法
假设源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中文件的路径发生了变化,那么要手动修改.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状态,需要用户手动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>