3

Say I have a very simple branch like this

A -- B -- C -- D -- E
*         *         *

in which A, C and E changed file F (i.e. B, D has nothing to do with F). What I'm trying to do is to extract A, B, E and then recombine them into a new branch:

A -- C -- E (new branch)

I don't know how to do it or if it's possible. Thanks.

qweruiop
  • 3,156
  • 6
  • 31
  • 55
  • possible duplicate of [Detach subdirectory into separate Git repository](http://stackoverflow.com/questions/359424/detach-subdirectory-into-separate-git-repository) – poke Apr 07 '14 at 13:24
  • 3
    You can use `git filter-branch`. Note that whatever you try will effectively create different commits A, C and E, which are incompatible to the original ones. – poke Apr 07 '14 at 13:26
  • @poke, please consider converting your comment into an answer as I think this is the only way to go as `git subtree`, which could carry out *almost* this task automatically, works on subdirectories (subtrees) and not on single files. So yes, a way to solve the problem is to "clone" `E` into a new branch and run properly instrumented `git filter-branch` on it. – kostix Apr 07 '14 at 14:18
  • @kostix I would like to have this question closed instead. The linked question shows how to use `filter-branch` in much more detail than I could possibly reproduce here. – poke Apr 07 '14 at 14:25
  • @poke, the page you linked to appears to be quite detailed but keeping a single file is different from keeping a subdirectory... Say, you invoke `git filter-branch --index-filter=...` and pass it what command exactly? `git rm` does not support negative matches (like "everything except whatever matches `*foo`"). – kostix Apr 07 '14 at 16:47

1 Answers1

0

Assuming you're not trying to automate bulk work, the ordinary commands work just fine for this:

git checkout --orphan  newbranch $A
# `git rm` everything but the one file here if that's really what you're after
git commit -a
git cherry-pick $C
git cherry-pick $E
jthill
  • 55,082
  • 5
  • 77
  • 137