1

I need to create an empty orphan branch from another remote branch.
The problem is if I checkout to the parent branch, it will pull the files, and those will then be included in the orphan branch. Or does it matter what branch another branch is created from, if I want to add those changes back to a particular branch?

Is there a way to create an orphan branch from a branch, without being checked into that branch? After adding the remote, I tried running

git checkout --orphan orphan_branch parent_branch 

but it gives me an error "Cannot update paths and switch to branch 'orphan_branch' at the same time."

Or, is it possible to run "git remote add origin giturl", and wind up in a branch other than the master branch?

HamletHub
  • 481
  • 1
  • 9
  • 19
  • What do you mean by checkout a new branch from a branch without its files? Are you looking for this: http://stackoverflow.com/a/13969482/4233593 – Jeff Puckett Oct 22 '16 at 00:39
  • You mean you want to create an empty orphan branch from a non-empty branch? Because if the parent branch is empty, where did those files come from? – Philip Tzou Oct 22 '16 at 00:57
  • I believe that link answers my questions. Only - if I merge the orphan branch back, I don't want it to delete the files. And yes, I would like to create an empty branch from a non-empty branch. And the problem is when you checkout to the branch, it pulls all the files – HamletHub Oct 22 '16 at 01:20
  • Possible duplicate of [How to create a new empty branch for a new project](http://stackoverflow.com/questions/13969050/how-to-create-a-new-empty-branch-for-a-new-project) – Sukima Oct 22 '16 at 02:50
  • I believe it might be different, because I want to merge this back, and I don't want it to delete the other files. I thought there would be a way to checkout without downloading the files – HamletHub Oct 22 '16 at 03:14
  • The phrase "checkout without downloading the *files*" implies that you're not thinking in Git's terms: you don't normally check out *files*, but rather *commits*. You must have the commits. Meanwhile the idea of an "orphan branch with a different parent" *also* makes no sense. Branches do not have parent branches. *Commits* have parent *commits*, and `--orphan` means "for the next commit I make, I would like to have no parent commit." Branches are merely moveable labels, pointing *to* commits. They have no existence outside of the commits they point-to. – torek Oct 22 '16 at 15:06
  • Thank you for that, it helps. I would like the orphan branch so that I can merge it with a particular branch in the future (lets call it branchb), or as you said merge those commits in the orphan branch with commits from a particular branch, not the master branch. In order to do that, I wanted to create the orphan branch directly from a branch other than the master branch. And the issue is that when I checkout to branchb, it then downloads? the commit and I don't have an empty branch to create the orphan from, and I don't want those files (from branch b commit) to merge back to branchb. – HamletHub Oct 22 '16 at 22:25
  • I apologize if this is not the appropriate place for this question, but I might have misunderstood the branch concept. I was assuming branches worked as though each branch was associated with the branch that spawned that branch. Is that true, or is it that every branch is directly off of the main (master) branch? – HamletHub Oct 23 '16 at 00:15
  • I have edited my answer to address those questions. – VonC Oct 23 '16 at 14:15

1 Answers1

2

If you want to go from:

 x--x--x--x--x    (origin/master)
        \
         Y--y--y  (origin/abranch)

to a local orphan branch:

Y--y--y (abranch)

You need to:

Depending on your case, you might find Y (the first commit from origin/abranch not in origin/master) with "how to find first commit of specific branch":

git log origin/master..origin/abranch --oneline | tail -1

If origin/abranch has a single merge base as shown above, then use git merge-base to find Y^ (parent commit of Y, since cherry-pick does not include the first commit itself):

git cherry-pick $(git merge-base origin/master origin/abranch)..origin/abranch

Or does it matter what branch another branch is created from, if I want to add those changes back to a particular branch?

A branch is just a pointer and help referencing all commits accessible from its HEAD. If a branch is spawned from another, that means all of the other branch commits are part of the new branch as well.
See "Using branches" and "Git Branching - Branches in a Nutshell".

It is best to merge back a branch in its the branch it came from (not exactly a "parent" branch as there is no concept of ancestors for branches)

  x--x--x--x--x--M (master)
   \            /
    Y--y------y    (abranch)

If you don't, that means oyu are cherry-pick or rebasing --onto, which is more complex and risk duplicating commits.
That would go from:

 x--x--x--x--x         (master)
        \
         p--p--p       (anotherbranch)
             \
              Y--y--y  (abranch)

To (with git rebase --onto master $(git merge-base anotherbranch abranch) abranch) :

               Y'--y'--y' (abranch)
              /
 x--x--x--x--x            (master)
        \
         p--p--p          (anotherbranch)

Then you can fast-forward abranch to master.

To experiment some more with branches, see "Learn Git Branching".

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • You would want to run the cherry-pick with `Y^..origin/abranch` so as to include commit `Y` itself. – torek Oct 22 '16 at 15:00
  • Right! So... git merge-base then? – VonC Oct 22 '16 at 15:07
  • If there's a single merge base (as in this drawing), yes. – torek Oct 22 '16 at 15:09
  • @torek From my very own (and old) answer (http://stackoverflow.com/a/1994491/6309) "If you want to pick the range `B` through `D` (inclusive) that would be `B^..D`.". I should re-read those answers of mine from time to time ;) – VonC Oct 22 '16 at 15:11