animus144
animus144

Reputation: 93

Applying commits from one subtree to another in same repo

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?

Upvotes: 3

Views: 718

Answers (2)

jthill
jthill

Reputation: 60295

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.

Upvotes: 5

Zac Wrangler
Zac Wrangler

Reputation: 1445

  • 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

Upvotes: 0

Related Questions