Applying commits from one subtree to another in sa

2019-07-21 05:38发布

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?

2条回答
Ridiculous、
2楼-- · 2019-07-21 06:12
  • 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

查看更多
女痞
3楼-- · 2019-07-21 06:14

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.

查看更多
登录 后发表回答