Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Ben Jackson's answer already covers the general idea, but I'd like to add a few notes (more than a comment's worth) about the ultimate goal here.</p> <p>You can quite easily have two branches, one with an entirely clean (no private files) history, and one complete (with the private files), and share content appropriately. The key is to be careful about how you merge. An oversimplified history might look something like this:</p> <pre><code>o - o - o - o - o - o - o (public) \ \ \ \ x ----- x ----x---- x - x (private) </code></pre> <p>The <code>o</code> commits are the "clean" ones, and the <code>x</code> are the ones containing some private information. As long as you merge from public to private, they can both have all the desired shared content, without ever leaking anything. As Ben said, you do need to be careful about this - you can't ever merge the other way. Still, it's quite possible to avoid - and you don't have to limit yourself to cherry-picking. You can use your normal desired merge workflow.</p> <p>In reality, your workflow could end up a little more complex, of course. You could develop a topic (feature/bugfix) on its own branch, then merge it into both the public and the private versions. You could even cherry-pick now and then. Really, anything goes, with the key exception of merging private into public.</p> <h1>filter-branch</h1> <p>So, your problem right now is simply getting your repository into this state. Unfortunately, this can be pretty tricky. Assuming that some commits exist which touch both private and public files, I believe that the simplest method is to use <code>filter-branch</code> to create the public (clean) version:</p> <pre><code>git branch public master # create the public branch from current master git filter-branch --tree-filter ... -- public # filter it (remove private files with a tree filter) </code></pre> <p>then create a temporary private-only branch, containing only the private content:</p> <pre><code>git branch private-temp master git filter-branch --tree-filter ... -- private-temp # remove public files </code></pre> <p>And finally, create the private branch. If you're okay with only having one complete version, you can simply merge once:</p> <pre><code>git branch private private-temp git merge public </code></pre> <p>That'll get you a history with only one merge:</p> <pre><code>o - o - o - o - o - o - o - o - o - o (public) \ x -- x -- x -- x -- x -- x -- x --- x (private) </code></pre> <p>Note: there are two separate root commits here. That's a little weird; if you want to avoid it, you can use <code>git rebase --root --onto &lt;SHA1&gt;</code> to transplant the entire private-temp branch onto some ancestor of the public branch.</p> <p>If you'd like to have some intermediate complete versions, you can do the exact same thing, just stopping here and there to merge and rebase:</p> <pre><code>git checkout -b private &lt;private-SHA1&gt; # use the SHA1 of the first ancestor of private-temp # you want to merge something from public into git merge &lt;public-SHA1&gt; # merge a corresponding commit of the public branch git rebase private private-temp # rebase private-temp to include the merge git checkout private git merge &lt;private-SHA1&gt; # use the next SHA1 on private-temp you want to merge into # this is a fast-forward merge git merge &lt;public-SHA1&gt; # merge something from public git rebase private private-temp # and so on and so on... </code></pre> <p>This will get you a history something like this:</p> <pre><code>o - o - o - o - o - o - o - o - o - o (public) \ \ \ x -- x -- x -- x -- x -- x -- x --- x (private) </code></pre> <p>Again, if you want them to have a common ancestor, you can do an initial <code>git rebase --root --onto ...</code> to get started.</p> <p>Note: if you have merges in your history already, you'll want to use the <code>-p</code> option on any rebases to preserve the merges.</p> <h1>fake it</h1> <p>Edit: If reworking the history really turns out to be intractable, you can always totally fudge it: squash the entire history down to one commit, on top of the same root commit you already have. Something like this:</p> <pre><code>git checkout public git reset --soft &lt;root SHA1&gt; git commit </code></pre> <p>So you'll end up with this:</p> <pre><code>o - A' (public) \ o - x - o - x - X - A (public@{1}, the previous position of public) \ x - x (private) </code></pre> <p>where <code>A</code> and <code>A'</code> contain exactly the same content, and <code>X</code> is the commit in which you removed all private content from the public branch.</p> <p>At this point, you can do a single merge of public into private, and from then on, follow the workflow that I described at the top of the answer:</p> <pre><code>git checkout private git merge -s ours public </code></pre> <p>The <code>-s ours</code> tells git to use the "ours" merge strategy. This means it keeps all content exactly as it is in the private branch, and simply records a merge commit showing that you merged the public branch into it. This prevents git from ever applying those "remove private" changes from commit <code>X</code> to the private branch.</p> <p>If the root commit has private information in it, then you'll probably want to create a new root commit, instead of committing once on top of the current one.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload