Applying commits from one subtree to another in same repo

牧云@^-^@ 提交于 2019-12-24 02:05:29

问题


I've been using git-p4 to clone parts of a Perforce repo into a git repo. The tree I've checked out has the following Perforce "branch" structure:

repo/releaseA
repo/releaseB
repo/featureA
repo/featureB

I have a bunch of git commits in my local git repo to the featureA directory; however, I'd like to "rebase" those commits onto the featureB directory instead. Is there a way to translate a set of patches/commits that were originally applied to one directory onto another instead?


回答1:


Yep. If your commits affect only repo/featureA this'll be very easy:

mkdir patches
git format-patch -o patches master..my_featureA_branch

git am patches/* -p3 --directory=repo/featureB

and you're done.

If your commits change files outside repo/featureA you need to strip those out,

cat >mystuff.sed <<\EOD
/^(From [0-9a-f]{40}|diff --git )/!{H;$!d}
x
/^From /b
${h;s,.*--,--,;x}
\,^diff[^\n]* [ab]/repo/featureA/,!{$!d;x;b}
${p;x}
EOD

and

sed -s -i -r -f mystuff.sed patches/*

before the git am. Any patches that didn't affect anything at all in repo/featureA you'll have to git am --skip in that case.




回答2:


  • if you only want to pick one single commit then use,

    git cherry-pick <commit-id>

  • if you want to apply a series of commit starting from C1 to Cx, then use rebase --onto

    git rebase --onto featureB C1 Cx



来源:https://stackoverflow.com/questions/19326233/applying-commits-from-one-subtree-to-another-in-same-repo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!